Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

 

In function topic, we have seen how arguments are sent from one function to another function.
But in function topic, we have seen only one type of passing values known as call by value.
Actually, there are two ways of passing values from caller function to called functions, as specified below.

1) Call by Value
2) Call by Reference

Now let’s see what they are actually & difference between them.

Call by Value

In call by value, a copy of the actual argument is sent from caller function to called function.
Therefore if the called function makes some operation on the value that is sent to it, the original value present in the caller function is not affected at all. Because the operation is done on a duplicate copy, not original.
For better understanding, look at the example shown below:

C Programming Language Call by Value Example Step 1


C Programming Language Call by Value Example Step 2

In the above figure after sending a duplicate copy of an original paper from caller function to called function, called function pours color on it and makes it colorful.
But in this case, the only duplicate copy is affected. Original copy remains as it is without any effect on it at address 1000.

Example

#include <stdio.h>
void calledFunction(int a, int b);
int main()   // here main() is a caller function
{
    int i=5,j=10;
    printf("\n Before sending i & j");
    printf("\n i = %d, j = %d",i,j);
    calledFunction(i,j);
    printf("\n");
    printf("\n After sending i & j");
    printf("\n i = %d, j = %d",i,j);
    return 0;
}

void calledFunction(int a, int b)
{
    a = a*2;
    b = b*2;
}

Output

Before sending i & j
i = 5, j = 10

After sending i & j
i = 5, j = 10

For the better understanding, look at the diagram shown below:

C Programming Language Call by Value Working

Leave a reply