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.

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

var x = "blue"
modify(x)
print(x) // "blue" indicates `x` was passed by value
         // "red" indicates `x` was passed by reference

Hint: almost all popular programming languages pass by value, at least by default. This includes C, C++, Java, JavaScript, Python, PHP and Visual Basic .NET. The most notable exception is Perl.

Some programming languages allow you to pass by reference if you use special syntax. This includes C++, C#, PHP and Visual Basic .NET.

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.

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

var x = {colour : "blue"}
modify(x)
print(x.colour) // "blue" if whole object `x` was passed by value
                // "red" if reference to object `x` was passed by value
                // "red" if `x` was passed by reference

Hint: almost all popular pass-by-value programming languages pass an object reference as their value. This includes Java, JavaScript and Python. A notable exception is C++.

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

Discussion (12)

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

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 qntm:

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 qntm:

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 qntm:

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.)

2015-05-24 21:10:35 by anonymous:

QuickBASIC also passes by reference by default (and does not support passing strings by value; you must make a copy of the string either where it is called or inside of the subroutine). C allows passing pointers to variables and that stuff, so if you use pointers then you can modify it in that way. JavaScript doesn't have have passing by reference, but an object value is a pointer to that object so you can modify the properties of that object and remains elsewhere too. In PHP it is possible to fake pointers by creating arrays of references. (Such a pointer cannot be serialized, but other than that can be used like any other values.)

New comment by :

Plain text only. Line breaks become <br/>
The square root of minus one: