Call by value and call by reference in c

 Where do these terms call by value and call by reference are used?

Call by value and call by reference is used in function calls.

 1. Call by value

In the call by value, while calling a function, the value of the variable is passed through the function.

Example:

Swapping  of two numbers using call by value.

#include<stdio.h>

void swap(int x,int y);  //function declaration
void main()
 {
  int a,b;
  printf("Enter the value of a and b:");
  scanf("%d %d",&a,&b);
  swap(a,b); //function calling
  printf("a=%d b=%d",a,b);
  return 0;
  }
 void swap(int x,int y) //function defination
{
 int temp;
 temp=x;
 x=y;
 y=temp;
 printf(“\n values of x and y after swapping);
 printf("x=%d y=%d \n",x,y);
}




 output::

call-by-value-c

the value of a is declared as 20 and b as 50.

call-by-value-c


values variables of a and b are passed through function calling and these values are stored in x and y.

x is 20 and y is 50.

Due to the swapping algorithm values under x and y are swapped with each other.

call-by-value-c

And now the values of x are 50 and y is 20.


  •  But by swapping the values of x and y, the values associated with the main variables a and b do not get swapped.
  •  By passing, the values of a and b to x and y with the help of a function call does not make any change to the values of a and b.
  • Because x and y are different variables, they have different address and address of a and b is different.
  • Separate memory is allocated to x and y. whatever changes are done in x and y are limited under the swap function.
  • If we want to swap the main values a and b then, we have to pass the address of a and b by function calls.
  • This is possible only because of pointers. Passing the address of the main variable by a function call is called a call by reference. 

2. call by reference.

  •  If you have read the call by the value, it will be easier for you to understand the call by reference in the previous program of the call by value.
  • The main values a and b are not swapped. To swap the main values a and b, call by reference is used.
  • To understand call by reference, we should have basic knowledge of pointers. As pointers stores the address of another variable.
  • Instead of passing the variable by a function call, we pass the address of the variable, and this address is stored under the pointer variable.
  • As the pointer variable contains the address of the main variable, the pointer variable points to the value of the main variable.
  • Pointer variable *x and pointer variable *y directly points to the value of a and b. swapping pointer variable will swap the values of a and b.

 

swapping of two numbers using call by reference 

#include<stdio.h>

void swap(int*x,int*y);  //function declaration
void main()
 {
  int a,b;
 
  printf("Enter the value of a and b:");
  scanf("%d %d",&a,&b);
  swap(&a,&b); //function calling
  printf(“\n values of a and b after swapping using pointer variables);
  printf("a=%d b=%d",a,b);
  return 0;
  }
 void swap(int*x,int*y) //function defination
{
 int *temp; // temporary pointer variable for swapping
 *temp=*x;
 *x=*y;
 *y=*temp;
  printf(“\n values of *x and *y after swapping);
 printf("x=%d y=%d \n",*x,*y);
}

 output::

call-by-reference-c

 

call-by-reference-c


 

 

see also:: 

function in c

pointers in c

pointer to pointer in c