Tuesday, January 17, 2012

the Swapping of two Values using Functions


#include<stdio.h>
#include<conio.h>
int swapval(int,int);
int swapref(int*,int*);
int a,b;
void main()
{
clrscr();
printf(“enter the two values\n”);
scanf(%d%d”,&a,&b);
printf(“pass by value\n”);
printf(“before function call a=%d b=%d “,a,b);
swapval(a,b);
printf(“after function swapval a=%d b=%d “,a,b);
printf(“pass by reference\n”);
printf(“before function call a=%d b=%d “,a,b);
swapref(&a,&b);
printf(“after function swapref a=%d b=%d “,a,b);
getch();
}
swapval(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf(“\nwith swap val x=%d y=%d”,x,y);
}
swapref(int*x,int*y)
{
int *t;
*t=*x;
*x=*y;
*y=*t;
printf(“\nwith swapref x=%d y=%d “,*x,*y);
}

Output:
give two numbers 
5
6
pass by value
 before function call a=5 b=6
with swapval x=6 y=5
after function swapval a=5 b=6
pass by reference
before function call a=5 b=6
with swapref x=6 y=5l
after function swapref a=6 b=5 

No comments:

Post a Comment