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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

5 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

5 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

11 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

11 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

11 months ago

Efficient Order Log Storage

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

12 months ago