If you call a function with an argument that corresponds to a non-reference parameter, you have passed that argument by value. The parameter is initialized with the value of the argument. You can change the value of the parameter (if that parameter has not been declared const) within the scope of the function, but these changes will not affect the value of the argument in the calling function.
The following are examples of passing arguments by value:
The following statement calls the function printf, which receives a character string and the return value of the function sum, which receives the values of a and b:
printf("sum = %d\n", sum(a,b));
The following program passes the value of count to the function increment, which increases the value of the parameter x by 1.
/** ** An example of passing an argument to a function **/ #include <stdio.h> void increment(int); int main(void) { int count = 5; /* value of count is passed to the function */ increment(count); printf("count = %d\n", count); return(0); } void increment(int x) { ++x; printf("x = %d\n", x); }
The output illustrates that the value of count in main remains unchanged:
x = 6 count = 5
Related References