What is Call by Value and Call by Reference in C Programming
- Get link
- X
- Other Apps
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.
- Get link
- X
- Other Apps
Comments
Post a Comment