17.Example of call by value & call by reference
#include<stdio.h>
void swap(int a,int b);
void swap1(int *a,int *b);
int main(){
int a,b;
printf("Enter the value of a and b :");
scanf("%d %d",&a,&b);
swap(a,b);
printf("\nThe value of a and b after swap(wrong method) is %d and %d",a,b);
swap1(&a,&b);
printf("\nThe value of a and b after swap is %d and %d",a,b);
return 0;
}
void swap(int x,int y)//call by value
{
int temp;//do not swap
temp=x;
x=y;
y=x;
}
void swap1(int *x,int *y)//call by reference
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
Comments
Post a Comment