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 :


C program to swap numbers, call by value




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

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