Differentiate between call by value and
call by reference concept by giving c program.
#include<stdio.h>
#include<conio.h>
void
swap1(int,int);
int
swap2(int*,int*);
void
main()
{
int
n1,n2;
clrscr();
printf("\n
Enter any two values for swap--");
scanf("%d%d",&n1,&n2);
printf("\n
Before swap the value-\n\n");
printf("N1=%d ,N2=%d",n1,n2);
//Call
by value
swap1(n1,n2);
swap2(&n1,&n2);
printf("\n
After swap the value by reference-\n\n");
printf("N1=%d ,N2=%d",n1,n2);
getch();
}
void
swap1(int a,int b)
{
a=a+b;
b=a-b;
a=a-b;
printf("\n
After swap the value by using call by value-\n\n");
printf("N1=%d ,N2=%d",a,b);
}
int
swap2(int *a,int *b)
{
int
temp;
temp=*a;
*a=*b;
*b=temp;
return
0;
}
output:
Enter
any two values for swap--45 10
Before
swap the value-
N1=45 ,N2=10
After swap the value by using call by value-
N1=10 ,N2=45
After swap the value by reference-N1=10 ,N2=45
No comments:
Post a Comment