Ex.No:6b Swapping Two Numbers using call by value
AIM:
To write a C program to swap two numbers using call by value.
ALGORITHM:
Step 1: Start the program.
Step 2: Set a ← 10 and b ← 20
Step 3: Call the function swap(a,b)
Step 3a: Start function.
Step 3b: Assign t ← x
Step 3c: Assign x ← y
Step 3d: Assign y ← t
Step 3e: Print x and y.
Step 3f: End function.
Step 4: Stop the program.
PROGRAM CODE:
#include<stdio.h> #include<conio.h> void swap(int , int); // Declaration of function void main( ) { int a = 10, b = 20 ; clrscr(); printf("n Before swapping"); printf ( "n a = %d b = %d", a, b ) ; swap(a,b);// call by value : a and b are actual parameters getch(); } void swap( int x, int y ) // x and y are formal parameters { int t ; t = x ; x = y ; y = t ; printf("n After swapping"); printf ( "n a = %d b = %d", x, y ) ; }
OUTPUT:
RESULT:
Thus the C program to swap two numbers using call by value is written and executed successfully.