C program to implement "Call By Value"
C program for "Call By Value"
The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.
By default, C programming uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.
To understand, it observe the code:
#include <stdio.h>
#include <conio.h>
void swapval(int,int); //Declariation of the swap function
int main()
{
int a,b;
clrscr();
//Get the values from the user of A & B for operation
printf("Swaping value using Call By Value:\n");
printf("Enter the first value A: ");
scanf("%d",&a);
printf("Enter the second value B: ");
scanf("%d",&b);
//Print the value inputed by the user
printf("\nValue inside main function:\n");
printf("A=%d B=%d",a,b);
swapval(a,b); //Call the swap function
getch();
}
void swapval(int x,int y)
{
int temp; //Define a temp variable for temporary storage of value
//Swap the values of x & y
temp=x;
x=y;
y=temp;
//Print the swaped value
printf("\nValue inside swap function:\n");
printf("A= %d B=%d",x,y);
}
Happy Coding :)
Comments
Post a Comment