14 Jan Call by Value in C
In Call by Value, when a function is called, the value of the actual parameter is copied into the formal parameter. This means changes made to the formal parameter inside the function do not affect the actual parameter. It ensures data protection, as the original data remains unchanged.
Actual parameters are the values or variables passed to the function when it is called. Formal parameters are the variables defined in the function declaration and definition that receive the values of the actual parameters.
Let us see an example to implement Call by Value in C programming:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Call by Value in C #include<stdio.h> void changeValue(int x) { x = 10; printf("Value = %d\n", x); } int main() { int a = 5; printf("Value of a (before) = %d\n", a); changeValue(a); printf("Value of a (after) = %d\n", a); return 0; } |
Output
1 2 3 4 5 |
Value of a (before) = 5 Value = 10 Value of a (after) = 5 |
In the changeValue() function, the x is assigned the value of a, which is 5. Then x is set to 10.
After calling the changeValue() function above, the value of a remains unchanged because changeValue() only modifies the copy of a (i.e., the parameter x).
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others:
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments