Difference between Call by Reference and Call by Value | Use of Pointer

Call by reference vs Call by value: In this article, we are going to learn the difference between call by reference and call value along with the use of pointer in C.
Submitted by Radib Kar, on September 06, 2019

If we consider the main use of pointer, then it is,

  1. Dynamic memory allocation
  2. Call by reference

Difference of call by value and call by reference

  1. Call by value
  2. Call by reference

First, go to through the code, run it and follow the output.

Code:

#include <stdio.h>
#include <unistd.h>

//function for call by value
void swap_call_by_value(int a, int b)
{

    //normal swap operation

    int temp;
    temp = a;
    a = b; //a now have value of b
    b = temp; //b now have value of a
    printf("Printing with in the function call by value swap...\n");
    printf("value of a afrer swap(call by value) %d \n", a);
    printf("value of b afrer swap(call by value) %d \n", b);
}

//function for call by reference
void swap_call_by_reference(int* a, int* b)
{

    //follow the parameter
    //a and b is now pointr type variables

    //normal swap operation

    int temp;
    temp = *a; //temp stores value at address &a
    *a = *b; //value at address &a is now value of address &b
    *b = temp; //value at address &b is now temp
    printf("Printing within the function call by reference swap...\n");
    printf("value of a afrer swap(call by reference) %d \n", *a);
    printf("value of b afrer swap(call by reference) %d \n", *b);
}

int main()
{

    int a = 10, b = 20;
    printf("Let's swap using call by value\n");
    sleep(2);
    swap_call_by_value(a, b);
    printf("the above function should have swapped already, \n");
    printf(" as it shows result printed with in function\n");
    sleep(2);
    printf("But is it swapped?\n");
    printf("Let's check by Printing here...\n");
    sleep(2);
    printf("value of a after swap(call by value) %d \n", a);
    printf("value of b after swap(call by value) %d \n", b);

    printf("you can see value is same as before, no swap\n");
    sleep(2);
    printf("What happened then?\n");
    sleep(1);
    printf("But before lets swap perfectly\n");

    printf("swapping by call by reference");
    sleep(2);

    //call by reference, address/reference is passed, not the value
    swap_call_by_reference(&a, &b);
    sleep(2);
    printf("the above function should have swapped already, \n");
    printf("as it shows result printed with in function\n");

    printf("But is it swapped?\n");
    printf("Let's check by Printing here...\n");
    sleep(2);
    printf("value of a after swap(call by reference) %d \n", a);
    printf("value of b after swap(call by reference) %d \n", b);

    printf("you can see now value is swapped...\n");

    return 0;
}

Note: The sleep() function used in code is only for display purpose, no relation with code.

Output

Let's swap using call by value
Printing with in the function call by value swap... 
value of a afrer swap(call by value) 20 
value of b afrer swap(call by value) 10 
the above function should have swapped already, 
 as it shows result printed with in function
But is it swapped?
Let's check by Printing here... 
value of a after swap(call by value) 10 
value of b after swap(call by value) 20 
you can see value is same as before, no swap
What happened then? 
But before lets swap perfectly
swapping by call by referencePrinting within the function call by reference swap... 
value of a afrer swap(call by reference) 20 
value of b afrer swap(call by reference) 10 
the above function should have swapped already, 
as it shows result printed with in function 
But is it swapped?
Let's check by Printing here... 
value of a after swap(call by reference) 20 
value of b after swap(call by reference) 10 
you can see now value is swapped...

Explanation:

In case of call by value:

You can see that within the function it looked like it has swapped successfully, but when printed in the main function we can see values have not been swapped. The reason is the swapping took place within the local variables of the function.

Our function call in the main function: swap_call_by_value(a,b);

Here a, b is the variable defined in main function

But in the function definition,

    void swap_call_by_value(int a, int b){
        ...
    } 

Here a, b is different, these are local variables of the above function.

Say these two are local_a, local_b

And a, b of the main function are main_a, main_b.

Addresses of local_a, local_b is different from the main_a, main_b.

Hence, the swap operation in the function is also local, the effect not reflected in main function, but working fine in the function itself.

While calling a function, the function saves its local variable in its own activation record. Thus, it can't update the variable of main.

In case of call by reference

You are sending addresses of your variables in the main function swap_call_by_value(&a,&b);

Hence the swapping is taking place on the same variable defined in the main function, thus, it's reflected even in the main function.

That's why to swap by function always use call by reference, and on other cases, you can use call by reference (use of pointer) where you need to reflect the effect of function on the variables declared in main functions.


Related Tutorials




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.