Understanding passing by reference
Ranked #4,616 in Internet, #243,967 overall
How to understand passing by reference vs passing by value
If you do not see your language of choice, feel free to suggest an example, or a request for the creation of one. Note that not every language directly supports passing by reference, but there are similar ways to achieve the affect if it is desired.
What is the difference between passing by value and passing by reference?
The Sheet of Paper

![]()
Muse with Scroll Art Print
Buy at AllPosters.com
You can pass it to him one of two ways (for the sake of this example).
- Upload a scan of the paper. A scanned document is a copy of the contents of the paper. So a new container (variable) gets a copy of the content (value) or the original. The person marking the work can do whatever they like to this copy. It is theirs. You may never see what they did to it during marking, or they may give a copy of their copy back to you. Either way, the original remains unchanged by them. This is passing by value.
- You give them the paper. The paper container is passed to them instead of just its content. You have passed by reference. The value on the paper may then be altered, erased or otherwise defaced by the marker. When you get your original back, it is unlikely to be the same as when you gave it to them. Passing by reference is like giving the container to recipient.
Getting a bit more help
It is useful for a beginner or those converting from another language in that it has a reference to the complete syntax of the compiler and the preprocessor, as well as the standard API.
This is also a handy reference for an experienced coder. If there are any details that are important, like operator precedence or complex macro operations then this is the definitive reference. I generally keep one in easy reach of the team I work with.
The box of pebbles
Copy all the pebbles or just look at the originals?
![]()
Zen Pebbles Art Print
Buy at AllPosters.com
A box of pebbles collected artistically seems like a simple thing. How do you show them off and allow someone else to experience them though? Consider the box the container (the variable) and the pebbles are the content (the value).
Again, for the sake of example, lets consider two possible methods:
- Upload a copy/scan? Scanning a box of pebbles for a 3d representation and reproducing it is a very complicated and expensive task. In computing terms this may represent a large or complicated structure that you do not want to copy.
- Handing someone the original - passing by reference would be handing someone the original box. They can hold the box, take out the pebbles and truly appreciate them. This is very cheap.
This comes with a warning, the recipient of the box may change its contents, like the paper, perhaps even just rearrange the pebbles undoing a meticulous arrangement. The danger of giving a recipient the original is it may not come back as you gave it to them.
Viewing the house
Giving the address? or a whole house?
This scenario is not totally ridiculous. Many of us have gone to look at a flat. That seems like quite a normal and routing thing to do.And yet, there are a few ways to let somebody look at a house.
The first is to hand them a house. Think about that statement. The house is a) Immovable b) impossibly heavy to hand over c) Probably belongs to somebody else. It would have to be a very small house, or they have very large hands.
The second (not shown in the image) is to hand them a copy of the house. Copying a house is pretty much impossible, and the immovable and handing over problems strike again. You could also give them photos of the house, but for a discerning customer, wanting a bigger overview, or to interact with the house - try the doors etc, it will not be good enough.
The third, and only sensible option in this case, is to give them the address of the house. This means that they can go and see it in-situ. You may even take them there - but you have still given them the reference of the house, how to get to the original, not a copy, and not trying to hand them the whole original.
The same is true in code, an object may be too big, or immovable, so passing by reference is the only sensible method.
Use Cases - why pass by reference?
When would you want to use a reference and not a value?
- When you intend for the recipient to change the value. A common trick in C is to have a functions return any error codes, and pass by reference variables to store the actual result of an operation.
- Passing in a large structure. From the C perspective - passing by value means that a whole structure is copied onto the stack. Too many of these, and an application may run out of stack space, which is a recipe for serious problems. Passing by reference means that the copy is not made, preserving stack space.
- Passing in a deep structure. In situations where the stack case is unlikely, it is still possible to have a deep structure, where copying the structure, its members, their submembers, and so on completely will be difficult. Passing by reference means that the original can be used and no copies need be taken there.
- Passing a reference to a resource from outside the app - a hardware buffer pointer for example.
Example in the C language
#include <stdio.h>
void passByValue(int value)
{
printf("I am the recipient of a pass by value.\n");
printf("I was given the value of %d\n", value);
value = 33;
printf("But I prefer %d, so I changed my copy to that\n", value);
void passByRef(int * valueRef)
{
printf("I am a recipient of a pass by reference.\n");
printf("I was given a reference to a value of %d\n", *valueRef);
*valueRef = 13;
printf("I prefer %d, so I shall change the original to that\n",*valueRef);
int main()
{
int container = 3;
printf("The variable container has a value of %d\n", container);
printf("Passing it by value:\n***\n");
passByValue(container);
printf("\n****\nAfter passing by value, container has a value of %d\n", container);
printf("\nPassing the container as a reference:\n***\n");
passByRef(&container);
printf("\n****\nAfter passing by reference, the container has a value of %d\n", container);
In this example there are two helper functions, passByValue and passByRef. I am actually passing it as a pointer for clarity here.
In passByValue I print the original value, then set the passed by value to 33.
If you look in the function main, the variable "container" is used here. I set it to 3, then pass it into the passByValue function. When that function returns, the value of 3 will still be in container, as passByValue was given the value only, and stored the value in its own local variable "value". When it modified that, it only modified the local value, and not the original variable.
After this, the main function will pass a pointer to value into the function passByRef. The pointer is the reference. The pointer itself is a value, but can be dereferenced to the actual storage area for container. This function will print the value - using the star operator to dereference the value. It will then set the value - note again, it dereferences before setting it. The function prints the new value, dereferencing again. Because the function has actually modified the value - the container that the reference was pointing to, then when it returns, the variable "container" in main has been affected and now has the value 13.
Output
The variable container has a value of 3
Passing it by value:
***
I am the recipient of a pass by value.
I was given the value of 3
But I prefer 33, so I changed my copy to that
****
After passing by value, container has a value of 3
Passing the container as a reference:
***
I am a recipient of a pass by reference.
I was given a reference to a value of 3
I prefer 13, so I shall change the original to that
****
After passing by reference, the container has a value of 13
C Programming Resources
Coding Tweets
Love This Lens?
This module only appears with actual data when viewed on a live lens. The favorite and lensroll options will appear on a live lens if the viewer is a member of Squidoo and logged in.
Reader Feedback and suggestions
Have you enjoyed this lens?
Did it help you overcome a difficulty understanding this issue (or possibly confuse you further - I hope not)?
Have you used this in a lecture or course?
Let me know also if you have code examples to show this.
-
Reply
-
Spook
Jul 14, 2009 @ 2:07 am | delete
- Blimey this is a bit past my limited mind but nevertheless an excellent and informative lens.
-
-
Reply
-
tdove
Mar 16, 2009 @ 3:52 pm | delete
- Thanks for joining G Rated Lense Factory!
-
About Me
Image Sources
The hand/house images were created by myself using inkscape.
by dannystaple
I have been coding since I was a kid, and have worked in plenty of languages. Though C is my forte, I use Perl, C#, Java, Python, PHP, a little Ruby and... more »
- 80 featured lenses
- Winner of 15 trophies!
- Top lens » The Lego RCX, Inside And Out
Explore related pages
- Learn to Program for the iPhone Learn to Program for the iPhone
- Tron Legacy Outfit Accessories Tron Legacy Outfit Accessories
- Tron Costumes For Sale Tron Costumes For Sale
- Programming Beginner Programming Beginner
- Best Book on Mahout: Mahout in Action Best Book on Mahout: Mahout in Action
- Teach Children How To Program A Computer Teach Children How To Program A Computer