C Tutorial – Call by Value or Call by Reference
In this C language tutorial we will take a look at call by value and call by reference (also known as pass-by-value and pass-by-reference). These methods are different ways of passing (or calling) data to functions.
Call by Value
If data is passed by value, the data is copied from the variable used in for example main() to a variable used by the function. So if the data passed (that is stored in the function variable) is modified inside the function, the value is only changed in the variable used inside the function. Let’s take a look at a call by value example:
#include <stdio.h>
void call_by_value(int x) {
printf("Inside call_by_value x = %d before adding 10.\n", x);
x += 10;
printf("Inside call_by_value x = %d after adding 10.\n", x);
}
int main() {
int a=10;
printf("a = %d before function call_by_value.\n", a);
call_by_value(a);
printf("a = %d after function call_by_value.\n", a);
return 0;
}
The output of this call by value code example will look like this:
a = 10 before function call_by_value.
Inside call_by_value x = 10 before adding 10.
Inside call_by_value x = 20 after adding 10.
a = 10 after function call_by_value.
Ok, let’s take a look at what is happening in this call-by-value source code example. In the main() we create a integer that has the value of 10. We print some information at every stage, beginning by printing our variable a. Then function call_by_value is called and we input the variable a. This variable (a) is then copied to the function variable x. In the function we add 10 to x (and also call some print statements). Then when the next statement is called in main() the value of variable a is printed. We can see that the value of variable a isn’t changed by the call of the function call_by_value().
Call by Reference
If data is passed by reference, a pointer to the data is copied instead of the actual variable as is done in a call by value. Because a pointer is copied, if the value at that pointers address is changed in the function, the value is also changed in main(). Let’s take a look at a code example:
#include <stdio.h>
void call_by_reference(int *y) {
printf("Inside call_by_reference y = %d before adding 10.\n", *y);
(*y) += 10;
printf("Inside call_by_reference y = %d after adding 10.\n", *y);
}
int main() {
int b=10;
printf("b = %d before function call_by_reference.\n", b);
call_by_reference(&b);
printf("b = %d after function call_by_reference.\n", b);
return 0;
}
The output of this call by reference source code example will look like this:
b = 10 before function call_by_reference.
Inside call_by_reference y = 10 before adding 10.
Inside call_by_reference y = 20 after adding 10.
b = 20 after function call_by_reference.
Let’s explain what is happening in this source code example. We start with an integer b that has the value 10. The function call_by_reference() is called and the address of the variable b is passed to this function. Inside the function there is some before and after print statement done and there is 10 added to the value at the memory pointed by y. Therefore at the end of the function the value is 20. Then in main() we again print the variable b and as you can see the value is changed (as expected) to 20.
When to Use Call by Value and When to use Call by Reference?
One advantage of the call by reference method is that it is using pointers, so there is no doubling of the memory used by the variables (as with the copy of the call by value method). This is of course great, lowering the memory footprint is always a good thing. So why don’t we just make all the parameters call by reference?
There are two reasons why this is not a good idea and that you (the programmer) need to choose between call by value and call by reference. The reason are: side effects and privacy. Unwanted side effects are usually caused by inadvertently changes that are made to a call by reference parameter. Also in most cases you want the data to be private and that someone calling a function only be able to change if you want it. So it is better to use a call by value by default and only use call by reference if data changes are expected.
That is all for this C tutorial, where you (hopefully) have learned the difference between call-by-value and call-by-reference.
wow nice post.
Okay, but what if i use “a=call_by_value(a)” in main and “return x” in last line of call_by_vale function?
[…] C Tutorial – Call by Value or Call by Reference […]
thank u!!!!!!!!!!! its really simple
Perfect yaaaarrr…
A very good tutorial.. Simple examples to demonstrate different concepts.
Thumbs up.
good details in website…very help full..website…nice details
thanks sir for this tutorial, it is very helpful in completing my assignment.
awesome!!!!
simple to understand the full concept…..
thank u……
Great job.
Good example with explanation. Really nice.If its any example for array pointer in call by reference
thanks
really gud one…:)
thnx..
thank you
Thanks! Amazing 😀
It was really helpful
awesome explanation ……….
thanks…it s good tutorial…
nice explanation..and very usefull…
its realy good.now i was called to interview
thankx
THANKX……… ITS GOOD TUTORIAL………….
Thanks
it was really very helpful
Thank you so………. much this Tutorial is very helpful…!
simple and very good tutorial and thank you very much. It was really helpful
Thanks. It is helpful for the new student.
Very well detailed.Keep it up.
thanxs it help me a lots of plz post this kind of tutorials a lot on the web
very nice tutorial… it really cleared all my doubts regarding the above topic… the examples used were very helpful… great job!!
so helpful
Thanks a lot….it was really helpfull for me…great job…
it is useful for us
Really nice tutorial….. very helpful and nicely explained….
this is very good tutorial for c language thank u
it is really good … it helpped me a lot during exam time……
way in which examples are explained is very helpful for me.
thank you.
so helpful to understand
Thanks! Helpful it is. 🙂
this tutorial was quite helpful, at least it helped me to clear my 50%-60% confusions regarding call by value and call by reference. thank you
Nice Tutorial….Helpful…
Its very easy to understand…
Nice explanation….
very very thnxxx
nice tutorial
Simply thanx, helped a lot
thanx..thats really nice
very nice…
Thanks……..
its very useful & clear…thanks
tanx 4 d tutorial . . .
simple and essesntial xplanation . . . . and useful 🙂
thanks a lot sir….
Great.Explanation.Thanks a lot sir.
Thanks a lot. Useful for my exams.
thanks alot that was great