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.
Contents
- Defining Passing By Reference
- What is the difference between passing by value and passing by reference?
- Getting a bit more help
- The box of pebbles
- Viewing the house
- Use Cases - why pass by reference?
- Example in the C language
- C Programming Resources
- Coding Tweets
- Bookmark This Lens
- Love This Lens?
- Reader Feedback and suggestions
- About Me
- Image Sources
Defining Passing By Reference
In computer science, a reference is a value that enables a program to directly access a particular data item, such as a variable or a record, in the computer's memory or in some other storage device. The reference is said to refer to the data item, and accessing that data is called dereferencing the reference.
A reference is distinct from the data itself. Typically, a reference is the physical address of where the data is stored in memory or in the storage device. For this reason, a reference is often called a pointer or address, and is said to point to the data. However a reference may also be the offset (difference) between the datum's address and some fixed "base" address, or an index into an array.
The concept of reference must not be confused with other values (keys or identifiers) that uniquely identify the data item, but give access to it only through a non-trivial lookup operation in some table data structure.
References are widely used in programming, especially to efficiently pass large or mutable data as arguments to procedures, or to share such data among various uses. In particular, a reference may point to a variable or record that contains references to other data. This idea is the basis of indirect addressing and of many linked data structures, such as linked lists.
A reference may be compared to a street address, such as "12 Main Street" or "three houses down the road on the left side". Going to the building with that address is analogous to dereferencing the reference. The name "Bob and Joe's Car Shop" might be a unique identifier for the same building, but cannot be compared to a data reference, because finding the building with that name requires a non-trivial search or a lookup in some directory. A reference stored in a data record can be compared to a sign on that shop saying "For tire service please go to 20 Cross Street". Passing a reference to a subroutine, instead of the data, is like giving your friends the address of that shop, instead of taking Bob and Joe and all their tools to your friend's home.
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
-
- ashleymarieh
- about to start coding for my African American history web site
-
- alphabetagaga
- @jewpacabrah MySpace scares me too, and I hadn't realized how effed up the musician coding was until I had to do it myself.
-
- PatBloomfield
- Reading '10 Useful WordPress Coding Techniques' (via @smashingmag) http://tinyurl.com/ylgdohs
-
- jadedoto
- Hanging out in OUTsource. Listening to Wicked. Coding.
-
- laurentk
- What do you use for coding ? I've been stuck on Win32Pad for years, but can't take it anymore... What's your advice ?
Bookmark This Lens
Love This Lens?
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.
About Me
Lensmaster dannystaple, aka Danny Staple, has been a member since July 5 2008, has rated 416 lenses, favorited 128, and has created 37 lenses from scratch. This member's top-ranked page is "OpenTTD vs Simutrans | The Linux Transport Simulation Showdown". See all my lenses
My Bio
I build stuff, grow stuff, read stuff and like to write about it. I like to philosophise, research and learn, and then go the next step and apply, do and build. I love reading How-to's and will experiment with things to see what else I can learn. Read more about me and my lenses here.

KudoSurf Me!
Check out these great lenses...
-
- HOWTO: Fix Broken Christmas Lights Quickly
Broken fairy lights are frustrating and it can take hours to fix them. This year, when about to put up the Christmas lights, I spotted that one set would not turn on and had a brainwave for fixing them. Have a look below for a poor chap faced with t... view lens -
- How to make a propagator to start your seeds
A seed propagator can give seeds a really good strong start and allow you to grow them out of season. Using one will speed up germination and increase germination success. Full step-by-step illustrated instructions for building your own propagator... view lens -
- HOWTO: Transport Your Tools with PortableApps
Being able to take your browser, music player or other software and its configuration with you when you go to other places with computers will save you time and frustration setting up and personalising. At home, you probably have your browser (and o... view lens -
- Danny Staple's Bio
First and foremost, I am now a very proud father to be, which means my life is undergoing all kinds of changes and priority shifts right now. Far from squidoo going out, getting into writing on squidoo was one of those shifts, as it is far less space... view lens -
- Windows XP, Vista, Linux and Ubuntu computer PC software hardware tips
I have written a few pages with tips on computers. I have worked with computers for many years, so many that it would be silly for me to deny being a complete computer geek. However, although I am geeky, I try to explain things in simple terms so an... view lens
Image Sources
The hand/house images were created by myself using inkscape.




