Write a C program to add two numbers using Call by value and Call by reference parameter passing techniques.

CALL BY VALUE: PROGRAM

#include<stdio.h>
main()
{
int a,b,sum=0;
clrscr();
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
printf("a:%d, b:%d, sum:%d\n",a,b,sum);
sum=add(a,b);
printf("a:%d, b:%d,\nsum:%d\n",a,b,sum);
getch();
}
int add( int num1,int num2)
{
int sum=0;
printf("num1:%d, num2:%d, sum:%d\n",num1,num2,sum);
sum=num1+num2;
printf("num1:%d, num2:%d, sum:%d\n",num1,num2,sum);
return(sum);
}

 

Output

Output:
Enter the values of a and b
5
6
a:5, b:6, sum:0
num1:5, num2:6, sum:0
num1:5, num2:6, sum:11
a:5, b:6,
sum:11
Enter the values of a and b
15
30
a:15, b:30, sum:0
num1:15, num2:30, sum:0
num1:15, num2:30, sum:45
a:15, b:30,
sum:45

 

CALL BY REFERENCE
PROGRAM:

 

#include<stdio.h>
void addnum(int *pa,int *pb,int *psum);
main()
{
int a,b,sum=0;
clrscr();
printf("Enter the values of a and b\n");
scanf("%d%d",&a,&b);
printf("a:%d\u, b:%d, sum:%d\n",a,b,sum);
printf("pa:%u pb:%u psum:%u\n",&a,&b,&sum);
addnum(&a,&b,&sum);
printf("a:%d, b:%d, sum:%d\n",a,b,sum);
getch();
}
void addnum(int *pa,int *pb,int *psum)
{
printf("add the pointer variables\n");
printf("pa:%u pb:%u psum:%u\n",*pa,*pb,*psum);
printf("values in pointer variables are\n");
printf("pa:%u pb:%u psum:%u\n",*pa,*pb,*psum);
*psum=*pa+*pb;
}

 

 

OUTPUT:
Enter the values of a and b
10
14
a:10u, b:14, sum:0
pa:65494 pb:65496 psum:65498
add the pointer variables
pa:10 pb:14 psum:0
values in pointer variables are
pa:10 pb:14 psum:0
a:10, b:14, sum:24
Enter the values of a and b
45
15
a:45u, b:15, sum:0
pa:65494 pb:65496 psum:65498
add the pointer variables
pa:45 pb:15 psum:0
values in pointer variables are
pa:45 pb:15 psum:0
a:45, b:15, sum:60