#include <stdio.h> swap (int *, int *); int main() { int a, b; printf("\nEnter value of a & b: "); scanf("%d %d", &a, &b); printf("\nBefore Swapping:\n"); printf("\na = %d\n\nb = %d\n", a, b); swap(&a, &b); printf("\nAfter Swapping:\n"); printf("\na = %d\n\nb = %d", a, b); return 0; } swap (int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; }
OUTPUT:
Enter value of a & b: 10 20 Before Swapping: a = 10 b = 20 After Swapping: a = 20 b = 10
The same program can be implemented using call by value, but there will be no change in the output. But in this program the values of a and b will successfully swapped. Refer an another example to implement the call by reference in Functions.