Call by Value & Call by Reference in C

Call by Value or Call by reference is an important concept which is to be discussed in context to the function. Before discussing call by value and call by reference let us go through an example and understand the basic structure of function and formal and actual arguments.

Function: A function is a group of programs or a smallest unit of a large piece of program which is self sufficient to perform any specific task. A function is also known as procedure or sub-routine in different programming context.

Function is associated with the 3 building blocks to perform its task.

  1. Function declaration
  2. Function calling
  3. Function definition

Actual Parameter: Actual parameters are the values or arguments which are passed in function calling.

Formal Parameter: Formal parameters are the values or arguments which are used in function definition.

Example:

// program to demonstrate a simple example of a function
#include<stdio.h>
#include<conio.h>
void sum(int a,int b);// function declaration
void main()
 {
 int n1,n2;
 clrscr();
 printf("\nEnter 1st number");
 scanf("%d",&n1);
 printf("\nEnter 2nd number");
 scanf("%d",&n2);
 sum(n1,n2); /* function calling
       where n1 and n2 are known as actual arguments or parameters*/

 getch();
  }

 void sum(int a, int b) /* fucntion defintion
       where a and b are known as formal arguments or parameters*/
  {
  int sum;
  sum=a+b;
  printf("\nSum=%d",sum);
  return (0);
  }

There are two ways to pass value into a function. They are:

  1. Call by Value
  2. Call by Reference

1. Call by Value

  • Using Call by Value approach in function, the value of the actual parameters are copied into the formal parameters of a function.
  • In Call by Value method one cannot modify the value of the actual parameters with the help of formal parameters.
  • In Call by Value technique compiler holds a separate memory for the actual and formal parameters.

Let us understand Call by Value through an example.

// program to implement call by value
#include<stdio.h>
#include<conio.h>
void student(int rollno);   // function declaration
void main()
 {
 int rollno=101;
 clrscr();
 printf("\nBefore function call Rollno=%d",rollno);
 student(rollno);  // actual argument
 printf("\n\nAfter function call Rollno=%d",rollno);
 getch();
 }

 void student(int r) // formal argument
   {
   printf("\n\nWelcome in callee function");
   printf("\nBefore changing Rollno=%d",r);
   r=r+2;
   printf("\nAfter changing Rollno=%d",r);
    }

Explanation: In call by Value method, value of actual parameter i.e Rollno=101 is same as in the formal parameter i.e. Rollno=101 in the callee function and thus it keeps a separate memory location for both values.

2. Call by Reference

  • In Call by Reference the address of the variable is passed to the callee function as the actual argument.
  • In Call by Reference the value of the actual parameter is changed when we made change to the formal arguments of the callee function because address of the actual parameter is passed.
  • In call by reference formal and actual parameters shares the same memory space. It means the actual value and the modified value share same address space.

Let us understand Call by Reference through an example.

// program to implement call by value
#include<stdio.h>
#include<conio.h>
void student(int *rollno);   // function declaration
void main()
 {
 int rollno=101;
 clrscr();
 printf("\nBefore function call Rollno=%d",rollno);
 student(&rollno);  // address of actual argument is passed
 printf("\n\nAfter function call Rollno=%d",rollno);
 getch();
 }

 void student(int *r) /* address of actual argument will be stored in
			 pointer variable r as formal argument */
   {
   printf("\n\nWelcome in callee function");
   printf("\nBefore changing Rollno=%d",*r);
   *r=*r+2;
   printf("\nAfter changing Rollno=%d",*r);
    }

Explanation: In call by reference, the value of Rollno=101 is changed after the function call i.e Rollno=103. This is because both,the actual as well as formal parameter shares the same memory address unlike in call by value.