Call by Value and Call by Reference in C Programming
By rajkishor09
Call by value and call by reference is the most confusing concept among new C language programmer. I also struggled with this, the reason may be my teacher is not explaining it in simple words or I was dumb. Whatever the reason is; here I will try to explain it in simple word. Let’s start with classic example, classic example means whenever people talk about call by value or call by reference, they cite this example i.e. swapping of two variable.
Call by Value and Call by Reference
#include "stdio.h"
#include "conio.h"
void callByValue(int, int);
void callByReference(int *, int *);
int main()
{
int x=10, y=20;
clrscr();
printf("Value of x = %d and y = %d.\n", x,y);
callByValue(x, y);
printf("\nCall By Value function call...\n");
printf("Value of x = %d and y = %d.\n", x,y);
callByReference(&x, &y);
printf("\nCall By Reference function call...\n");
printf("Value of x = %d and y = %d.\n", x,y);
getch();
return 0;
}
void callByValue(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
}
void callByReference(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}In above call by value and call by reference C program example we have two functions; “callByValue()” and “callByReference()” to demonstrate respective logic.
Let’s start with “callByValue()” function, this function (line no. 25-31) does swapping of variable using temp variable, we have to pass two integer type data as arguments to this function.
The “callByReference()” function (line no. 33-39) also does swapping of variable using temp variable, but we have to pass two integer type pointer as arguments of this function. To reduce confusion; I kept variable name and logic same in both function except some extra asterisk (*) in “callByReference()” function.
We have declared two integer variable in line no. 9. We will pass those two variables to both function and will try to swap value stored in that. First we are calling function “callByValue()” (line no. 13) and we are passing those two integer value. In line no. 15 we are displaying the value of variables (x and y) after swapping. I know that swapping didn’t happen when we called “callByValue()” function.
In line no. 17 we are calling function “callByReference ()” and here we are passing address ofthose two integer variable. In line no. 19 we are displaying the value of variables (x and y). As you can see value of variables (x and y) is successfully swapped.
You must be thinking why this swapping logic worked for “callByReference()” function but not for “callByValue()” function.
Why swapping logic didn’t work for “Call by Value”?
When we called function “callByValue()” in line no. 13 that time we passed value of “x” and “y”, which means we passed copy of “x” and “y” variable value not the actual “x” and “y”. Now you understood why this method of calling function is called “Call by value”? Let me tell you one interesting point about this function, when you will print value of “x” and “y” variable inside this function it will show you the swapped value. But as I told you this logic works on copy of value so those changes will not reflect in “main()” function. See below example and output.
But in case of “callByReference()” function we passed address of “x” and “y” variables (see line no. 17 carefully there we are passing ampersand ‘&’ along with variable name), which means actual location of “x” and “y” variables. So in this function we swapped value which was stored in “x” and “y” location rather than copy of value. As this logic works on address (reference) of variable so this logic is known as “Call by Reference”.
#include "stdio.h"
#include "conio.h"
void callByValue(int, int);
void callByReference(int *, int *);
int main()
{
int x=10, y=20;
clrscr();
printf("Value of x = %d and y = %d.\n", x,y);
printf("\nCall By Value function call...\n");
callByValue(x, y);
printf("Value of x = %d and y = %d.\n", x,y);
printf("\nCall By Reference function call...\n");
callByReference(&x, &y);
printf("Value of x = %d and y = %d.\n", x,y);
getch();
return 0;
}
void callByValue(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
printf("Value of x = %d and y = %d inside callByValue function.\n", x,y);
}
void callByReference(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}




sandeep 3 months ago
C made simple