What is call by reference in C language?


Pass by reference means addresses are sent as arguments.

The call by reference or pass by reference method passes the arguments to a function by the means of address to an argument. This is done into the formal parameter. Inside the function, the address is used to access an actual argument.

Example

#include<stdio.h>
void main(){
   void swap(int *,int *);
   int a,b;
   printf("enter 2 numbers");
   scanf("%d%d",&a,&b);
   printf("Before swapping a=%d b=%d",a,b);
   swap(&a, &b); //address are sent as an argument
   printf("after swapping a=%d, b=%d",a,b);
   getch();
}
void swap(int *a,int *b){
   int t;
   t=*a;
   *a=*b; // *a = (*a + *b) – (*b = * a);
   *b=t;
}

Output

When the above program is executed, it produces the following result −

enter 2 numbers 10 20
Before swapping a=10 b=20
After swapping a=20 b=10

Updated on: 08-Mar-2021

494 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements