Programs to show "Call by Value",

Call by value
/*Program to show Call by value*/
#include <stdio.h>
#include <conio.h>
void swap(int x, int y)
{
 int z;
 z = x;
 x = y;
 y = z;
 printf("\n Swapped values are a = %d and b = %d", x, y);
}

int main (int,int)
{
 int a = 7, b = 4;
 printf("Original values are a = %d and b = %d", a, b);
 swap(a, b);
 printf("\n The values after swap are a = %d and b = %d", a, b);

}

Expected Output:
Original values are a = 10 and b = 20
Swapped values are a = 20 and b = 10
The values after swap are a = 10 and b = 20


/*Addition of two numbers using Call By Value*/
#include<stdio.h>
#include<conio.h>
int sum(int a, int b)
{
 int a,b,c;
 c=a+b;
 return c;
}

int main()
{
int x =10;
int y = 20;
int z = sum(x, y);
printf("Sum of x and y is=%d", z);

return 0;
}
Expected Output:
Sum of x and y is=30



Post a Comment

Previous Post Next Post