c program to swap two numbers using call by reference
>> Tuesday, November 1, 2011
This is another program to swap two number using call by reference method. In call by reference the addresses of actual
arguments in the calling function are copied into formal arguments of the called function. This means that using these addresses we
would have an access to the actual arguments and hence we would be able to manipulate them.
#include<stdio.h>
#include<conio.h>
void swap( int *x, int *y ) // call by refrence
{
int t ;
t = *x ;
*x = *y ;
*y = t ;
printf( "\nx = %d y = %d", *x,*y);
}
int main( )
{
int a = 10, b = 20 ;
swap ( &a, &b ) ; // passing the address of values to be swapped
printf ( "\na = %d b = %d", a, b ) ;
getch();
}
output :
Note that this program manages to exchange the values of a and b
using their addresses stored in x and y.
Lets have a look on how call by value is done. Here is the program to swap two numbers using call by value :
Call by value
0 comments:
Post a Comment