While Loop in C++ Programming Language

Image
  While Loop in C++ Programming Language We have discussed about the entry-controlled loop or pre-tested loop previously in our blog posts. These statements or loops are used to repeat a piece of code for a specific number of times as per our program requirement. We have studied about the for loop already. Today in this blog post we are going to study about the while loop in C++ programming language. And we will look at how this while loop actually works and how we can stop it by making some condition false so that the loop might not run infinitely. We use while loop in C++ program when the number of iterations is not known in advance and depends on the condition. When we know exactly that how many times a loop should run then we use the for loop instead of while loop. Basic Syntax Of While Loop in C++ Programming Language Basic Working Of While Loop In C++ Programming Language Now we are going to discuss about the basic working of while loop in C++ programming....

What is Call by Value and Call by Reference in C Programming


What is Call by Value and Call by Reference in C Programming

We use functions in programming languages like C, C++, C#, etc. Today we will cover some important concepts about functions that are implemented in C programming language. The topic we will cover today is the functions and the use of call by value and call by reference in C programming. This is a very important topic for functions. As we all know when we use functions we can pass values to functions from within the main program, the point where we call a function. There are two ways to pass data to functions in C language i.e.: Call by value and Call by reference.





What is Call by value in C programming




In Call by value, we pass the values of parameters or arguments that are used inside the main program. We only send variables' values to the function parameters. As shown in the above coding example, we cannot modify or change the values of parameters that are used inside the main program.

In the above coding example, we pass the value parameter to the function named CallByValue(value) 

In call by value, different memory is allocated to both the parameters that are used during the function call and the parameters that are used inside function round brackets, during the function definition.

In call by value, the value of the actual variable or argument is not changed. For example in the above code, the value of the value variable before and after the function call will be the same as 50, though we changed the value of the value1 variable inside the function value will not change inside the main program.


What is Call by Reference in C Programming




Now what is call by reference. It is different from call by value in that during the call by reference method we pass the reference or address of the arguments used during the function call.

In the above coding example, we pass the value parameter to the function with the & operator which specifies the address or reference of the argument passed CallByReference(&value).

Another very important point is that in the call-by-reference, the value of the arguments that we used during the function call can be modified by changing the value of the parameters that are used inside the function definition point within the curly round brackets. The main reason behind the value change is that during the function call, we pass the address of the arguments that are used during the function call.

For example, in the above code, the value of the value variable will be changed before and after the function call. First, it is 50, but after the function call is completed, the value of the actual variable is changed to 60.

In call by reference, the same memory location is used for both the arguments that we use during the function call and the function parameters that are used in the function definition point. That is why if we change the value of the parameters in the function definition it is reflected inside the main program variables.


Comparison between call by value and call by reference

In call by value method, the value of the arguments is passed to the function definition, whereas in call by reference the address of the arguments is passed to the function definition.

In call-by-value, changes in the values of parameters in the function definition are not reflected in the main program variable values, whereas in call-by-reference, changes in the values of parameters in the function definition are reflected inside the main program arguments values.

In call by value, both the arguments and the parameters have different memory locations whereas in call by reference both the arguments and the parameters have the same memory location.

Congratulations we have successfully gathered and understood the concept of call by value and call by reference.







Comments

Popular posts from this blog

How to run c project in visual studio community 2022

While Loop in C++ Programming Language

if and if else statement in C++ programming language