The difference between pass-by-value and pass-by-reference

The difference between pass-by-value and pass-by-reference (also known as "call-by-value" and "call-by-reference" respectively) is most easily explained using the following pseudocode.

x = "blue"

function modify(input) {
	input = "red"
}

modify(x)
print(x) // "blue" indicates language passes by value
         // "red" indicates language passes by reference

Hint: almost all popular programming languages pass by value. This includes C, Java, Python and PHP. The most notable exception is Perl.

(Some programming languages allow you to pass by reference if you use special syntax. This includes PHP and C#.)

Look out!

When x is an object, many pass-by-value programming languages pass an object reference as their value.

Use this pseudocode to test this behaviour.

x = new Object
x.colour = "blue"

function modify(input) {
	input.colour = "red"
}

modify(x)
print(x.colour) // "blue" if pass-by-value and value is whole object
                // "red" if pass-by-value and value is reference to object
                // "red" if pass-by-reference

Hint: almost all popular pass-by-value programming languages use a reference to the object as their value. This includes Java and Python.

That's it. Now you know the whole story.

Back to Code
Back to Things Of Interest

Facebook Twitter Reddit Email Hacker News StumbleUpon

Discussion (11)

2013-06-17 19:30:06 by Sam:

There are approximately ten thousand places on the internet where this is explained, but none of them were succinct enough for my taste. Every word spent explaining is wasted when you can just demonstrate the concept with code. I considered adding examples, but I don't think they're needed.

2013-06-17 19:41:22 by AliceOMeta:

http://pastebin.com/RGnG33rQ

2013-06-17 19:46:39 by Sam:

Thanks for that example which demonstrates all three behaviours. That is a neat piece of code and I did come up with it myself as well, but I decided not to supply it.

If your aim was to find out whether your chosen programming language calls by value, by entire object or by reference-to-object, then that example code would be quite useful. But my aim is to make the conceptual differences between the three behaviours unambiguously clear by people reading and understanding the pseudocode, not necessarily by running it. For that I was aiming for the simplest possible snippets. The two separate examples are much simpler to follow, even if the third snippet is a little shorter.

Cheers!

2013-06-17 20:29:09 by David:

I still feel like the difference between call-by-reference and call-by-value needs to be explained from a memory point of view. Admittedly, this requires delving into pointers, but it's more valuable to understand that a reference is a place in memory and a value is what is at that place. Of course, determining language behavior is neat too.

2013-06-17 22:40:33 by Ian:

In my experience, the problem is less one of misunderstanding of the differences between "call/pass-by-value" vs. "call/pass-by-reference" and more a lack of any universal agreement as to what the phrases mean. I've heard arguments that call-by-value is what you describe as call-by-reference and vice versa.

2013-06-18 06:43:09 by skztr:

Doesn't PHP also use a reference-to-the-object as the value?

2013-06-18 10:52:50 by Sam:

Hmm, you're right. However, PHP treats arrays as primitive values, not objects, and passes them entirely.

Got me again, PHP!

2013-06-18 18:18:07 by bdew:

Python isn't exactly call by value... It makes a copy of the reference to the underlying object, if that object is mutable... you're in for some surprises :P
For those coming from C-like languages - all variables in python are actually pointers to an object and that's what gets passed around in calls.

2013-06-18 20:09:56 by Sam:

Yes, that's what the article says.

2013-06-25 02:57:21 by KimikoMuffin:

C# allows you to specify whether an object type is called by value or by reference; the definition is a "struct" in the former case, and a "class" in the latter.

2013-07-26 23:20:33 by JonSkeet:

@KimikoMuffin: No, struct vs class doesn't change *how* an argument is passed - it changes whether the value passed is a reference or a struct value. However, ref/out *does* change how an argument is passed. See http://pobox.com/~skeet/csharp/parameters.html for more details.

(It's worth separating those two out very clearly; you can pass a reference type argument *by* reference or by value, and it makes a big difference.)