Call by Value vs Call by Reference in C

Call by Value and Call by Reference are related to the way we pass parameters in time of calling C functions.

In Call by Value mechanism, we pass the VALUE of a variable to a function. The called function can use the value but won’t be able to change the value of the original variable. That means the caller function will not see any impact on the value of the variable.

In Call by Reference mechanism, we pass the reference or pointer of a variable to a function. The called function can use the value of the variable as well as change the value of the original variable. The caller function will be able to see the changed value.

Call by Value

Lets consider the code snippet.

void ChangeValue(int X)
{
    X = X + 5;
    prtinf("Value of X inside the function: %dn", X);
}

int main ()
{
    int X = 5;
    
    prtinf("Value of X before function call: %dn", X);
    
    ChangeValue(X);
    
    prtinf("Value of X after function call: %dn", X);
}

Here we have a variable X with value 5. We printed that value before calling the function ChangeValue(). We added 5 with the input variable and printed that in function ChangeValue(). After getting out of the function, we printed the same variable X again. Here is the output.

Value of X before function call: 5
Value of X inside the function: 10
Value of X after function call: 5

From this output it is clear that that value of the variable X is not changed in main() even if we tried to change that in function ChangeValue(). This figure will help you to understand what happened under the hood.

Fig 1: Call by Value

When we initialize the variable X with 5 in main(), memory is allocated for that variable (say 0x12345678) and the value 5 was stored in that location. When we called the ChangeValue() function, another variable got created in different memory location (say 0x23456789). It is important to understand that variable X in function ChangeValue() is actually a different than the X of main(). When we changed the X in ChangeValue(), the value of location 0x23456789 got changed. It does not have any impact on the variable location at 0x12345678.

Call by Reference

The above code is little bit modified to call the ChangeValue() function with reference of the variable.

void ChangeValue(int* X)
{
    *X = *X + 5;
    prtinf("Value of X inside the function: %dn", *X);
}

int main ()
{
    int X = 5;
    
    prtinf("Value of X before function call: %dn", X);
    
    ChangeValue(&X);
    
    prtinf("Value of X after function call: %dn", X);
}

The signature of the function is slightly different – it takes a pointer of an integer as input as opposed to a simple integer.

In time of calling the function we passed the reference (pointer) of the variable X like ‘ChangeValue(&X);‘. Here is the output of the program.

Value of X before function call: 5
Value of X inside the function: 10
Value of X after function call: 5

From this output we can see that the caller function main() can see the changed value. This figure will help you to understand what happened.

Fig 2: Call by Reference

As before when we initialized variable X in main(), memory (ox12345678) was allocated where the value 5 is stored. When we called the function ChangeValue(), another variable X (located at 0x23456789) is created. But this X variable is a pointer variable that holds the location (or pointer) of the original variable X. That means the value of X in ChangeValue() is 0x12345678.

In function ChangeValue() we did not change the value of pointer X but we changed the value pointed by pointer X. Here is the statement : ‘*X = *X + 5‘. As pointer X was pointing to the original X variable, the value of original X got changed.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

One thought on “Call by Value vs Call by Reference in C”

Leave a Reply

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

0
0
0
0
0
0