What is Call by Value and Call by Reference in C Programming
What is Call by Value and Call by Reference in C Programming
In todays blog post we are going to cover a very important concept that is call by value and call by reference. In C programming language there are two primary ways to pass arguments to a function, the first one is call by value and the second one is call by reference.
1) Call by value
2) Call by reference
Now we will have a brief look at how these two primary ways of function calling works in C programming language. In call by value method we simply pass the value that is contained by the variable and in call by reference we simply pass the memory address of variable instead of its value. Just remember one thing at this point, the function parameter must be declared as a pointer(*) so that it can hold the address of variable or argument passed during function call.
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.
Call by value example in C programming
#include<stdio.h>
void CallByValue(int value1) { value1 = value1 + 10; printf("Value after addition inside function = %d \n", value1); } void main() { int value = 50; printf("Value before function call = %d \n", value); CallByValue(value); printf("Value after function call = %d \n", value); _getch(); }
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.
int value = 50;
CallByValue(value);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 or main() function.
Output of coding example
Value before function call = 50
Value after addition inside function = 60
Value after function call = 50
Call by Reference example in C Programming
#include<stdio.h> void CallByReference(int* value1) { *value1 = *value1 + 10; printf("Value after addition inside function = %d \n", *value1); } void main() { int value = 50; printf("Value before function call = %d \n", value); CallByReference(&value); printf("Value after function call = %d \n", value); _getch(); }
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).
int value = 50;
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.
Output of coding example
Value before function call = 50
Value after addition inside function = 60
Value after function call = 60
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! like always we have successfully implemented the concept of call by value and call by reference in C programming language.
Comments
Post a Comment