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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago