Raj is an ardent coder who loves exploring new technology. He is an IT pro with 9 years of exp in C#, Angular, React, Vue.
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
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”.
© 2012 RAJKISHOR SAHU
Safwan on February 28, 2018:
dipali on June 01, 2015:
thank its really very very helpful
vickey on July 22, 2014:
thanks sir it's really very helpful
harshita on January 15, 2014:
Really its very useful to me
priyanka sharma on October 08, 2013:
sunil on March 11, 2013:
really helpfull ......
kevi on March 09, 2013:
thnks very much
Ragini on January 14, 2013:
Raheel on January 08, 2013:
Thanx sir, it helped a lot
Pankaj on December 30, 2012:
You are great.......
firstname.lastname@example.org on July 27, 2012:
Many Many thnx sir............
navi on March 15, 2012:
helpful, thanks sir
sudheer on February 22, 2012:
sandeep on February 18, 2012:
C made simple