top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the difference between Call by Value and Call by Reference?

+1 vote
755 views
What is the difference between Call by Value and Call by Reference?
posted Dec 2, 2014 by Vinitha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

3 Answers

+1 vote

In Call by value you are passing only value of that variable to function. so you function can use the value and can do the operation with that value. but the effect will be only throughout that perticullar function. it won't effect in the main function.
but in call by reference you are actually passing the address of that variable so whatever operation you will do will be effected in the main function.

try the same for swapping 2 numbers.
write 1 program for swapping 2 numbers through function. in that first try by passing value you will see there is no swapping in the main function. then try with passing address (pass by reference) you can see the swapping.

I hope i have cleared your doubt.

answer Dec 2, 2014 by Chirag Gangdev
+1 vote

Call by value example:

#include <stdio.h>

void fun_call_by_value(int a, int b)
{
    int t;

    t = a;
    a = b;
    b = t; 
    /*  Here a and b will act as a local variable as t. So the change we have done here is last
         only in this function. So there is no changes at the caller side value of a and b.  */
 }

int main()
{
    int a = 10, b = 20;

    printf(" a = %d and b  = %d\n", a, b);

    fun_call_by_value(a, b);    //try to swap a with b.

    printf(" a = %d and b = %d\n", a, b);

    return 0
}

Here out put will be same:
1st print: a = 10 and b = 20
2nd print: a = 10 and b = 20

Call by reference : Here we will be sending the address of the variable, and if we perform any operation on those variable it will reflect at the source also.
Example:

#include <stdio.h>

void fun_call_by_reference(int *a, int *b)
{
    int t;

    t = *a;
    *a = *b;
    *b = t; 
    /*  Here *a and *b (represent the value at a and b). So the change we have done here will be directly 
         affecting at it source address. So any changes will reflect at the calling funtion. */
 }

int main()
{
    int a = 10, b = 20;

    printf(" a = %d and b  = %d\n", a, b);

    fun_call_by_reference(&a, &b);    //try to swap a with b. 

    printf(" a = %d and b = %d\n", a, b);

    return 0
}

Here out put will not be the same:
1st print: a = 10 and b = 20
2nd print a = 20 and b = 10

Try exectuting these two program and see the result.
Hope your dought is clear now :)

answer Dec 2, 2014 by Arshad Khan
0 votes

When using Call by Value, you are sending the value of a variable as parameter to a function, whereas Call by Reference sends the address of the variable. Also, under Call by Value, the value in the parameter is not affected by whatever operation that takes place, while in the case of Call by Reference, values can be affected by the process within the function.

answer Dec 2, 2014 by Shivaranjini
...