Call by value and call by reference in C

There are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference.

Call by value and call by reference in C

call by value and call by reference in c

Let's understand call by value and call by reference in c language one by one.

C provides two ways of passing arguments to a function.

1.Call by value or Pass by value.

2.Call by reference.

Call by value in C

In call by value, original value is not modified.

In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().

Let's try to understand the concept of call by value in c language by the example given below:

In call by value



#include<stdio.h>
void change(int num) {    
    printf("Before adding value inside function num=%d \n",num);    
    num=num+100;    
    printf("After adding value inside function num=%d \n", num);    
}    
int main() {    
    int x=100;    
    printf("Before function call x=%d \n", x);    
    change(x);//passing value in function    
    printf("After function call x=%d \n", x);    
return 0;  
}    



Output

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=100




#include<stdio.h>
void try_to_change(int, int);

int main()
{
    int x = 10, y = 20;

    printf("Initial value of x = %d\n", x);
    printf("Initial value of y = %d\n", y);

    printf("\nCalling the function\n");

    try_to_change(x, y);

    printf("\nValues after function call\n\n");

    printf("Final value of x = %d\n", x);
    printf("Final value of y = %d\n", y);

    // signal to operating system program ran fine
    return 0;
}

void try_to_change(int x, int y)
{
    x = x + 10;
    y = y + 10;

    printf("\nValue of x (inside function) = %d\n", x);
    printf("Value of y (inside function) = %d\n", y);
}



Expected Output:

Initial value of x = 10

Initial value of y = 20

Value of x (inside function) = 20

Value of y (inside function) = 30

Values after function call

Final value of x = 10

Final value of y = 20

How it works ?

The variables x and y inside function main() and variable x and y in the formal arguments of function try_to_change() are completely different. In line 10, when try_to_change() function is called a copy of values of x and y is made and that copy is passed to the formal arguments x and y of the function try_to_change(). Inside the function try_to_change() we have tried to change the original value of x and y by assigning new values to it. Since try_to_change() is working on a copy of x and y, changes made by try_to_change() function will have no effect on the actual arguments x and y.

Call by reference in C

In call by reference, original value is modified because we pass reference (address).

Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

Note: To understand the call by reference, you must have the basic knowledge of pointers.

Let's try to understand the concept of call by reference in c language by the example given below:




#include<stdio.h>
void change(int *num) {    
    printf("Before adding value inside function num=%d \n",*num);    
    (*num) += 100;    
    printf("After adding value inside function num=%d \n", *num);    
}      
int main() {    
    int x=100;    
    printf("Before function call x=%d \n", x);    
    change(&x);//passing reference in function    
    printf("After function call x=%d \n", x);    
return 0;  
}    



Output

Before function call x=100

Before adding value inside function num=100

After adding value inside function num=200

After function call x=200

Difference between call by value and call by reference in c

To use call by reference we need to do two things:

  1. Pass the addresses of the actual arguments instead of passing values to the function.
  2. Declare the formal arguments of the function as pointer variables of an appropriate type.

The following program demonstrates call by reference.




#include<stdio.h>
void try_to_change(int *, int *);

int main()
{
    int x = 10, y = 20;

    printf("Initial value of x = %d\n", x);
    printf("Initial value of y = %d\n", y);

    printf("\nCalling the function\n");

    try_to_change(&x, &y);

    printf("\nValues after function call\n\n");

    printf("Final value of x = %d\n", x);
    printf("Final value of y = %d\n", y);

    // signal to operating system everything works fine
    return 0;
}

void try_to_change(int *x, int *y)
{
    (*x)++;
    (*y)++;

    printf("\nValue of x (inside function) = %d\n", *x);
    printf("Value of y (inside function) = %d\n", *y);
}



Expected Output:

Initial value of x = 10

Initial value of y = 20

Calling the function

Value of x (inside function) = 11

Value of y (inside function) = 21

Values after function call

Final value of x = 11

Final value of y = 21

Here we are passing addresses of integer variables to a function. So the formal arguments must be declared as a pointer to int or (int *). The expression (*x)++ means that first dereference the value at x then increment it. Similarly, (*y)++ means that first dereference the value at y then increment it. When the function try_to_change() ends, the control passes back to main() and printf() statements in line 17 and 18 prints the new value of x and y respectively.


Difference between call by value and call by reference in c

No.Call by valueCall by reference
1A copy of value is passed to the functionAn address of value is passed to the function
2Changes made inside the function is not reflected on other functionsChanges made inside the function is reflected outside the function also
3Actual and formal arguments will be created in different memory locationActual and formal arguments will be created in same memory location



Instagram