Difference Between Call By Value and Call By Reference

By | May 24, 2016

Difference between Call By Value and Call By Reference

Here’s an article that offers a complete guide on the difference between call by value and call by reference approach. There are two C programs below that demonstrates the difference between call by reference and call by value in C programming language. Apart from the explanation, we have also mentioned the difference between call by value and call by reference in C in tabular form at the bottom of this article.

Note: There is no such thing as call by reference or pass by reference in C programming language. Every argument is passed by using call by value or pass by value method in C Language. It’s just a false impression and the use of pointers is assumed to be working as call by reference in C programming. Everything is done through pass by value approach. You’re actually passing the value of a pointer to another function.

Learn Difference Between Call By Value and Call By Reference in C Programming

Call By Value Method

In Call By Value method, the arguments are passed to a function using the value of the variable. There is a second copy of the variable is formed while passing values from one function to another.

Therefore, the original values or the original variables do not change. The call by value and call by reference approach are mostly used while passing values to functions in C programming.

The original variables are called Actual Parameters whereas the new values that are passed to the function are called as Formal Parameters.

In case of call by value approach, the copy of the values are passed to the function. The actual parameters and formal parameters are thereby created in different memory locations. Therefore, the original value is not modified in call by value method in C programming.

The values passed to the User Defined Function are created on Stack memory location. Therefore, the changes made within the user defined function is not visible in the main() method or any other method for that matter.

The disadvantage with pass by value method is that there are two copies created for the same variable which is not memory efficient. However, the advantage of call by value approach is that this method doesn’t change the original variables thereby preserving data. The following program will demonstrate how to write a C code to swap two variables using call by value method.

In the following C program to swap variables with Call By Value approach, the variables var1 and var2 are passed to the swap() method by using their values and not the addresses.

When the user defined function swap() receives the values of var1 and var2, two local copies are created for that variables in the function. These local values val1 and val2 are manipulated by the program itself and there is no effect on the actual parameters var1 and var2 that were passed from the main() function.

Also Read: C Program To Swap Two Variables using Call By Value Approach

C Program To Swap Variables using Call by Value Method

Call By Reference Method

In Call By Reference method, the arguments are passed to another function using the address of the variables. This address is also known as Reference Pointers. There is no second copy of the variable while passing values from one function to another.

 

Since, the original variables’ address is passed, the modifications done to the variable in the User Defined will change the original variable as well. Therefore, the Original Values will change.

The original variables are called Actual Parameters whereas the new values that are created within the user defined function are called Formal Parameters. In case of call by reference approach, the address of the original values are passed to the function.

The actual parameters and formal parameters are thereby created in the same memory location. All the modification is done on actual parameters. Therefore, the original value is modified in Pass By Reference method in C programming.

The changes made within the user defined function are visible in the main() method and any other method for that matter. We have demonstrated below a C code for call by reference in C using Pointers.

The Advantage of using pass by reference approach is that it does not create duplicate data for holding only one value. Therefore, this helps to save memory space. The following program will demonstrate how to write a C program to swap two variables using pass by reference method.

In the C program to swap variables with passing by reference approach, the variables var1 and var2 are passed to the swap() method by using their addresses and not the values.

When the User Defined Function swap() receives the addresses of var1 and var2, the pointers *ptr1 and *ptr2 holds the values of the var1 and var2 since the addresses are passed to them. Therefore, no local copies are created. The original variables var1 and var2 will be directly modified by prt1 and ptr2.

Also Read: Swapping Two Numbers using Call By Reference approach in C Programming

C Program To Swap Variables using Call By Reference Method

 

 

Difference Between Call By Value and Call By Reference in Points

 

Sr. No.Call By ReferenceCall By Value
1The address of the variables are passed as arguments to the function in Pass By Reference method.The value of the variables are passed as arguments to the function in Pass By Value method.
2No copy of the variables are created on the memory stack.A copy of the original variables are created in memory stack.
3The modifications are made to the original variables which is the old value that was passed to the function.The modifications are made to the new variables which are known as formal variables.
4The changes are visible in every function.The changes are visible only in a particular method where the arguments are passed.
5The pointers are used in call by reference approach.No pointers are used in call by value approach.
6The pass by reference method creates only a single copy, thereby, providing memory efficiency.The pass by value approach creates two or more copies of the original variables and, hence it is not memory efficient.
7The actual and formal parameters are created on the same memory stack.The actual and formal parameters are created on a different memory stack.

If you have any compilation error with the above given C program for passing by reference and passing by value or if you have any doubts about difference between call by value and call by reference in C programming, let us know about it in the comment section below.

12 thoughts on “Difference Between Call By Value and Call By Reference

  1. Harish

    Excellent Details for Call By Reference in C Programming. I was not aware about the fact that there is no Call By Reference in C Programming actually. Anyway Thanks. 🙂

    Reply
    1. Tushar Soni Post author

      Your Welcome Harish! Not every article regarding the difference between call by value and call by reference mentions about the fact that there is no call by reference in actual. I’ve got this information after a lot of research. Hope it comes to use in your future.

      Reply
  2. C Noob

    There are a few syntax errors in both of your coding examples. For instance, neither of your swap functions return an int, and often time they refer to a local var called, “ptr1” and “ptr2” when they should actually be “val1” and “val2”

    Reply
  3. Parag Vidhate

    This is something new that I found in differences between call by reference and call by value. Thanks for this. I hope it helps me someday.

    Reply
  4. Rajiv Makhija

    This means that call by reference is primarily done using pointers. But, there is no such thing as call by reference.

    Reply
  5. Punit Tongia

    So, in call by reference, the original passed values are modified whereas in call by value, a local copy of the passed variables is created and they are modified. The original values / variables remains the same.

    Reply
  6. Sachin Bhoi

    Amazing. Thank you so much. Finally, I can now write something good in my exams if this questions comes up. 🙂

    Reply
  7. Raj Dixit

    Nice explanation. Thanks a lot. You have so many other good notes about programming. Exploring them one by one.

    Reply
  8. Shashank Yadav

    The distinguish of call by value and call by reference is just so complete. It has helped me to understand the complete concept.

    Reply
  9. Vishar P.

    Thanx a lot, Tushar!

    For the first time I got simple and clear explanation about Call by value and Call by reference methods in C. Keep it up…

    Reply

Let's Discuss