0% found this document useful (0 votes)
12 views2 pages

Ex No 9 Call by Value and Call by Address

Uploaded by

Amrita Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Ex No 9 Call by Value and Call by Address

Uploaded by

Amrita Rajesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Ex.

No 9 Call by Value and Call by Address


Aim: To swap two numbers using call by value and call by address
Algorithm(Call by Value)
Step 1: Start the program
Step 2: Declare the variable a,b
Step 3: Declare a function swap which accept two integer as parameter.
Step 3: Read the value of a and b
Step 4: call the function swap() and pass the value of a, b as argument.
Step 5: Print the swapped value of a and b inside main block
Step 6: Stop
Function swap(x,y)
Step 1: Declare the variable t
Step 2: Assign t=x, x=y, y=t
Step 3: Print the value of x, y
Program: Call by Value
#include<stdio.h>
void main()
{
int a,b;
void swap(int,int);//function declaration
printf("Enter the value of a & b:");
scanf("%d%d",&a,&b);
printf("Before Swapping\n");
printf("a=%d\nb=%d",a,b);
swap(a,b);
printf("\nAfter Swapping in main function\n");
printf("a=%d\nb=%d",a,b);
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf("\nAfter Swapping in swap function\n");
printf("a=%d\nb=%d",x,y);
}
Output:
Enter the value of a & b:5 10
Before Swapping
a=5
b=10
After Swapping in swap function
a=10
b=5
After Swapping in main function
a=5
b=10
Algorithm(Call by Address)
Step 1: Start the program
Step 2: Declare the variable a,b
Step 3: Declare a function swap which accept two pointer as parameter.
Step 3: Read the value of a and b
Step 4: call the function swap() and pass the address of a, b as argument.
Step 5: Print the swapped value of a and b inside main block
Step 6: Stop
Function swap(*x,*y)
Step 1: Declare the variable t
Step 2: Assign t=*x, x=*y, y=*t
Step 3: Print the value of x, y
Program: Call by Address
#include<stdio.h>
void main()
{
int a,b;
void swap(int*,int*);//function declaration
printf("Enter the value of a & b:");
scanf("%d%d",&a,&b);
printf("Before Swapping\n");
printf("a=%d\nb=%d",a,b);
swap(&a,&b);
printf("\nAfter Swapping in main function\n");
printf("a=%d\nb=%d",a,b);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
printf("\nAfter Swapping in swap function\n");
printf("a=%d\nb=%d",*x,*y);
}
Output:
Enter the value of a & b:5 10
Before Swapping
a=5
b=10
After Swapping in swap function
a=10
b=5
After Swapping in main function
a=10
b=5

Result: Thus the numbers were swapped using call by value and call by address and its output was
verified.

You might also like