Categories: C

Call by Value & Call by Reference

There are two ways to pass the data into the function in C language:

  • method :call by value
  • method :call by reference.
Call by value in C
  • In this method, we pass the values of variables (the value of the actual parameters) into the formal parameters.
  • Here, the formal parameter cannot modify the value of the actual parameter.
  • The formal parameter gets a copy of the value of the actual parameter, therefore the memory allocation is different for actual and formal parameters.
  • The actual parameter is the argument that is used in the function call, whereas the formal parameter is the argument that is used in the function definition.
Example: Swapping values on integers
#include <stdio.h>  
void swap(int , int); //prototype of the function   
int main()  
{  
    int a = 10;  
    int b = 20;   
    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  
    swap(a,b);  
    printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20  
}  
void swap (int a, int b)  
{  
    int temp;   
    temp = a;  
    a=b;  
    b=temp;  
    printf("After swapping values in function a = %d, b = %d\n",a,b); // Formal parameters, a = 20, b = 10   
}  
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20

Here, the value of a and b remain unchanged in the main function, but that of function swap() is getting exchanged.

Call by reference in C
  • In call by reference, the addresses of actual arguments in the calling function are copied into the formal arguments of the called function
  • Using this method, we have access to the actual arguments and hence we can be able to manipulate them.
  • The memory allocation is the same for both formal parameters and actual parameters.
Example: Swapping integer values
#include <stdio.h>  
void swap(int *, int *); //prototype of the function   
int main()  
{  
    int a = 10;  
    int b = 20;   
    printf("Before swapping the values in main a = %d, b = %d\n",a,b); // printing the value of a and b in main  
    swap(&a,&b);  
    printf("After swapping values in main a = %d, b = %d\n",a,b); // The value of actual parameters do not change by changing the formal parameters in call by value, a = 10, b = 20  
}  
void swap (int *a, int *b)  
{  
    int temp;   
    temp = *a;  
    *a= *b;  
    *b=temp;  
   
}  
Output
Before swapping the values in main a = 10, b = 20
After swapping values in main a = 20, b = 10

The above code manages to exchange the values of actual parameters.

Note: also read about the Passing arguments between functions in C & Functions in C

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

2 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

1 month ago