Passing argument to a function by value is also know as call by value. This method of passing arguments to a function copies the original value of argument into the formal parameter of a function. In other words if we change parameter which are given inside a function posses no effect on the argument .
C language support by default call by value for passing arguments.
Below is the example of how to pass argument by value.
// function definition for swapping two integer type values a and b
void swap(int a, int b)
{
int t;
t = a; // t variable save the value of a
a = b; // we put value of b into a
b = t; // put t into b
return;
}
Now, we call the function swap() by passing original values .
#include <stdio.h>
void swap(int a, int b);
int main () {
int x = 5; // local variable definition
int y = 6;
printf("Before swap, value of x : %d\n", x );
printf("Before swap, value of y : %d\n", y );
// Now we are calling a function to swap two values
swap(x, y);
printf("After swap value of x : %d\n", x );
printf("After swap value of y : %d\n", y );
return 0;
}
Before swap value of x :5
Before swap value of y :6
After swap value of x :5
After swap value of y :6
This shows that if we change parameter which are given inside a function posses no effect on the argument .
0 Comment(s)