Program to implement call by value and call by reference
24a) Call by Value
Source Code:
#include<stdio.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
int main()
{
int a,b;
a=50;
b=60;
printf("Before Swapping:\na=%d\nb=%d\n",a,b);
swap(a,b);
printf("After Swapping:\na=%d\nb=%d\n",a,b);
}
Output:
Before Swapping:
a=50
b=60
After Swapping:
a=50
b=60
a=50
b=60
After Swapping:
a=50
b=60
24b) Call by Reference
Source Code:
#include<stdio.h>
void swap(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
int main()
{
int a,b;
a=50;
b=60;
printf("Before Swapping:\na=%d\nb=%d\n",a,b);
swap(&a,&b);
printf("After Swapping:\na=%d\nb=%d\n",a,b);
}
Output:
Before Swapping:
a=50
b=60
After Swapping:
a=60
b=50
a=50
b=60
After Swapping:
a=60
b=50
No comments:
Post a Comment