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 :

 C program to swap two numbers using call by reference 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

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save