CALL BY VALUE

·

In the preceding examples we have seen that whenever we called a function we have always passed the values of variables to the called function. Such function calls are called ‘calls by value’ by this what it meant is that on calling a function we are passing values of variables to it.

The example of call by value are shown below ;
sum = calsum (a, b, c);
f = factr (a);

In this method the value of each of the actual arguments in the calling function is copied into corresponding formal arguments of the called function. With this method the changes made to the formal arguments in the called function have no effect on the values of actual argument in the calling function. the following program illustrates this

main ( )
{
int a = 10, b=20;
swapy (a,b);
printf (“\na = % d b = % d”, a,b);
}
swapy (int x, int y)
{
int t;
t = x;
x = y;
y = t;
printf ( “\n x = % d y = % d” , x, y);
}

The output of the above program would be;
x = 20 y = 10
a =10 b =20

About Me

Blog Archive