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.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 101 responses to “C Tutorial – Call by Value or Call by Reference”

Why not let us know what you think by adding your own comment!

  1. Shaan Royal on October 24th, 2012:

    good tutorial . . :)

  2. Sadat on October 25th, 2012:

    a nice tutorial, thanks.

  3. vivek sharma on October 28th, 2012:

    very impressive..

  4. vinay on November 8th, 2012:

    a very good tutorial…thanks

  5. mijas on November 13th, 2012:

    thnks for the tutorial

  6. Hemjyoti M on November 16th, 2012:

    this is really good. it helped me a lot. thank you very much.

  7. Darshan on November 18th, 2012:

    really cool explained sufficiently its enough for beginer bt need more for intermediate

  8. satya on November 19th, 2012:

    good

  9. preet on November 20th, 2012:

    thanks a lottttt

  10. Fahad on December 3rd, 2012:

    I have really been helped, calling has been puzzling me 4 so long but now, the puzzling is all gone.

    thanks

  11. Rahul on December 10th, 2012:

    Thanks a lot. it helped me

  12. pranay on December 12th, 2012:

    Good tutorial

  13. Mussawir Brohi on December 12th, 2012:

    this was very helpfull, i got my assignment done, thanks alot ;)

  14. srikanth on December 13th, 2012:

    i hav this doubt for long time….
    now its cleared

  15. Satya Deo on December 21st, 2012:

    Thanks ……………..

  16. bharath kumar goud on December 27th, 2012:

    its very useful……. very nice.. thankingh u soo much sir,who published this page … thank you so much

  17. sukhi on December 27th, 2012:

    thanx………

  18. nithin on December 29th, 2012:

    very helpfull…

  19. sumathi on January 10th, 2013:

    It’s easy to understand. Thank you !

  20. pinku on January 14th, 2013:

    thangs

  21. amier on January 19th, 2013:

    very easy to understand explanation. thank you

  22. bhqavna on January 31st, 2013:

    nice

  23. prince on February 1st, 2013:

    nice….

  24. sree on February 1st, 2013:

    thanks a lot.

  25. Orkut Mahmud on February 5th, 2013:

    thnx

  26. roya on February 8th, 2013:

    wow!
    thank you

  27. pramod on February 11th, 2013:

    Thank you very much sir for sharing this concept

  28. suresh on February 12th, 2013:

    Thank you…………

  29. seema on February 25th, 2013:

    good example with explaination

  30. yuvraj on March 5th, 2013:

    thanks a lot:)

  31. Ananth on March 7th, 2013:

    Excellent :)

  32. Varun on March 7th, 2013:

    Thank you..

  33. veeraprasad.g on March 19th, 2013:

    thanks for the notes

  34. vinod gour on March 21st, 2013:

    its very good explanation

  35. nikhat azhar on March 21st, 2013:

    gre8….very useful,,,thanx

  36. shubham negi on March 21st, 2013:

    thnkxx alot

  37. shubham negi on March 21st, 2013:

    thank you

  38. vidhya on April 2nd, 2013:

    Thanks.But its not enough for an intermediate person.So,i expect more.

  39. DIVYANSHU on April 5th, 2013:

    thanx….

  40. Abhineet on April 14th, 2013:

    i dint get 10/10 in c programming internal… writing exactly point to point as given above ! :P
    i wonder what i should have added !!! :D

  41. satish gangawathi on April 23rd, 2013:

    so nsweet of uu

  42. sagar on April 26th, 2013:

    thanks……

  43. Shery pn April 29,2013 on April 29th, 2013:

    very clear with explanation..Excellent

  44. monisha on May 1st, 2013:

    clear cut explanations…..satisfied search..

  45. aadhira suresh on May 1st, 2013:

    thank u a lot… it was really helpful!!!!!!

  46. sagar on May 28th, 2013:

    up to the mark explaination ….
    very good.

  47. morientes on June 1st, 2013:

    its clear now thanks hey!!

  48. sanjay gupta on June 11th, 2013:

    ………..thanx alot for tutorial

  49. suresh on June 13th, 2013:

    i havebeen confusing wid this for the last 6 mnths..at last it gave me nice clarification…thanx for the awesome post..

  50. Gaurav on June 18th, 2013:

    This was very helpful for the preparation of my Assignment.. Thanks a lot..

  51. kaviya on June 20th, 2013:

    wow nice post.

  52. remo on June 27th, 2013:

    Okay, but what if i use “a=call_by_value(a)” in main and “return x” in last line of call_by_vale function?

  53. C Programming এর হাতে খড়ি পর্ব -৯ (Functions) | | বিডি পাঠশালা | on July 16th, 2013:

    [...] C Tutorial – Call by Value or Call by Reference [...]

  54. rajasekar on July 21st, 2013:

    thank u!!!!!!!!!!! its really simple

  55. swami on July 30th, 2013:

    Perfect yaaaarrr…

  56. Adil on August 3rd, 2013:

    A very good tutorial.. Simple examples to demonstrate different concepts.
    Thumbs up.

  57. parth on August 8th, 2013:

    good details in website…very help full..website…nice details

  58. deepak on August 12th, 2013:

    thanks sir for this tutorial, it is very helpful in completing my assignment.

  59. rama on August 18th, 2013:

    awesome!!!!
    simple to understand the full concept…..
    thank u……

  60. Oviya on September 13th, 2013:

    Great job.
    Good example with explanation. Really nice.If its any example for array pointer in call by reference

  61. Oviya on September 13th, 2013:

    thanks

  62. saurabh srivastav on September 13th, 2013:

    really gud one…:)
    thnx..

  63. abhishek on September 14th, 2013:

    thank you

  64. Rushikesh on September 15th, 2013:

    Thanks! Amazing :D

  65. Annu on September 18th, 2013:

    It was really helpful

  66. vijayaraj v.s on September 19th, 2013:

    awesome explanation ……….

  67. siddu on September 20th, 2013:

    thanks…it s good tutorial…
    nice explanation..and very usefull…

  68. ganesh on September 24th, 2013:

    its realy good.now i was called to interview
    thankx

  69. Kishan on October 4th, 2013:

    THANKX……… ITS GOOD TUTORIAL………….

  70. gadha on October 8th, 2013:

    Thanks
    it was really very helpful

  71. K.Revathy on October 9th, 2013:

    Thank you so………. much this Tutorial is very helpful…!

  72. suresh on October 19th, 2013:

    simple and very good tutorial and thank you very much. It was really helpful

  73. Bidyut on October 21st, 2013:

    Thanks. It is helpful for the new student.

  74. Sunil on October 22nd, 2013:

    Very well detailed.Keep it up.

  75. sujit kumar das on October 25th, 2013:

    thanxs it help me a lots of plz post this kind of tutorials a lot on the web

  76. ankur chavda on October 26th, 2013:

    very nice tutorial… it really cleared all my doubts regarding the above topic… the examples used were very helpful… great job!!

  77. subbu on November 12th, 2013:

    so helpful

  78. jaccob on November 20th, 2013:

    Thanks a lot….it was really helpfull for me…great job…

  79. vivek shinestar on November 25th, 2013:

    it is useful for us

  80. Siddharth Saxena on November 26th, 2013:

    Really nice tutorial….. very helpful and nicely explained….

  81. s karthik on December 4th, 2013:

    this is very good tutorial for c language thank u

  82. shrinidhi s uppoor on December 12th, 2013:

    it is really good … it helpped me a lot during exam time……

  83. Himanshu Mishra on December 14th, 2013:

    way in which examples are explained is very helpful for me.
    thank you.

  84. KIRUTHIKA on December 16th, 2013:

    so helpful to understand

  85. optimus on December 20th, 2013:

    Thanks! Helpful it is. :)

  86. sidhant on December 21st, 2013:

    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

  87. HarishMaturi on January 1st, 2014:

    Nice Tutorial….Helpful…

  88. shyam on January 8th, 2014:

    Its very easy to understand…

  89. Danvir on January 9th, 2014:

    Nice explanation….

  90. DMV on January 15th, 2014:

    very very thnxxx
    nice tutorial

  91. Dani on January 18th, 2014:

    Simply thanx, helped a lot

  92. Shubham Choudhary on January 29th, 2014:

    thanx..thats really nice

  93. shree on February 11th, 2014:

    very nice…

  94. autkid on February 14th, 2014:

    Thanks……..

  95. Geetha on February 21st, 2014:

    its very useful & clear…thanks

  96. boviee on March 13th, 2014:

    tanx 4 d tutorial . . .
    simple and essesntial xplanation . . . . and useful :-)

  97. avinash on March 13th, 2014:

    thanks a lot sir….

  98. Azizur Rahman on March 19th, 2014:

    Great.Explanation.Thanks a lot sir.

  99. Arun Sasidharan on March 28th, 2014:

    Thanks a lot. Useful for my exams.

  100. mohammed on April 4th, 2014:

    thanks alot that was great

  101. naveen on April 15th, 2014:

    Good tutorial
    Thanks…