Call by Value and Call by Reference in C

In this tutorial, you will learn about call by value and call by reference in the C programming language with the help of examples.


In C, a function call can be divided into two categories in terms of passing parameters to them. They are –

  • Call by Value
  • Call by Reference

Before jumping further into this topic, let’s understand actual parameters and formal parameters.

The parameter passed to the function is called the actual parameter. The parameters declared by the function to receive the values of the argument are called formal parameters.

Now let’s understand call by value and call by reference in the C programming language one by one.


Call by Value

  • In call by value method, the value of the actual parameters copies to the formal parameter. In other words, we can say that the value of the variables used in the function call is used in the call by value method.
  • In Call by Value method, the formal parameter cannot modify the value of the actual parameter because the formal parameters work on the copies of the actual parameter.
  • Different memory is allocated for the formal and actual parameters.
  • The formal parameters are the variables that are used in the function definition whereas the actual parameters are used in the function call.

Example

Let’s understand the concept of the Call by Value in C language with the help of examples.

// C call by Value example
#include <stdio.h> 
void funct(int x)   // Only a copy of the variable is passed
{
    x = 30;
}

int main() {
    int x = 10;
    printf("The value of x before function call = %d",x);
    funct(x);   // Call by value where the variable is passed
    printf("\nThe value of x after function call = %d",x);
    return 0;
}

Output

The value of x before function call = 10
The value of x after function call = 40

Call by Reference

  • In call by reference, the address of the actual parameter is passed to the formal parameter.
  • The values of the actual parameter can be modified in this case since the address of the actual parameter is passed.
  • In call by reference, the same memory address is used by both the formal and actual parameters. Any changes made inside the function definition are store in the memory location of the actual parameter and the modified value gets reflected in the actual parameter.

Example

Let’s check an example of the call by reference in the C language.

// C call by Reference example
#include <stdio.h>  
void funct(int *p) // The address of the variable is accepted by a pointer
{
    *p = 40;   // The value at the address p is updated
}
int main() {
    int x = 10;
    printf("The value of x before function call = %d",x);
    funct(&x);  // Call by Reference where the address is passed
    printf("\nThe value of x after function call = %d",x);
    return 0;
}

Output

The value of x before function call = 10
The value of x after function call = 40

Please get connected & share!

Categories

Advertisement