Difference between function call by value and call by reference

Best explanation on difference between function call by value and call by reference

In C programming language, functions can be called by another functions. Functions can have multiple arguments. There are two ways functions with arguments can be called- call by value and call by reference. In functions call by value, the variable values are passed to the function arguments during the function call. For example, add(a, b) is a function call from another function with two arguments and values are passed via arguments. But in case of function call by reference, during the function call, the address of the variable is passed like add(&a, &b).The function add() itself, accepts deferenced pointers to the variables a and b such as add(int* x, int* y). See the following C program example of add() function to illustrate function call by value and function call by reference.

Examples of Function Call by Value:

Examples of Function call by reference:

So far you may have understood the difference between Function call by value and Function call by reference in that in case of function call by value, the value are passed from the calling function to the called function and in case of function call by reference, the address of the variable is passed from the calling function and the calling function accepts the deferenced pointers of the variables. And you may have learned how to implement these.

But, is there more to it than just this. Aren’t there any advantages? The method of calling the function by reference does have number of benefits. If we use function call by value, the variable value is copied from the called function to the calling function. Which means that the variable value in the calling function remains the same regardless of what you do in the called function. Also the difference is also in the memory allocation. In case of function call by value, memory is allocated for the variables. But in case of function call by reference, memory is not allocated in advance. Only when the calling function is run, the memory is allocated. So in simple words, function call by reference saves memory allocation.

There are also other between Function call by value and Function call by reference. Next we illustrate two important ones:
1. Using Function call by value, the variables value are not updated in the calling function such as main() when the value changes in the called function such as the add() function.
2. If we use Function call by value, we cannot pass data types such as array from calling function such as main() to the called function such as add().

The first difference can be illustrated using example of swap() function. The function accepts two integer values from main() function and swaps the integers. Once swapped, the first variable contains the value of the second variable and vice versa. In some programs, one may want to swap the variable both in the calling function (main()) and called function (swap()) but because of using function call by value, the variable are not swapped in the calling function. It is swapped only in the called function swap() locally. In such case, function call by reference is used. This is one advantage of using function call by reference.

The following example code illustrates swap function call by value and how the local variables in main() function retains the variable value although the variable value were swapped by the swap() function.

If you run this program you will get the following output.

swap function

As you can see from the output, the variables value is not swapped after the function swap() was used.

Now,the following example code illustrates the swap function where the value is passed by reference. It illustrates that the values in both the calling function main() and in the called function swap() are changed.

The following is the output of this program.

As you can see in this case, the numbers are swapped.

Note that the C program codes was compiled and run with Code::Blocks. See How to set compiler for code blocks for more information on this.

What do you think?

0 points
Upvote Downvote

Total votes: 0

Upvotes: 0

Upvotes percentage: 0.000000%

Downvotes: 0

Downvotes percentage: 0.000000%

Leave a Reply

Your email address will not be published. Required fields are marked *

learning structures in C

Learning to Write Structures in C

Printing Array values using pointers