14. Write a program to differentiate call by value and call by reference.
#include <stdio.h>
#include <conio.h>
void swapref(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void swapval(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void main()
{
int a = 5, b = 2;
clrscr();
printf("Before calling swapval function a=%d,b=%d", a, b);
swapval(a, b);
printf("\nAfter calling swapval function a=%d, b=%d", a, b);
printf("\nBefore calling swapref function a=%d,b=%d", a, b);
swapref(&a, &b);
printf("\nAfter calling swapref function a=%d, b=%d", a, b);
}
Output
Before calling swapval function a=5,b=2
After calling swapval function a=5, b=2
Before calling swapref function a=5,b=2
After calling swapref function a=2, b=5