Call by Value in C and C++ - Pass by Value
In the Call-by-value approach in C programming, a copy of the actual parameter value is passed to the function. Different memory locations are allocated for actual and formal parameters. If the called function changes the value of the formal parameter, the value in the calling function does not change. Modifications are done only within the called function. While calling or invoking the function, the parameter name is being passed in the function call. In C programming, the call-by-value approach is the most common means to pass an argument to the function.
Let us see the example program using call by value.
Call by Value Example
#include<stdio.h> int multiply(int, int); void main() { int a = 1, b = 2, c; printf("The value of a and b: %d\t%d", a,b); c= multiply(a,b); printf("\nThe value of a and b after function call: %d\t%d ", a,b); printf("\nThe multiplication result: %d",c); getch(); } int multiply(int m, int n) { int result; m = m + 2; result = m * n; return(result); }
Here the copy of actual parameter values (a & b) are passed to the function call. When you compile and run the above program, you will get the following output.
The result shows that the changes made inside the called function will not affect the original value of the actual parameters.