What is call by reference in C language?

CServer Side ProgrammingProgramming

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
raja
Published on 08-Mar-2021 10:10:58
Advertisements