C Program to Swap Two Variables Using Call by Value

In this program, you will learn how to swap two numbers using Call by Value. The Call by Value method of passing arguments to a function copies the values of an argument into the formal parameter. Inside the function, the value is used to access the actual argument used in the call. It means the changes made to the parameter does not affect the passed argument.



Source Code
#include<stdio.h>

void swap(int, int);
int main()
{
    int a, b;
    printf("\nEnter the values of a b:");
    scanf("%d %d", &a, &b);
    printf("\nBefore swapping:");
    printf("\na=%d b=%d", a, b);
    swap(a, b);
    return 0;
}
void swap(int x, int y)
{
    int temp;
    temp = x;
    x = y;
    y = temp;
    printf("\n\nAfter swapping:");
    printf("\na=%d b=%d", x, y);
}
Output
Enter the values of a b:
58
29

Before swapping:
a=58 b=29

After swapping:
a=29 b=58