Header Ads Widget

Ticker

6/recent/ticker-posts

functions program in c :- 6

 Program 6: Swap the two numbers using call by value. Swap function with argument list but no return type.


#include< stdio.h >

#include< conio.h >

void swap(int x, int y)

{

int t;

t=x;

x=y;

y=t;

}

void main()

{

int a, b;

printf(“\n Enter the first number a=”);

scanf(“%d”, &a);

printf(“\n Enter the second number b=”);

scanf(“%d ”,&b);

swap(a , b); //actual swapping is not done in call by value

printf(“\n After swapping value of a = %d”, a);

printf(“\n After swapping value of b = %d ”, b);

getch();

}


Output:

Enter the first number a= 10

Enter the second number b= 20

After swapping value of a = 10

After swapping value of b = 20


Explanation:

In call by value when a and b argument is passed to function swap, that values are copied to x and y. When we made changes on x and y that is only visible into swap function and not reflected back into a and b in main function. Old values that is a and b remains same only x and y values get swapped. So output after swapping remains same in the main function.

Post a Comment

0 Comments