ProAnswers.org

Write a C program to find out the Swapping of two Values using Functions ?

#include

#include 

int swapval(int,int); 

int swapref(int*,int*); 

int a,b; 

void main() 

{ 

clrscr(); 

printf(“enter the two values

”);

scanf(“%d%d”,&a,&b); 

printf(“pass by value

”);

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

”);

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(“

with swap val x=%d y=%d”,x,y);

} 

swapref(int*x,int*y) 

{ 

int *t; 

*t=*x; 

*x=*y; 

*y=*t; 

printf(“

with 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