Call by value and Call by reference is the extended part of Functions. Let's see what's it? For this, you need to understand the concept of Formal Parameters and Actual Parameters. When the function is called, the parameters which we pass are known as actual parameters. And the parameters which get pass from the User-defined function are known as formal parameters.
To need understand the concept more clearly, let's understand how to functions a little bit deeper. Let's take an example of swapping the values of two variables with the help of User-defined Function. Also we need to include pointer in this example.
#include <stdio.h>
void swap(int *a, int *b);
int main()
{
int x, y;
printf("Enter the value of variable 1 = ");
scanf("%d", &x);
printf("Enter the value of variable 2 = ");
scanf("%d", &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int t;
t = *b;
*b = *a;
*a = t;
}
Explaination : In this program, we are swapping the values of variables using pointers and using User-defined Function. So, firstly we have declared the swap function. Then in main function, we have scanned 2 variables. And then we have passed the address of the variables x and y and not the values. So, this is called Call By Reference. Call by Value is simple, which we use. In the functions, if have used Call by Value, then we will pass the variables and not the address of the variable. In the User-defined functions, if the pointers variables are passed and simple variables are passed then in memory they have create their own space and after the function returns any value or the functions get terminate the variables get destroy but here, the pointers are passed so, the operations are done on the variable's address' value and not on the variable directly. The above program is working under Call by Reference method.