c program to swap two numbers using call by value
>> Tuesday, November 1, 2011
This program demonstrates how the two number are swapped. This swapping of variables are done in function 'swap' which we have defined. It also clears the concept, that how "call by value" is implemented. Lets see how this 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 ) ;
}
Output :Note that values of a and b remain unchanged even after
exchanging the values of x and y.
Lets have a look on second method of swapping the number using call by reference :
here is the link provided for :
Call by refrence
0 comments:
Post a Comment