View All C Programs

 Previews Example  View All C Programs Next Example 

Program to swap using call by value in C

This program to swap using call by value. This swapping of variables are done in function 'swap' which we have defined. Swapping c program clears the concept, that how "call by value" and "call by reference" is implemented.
#include <stdio.h>
#include <conio.h>
 int swap(int , int);    // Declaration of function

main( )
{
    int a = 10, b = 20 ;  // call by value
    swap(a,b);            // a and b are actual parameters
    printf ( "\na = %d b = %d", a, b ) ;
    getch();
}

int swap( int x, int y )    // x and y are formal parameters
{ 
    int t ;
    t = x ;
    x = y ;
    y = t ;
    printf ( "\nx = %d y = %d", x, y ) ;
}   
Result
x = 10 y = 20

a = 20 b = 10