• In my last post I discussed about “Types of Function”In this post I will serve one important concept which is related to the function e.i Call by value v/s Call by reference or sometimes also referred to pass by value v/s pass by reference.
  • We have learnt in my previous post about function with arguments and as we know that while calling such functions we have to supply appropriate arguments.
  • There are two ways through which we can pass arguments to the function.
  1. Call by Value (Pass by Value)
  2. Call by Reference (Pass by Reference) 
  • In this post I am going to discuss the basic difference between these two methods.
  • Difference between these two methods can be summarized as below:
Call by Value Call by Reference
During the function call if we pass values of arguments to the called function instead of memory address than it is referred to as pass by value. During the function call if we pass memory address of arguments to the called function then it is referred to as pass by reference or pass by address.
In pass by value method, a duplicate copy of the original arguments is created and that copy is passed to the called function. In pass by reference method, since we are passing memory location of the arguments, no duplicate copy will be prepared
Any changes made to the passed arguments inside the calling function are reflected to that copy only. No changes are made to the original copy of the arguments. That means our original value of the arguments inside the main () will be not affected. Any changes made to the arguments inside the calling function will be directly reflected to the actual arguments inside the main () also.
It does not require pointer to receive arguments as values are passed and not the memory address. It requires pointer to receive arguments as memory addresses are passed.
  • The following example to swap two values using call by value and call by reference demonstrates the difference between these two methods.

Example using Call by Value

  • The following program uses swapByValue() function to exchange values of X and Y using call by value method.

#include "stdio.h"

void swapByValue(int, int); /* Function Prototype */

void main() /* Main function */
{
    int x = 10, y = 20;
    
    printf("\n Before swapping X: %d, Y: %d\n", x, y);
    /* actual arguments will be as it is */
    swapByValue(x, y);     /* calling function for swapping */
    printf("\n After swapping X: %d, Y: %d\n", x, y);
}

void swapByValue(int a, int b)  /* Function definition */
{
   int t;
   t = a; a = b; b = t;    /* swapping */
}

OUTPUT
======
X: 10, Y: 20
  • Since any changes made to the passed arguments inside the calling function are reflected to that copy only and no changes are made to the original copy of the arguments, values of X and Y are not affected as it operates on local copies of a and b only.

Example using Call by Reference

  • The following program uses swapByReference() function to exchange values of X and Y using call by reference method. Note that we are passing addresses of X and Y inside the function.
#include "stdio.h"

void swapByReference(int *, int *); /* Function Prototype with pointer */

void main() /* Main function */
{
    int x = 10, y = 20;
    
    printf("\n Before swapping X: %d, Y: %d\n", x, y);
    /* actual arguments will be as it is */
    swapByReference(&x, &y);     /* Passing addresses for swapping */
    printf("\n After swapping X: %d, Y: %d\n", x, y);
}

void swapByReference(int *a, int *b)  /* Function definition with pointer */
{
   int t;
   t = *a; *a = *b; *b = t;    /* swapping */
}

OUTPUT
======
X: 20, Y: 10
  • Since any changes made to the passed arguments inside the calling function are reflected to the original copy of the arguments, values of X and Y are affected directly.
  • I hope after reading this post, it would be clear to you how these two methods behave differently.