In this program, you will learn how to swap two numbers using call by reference. The Call by Reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
#include <stdio.h>
void swap(int *x, int *y);
void main()
{
int a, b;
printf("Enter two numbers a & b: ");
scanf("%d%d", &a, &b);
printf("\nBefore swapping\n a=%d and b=%d", a, b);
swap(&a, &b);
printf("\n\nAfter swapping\n a=%d and b=%d", a, b);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Enter two numbers a & b: 74 23
Before swapping
a=74 and b=23
After swapping
a=23 and b=74