C - Call by Value in Functions

<<Previous

Next >>





In this method, the value of the variable is passed to the function. That means, only copy of the actual parameter's value is passed to the function. Here, different memory location allocated for both actual and formal parameters. If called function changes the value of the formal parameter, it will not change the original value of the actual parameter which means the changes are done only on the called function. While calling, parameter name is being passed in function call. In C programming, this method is used as default method to pass argument to the function.

Let's see the example program using call by value.

#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.


<< Previous

Next >>