-1

I came across this yesterday and was wondering what the relationship of pass-by-value, pass-by-reference and pass-reference-by-value is. How do they all behave? Which programming language uses which?

Additionally: I have also occasionally heard other terms like pass-by-pointer, pass-by-copy, pass-by-object...how do they all fit in?

Sarien
  • 6,647
  • 6
  • 35
  • 55
  • possible duplicate of [Is Java "pass-by-reference"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference) – Servy Jun 28 '13 at 18:34
  • You should not be asking 5 different questions in a single question. The question of what pass by value/reference is is a duplicate of a million other questions, which languages use which is a list question that's not appropriate on SO, and the last line explaining those terms is either a duplicate of something, or would need to be in a separate question. Such a question also really requires context to really understand what is meant by such terms. – Servy Jun 28 '13 at 18:37

2 Answers2

2

It's a made-up term that refers to something that is actually pass-by-value, but where people always get confused because the only values in the language are references (pointers to things). Many modern languages work this way, including Java (for non-primitives), JavaScript, Python, Ruby, Scheme, Smalltalk, etc.

Beginners always ask questions on StackOverflow about why it's not pass-by-reference, because they don't understand what pass-by-reference really is. So to satisfy them some people make up a new term that they also don't understand, but makes it seem like they learned something, and which sounds vaguely like both pass-by-value and pass-by-reference.

Use of terminology varies widely between different language communities. For example, the Java community almost always describes this semantics as pass-by-value, while the Python and Ruby communities rarely describe it as pass-by-value, even though they are semantically identical.

newacct
  • 119,665
  • 29
  • 163
  • 224
1

Let's start with the basics. You probably already know this or at least think you do. Surprisingly many people are actually misusing these terms quite a bit (which certainly doesn't help with the confusion).

What is pass-by-value and pass-by-reference?

I am not going to tell you the answer just yet and explain it using examples instead. You will see why later.

Imagine you are trying to learn a programming language and a friend tells you that he knows a great book on the topic. You have two options:

  1. borrow the book from him or
  2. buy your own copy.

Case 1: If you borrow his book and you make notes in the margins (and add some bookmarks, references, drawings, ...) your friend can later benefit from those notes and bookmarks as well. Sharing is caring. The world is great!

Case 2: But if, on the other hand, your friend is a bit of a neat freak or holds his books very dear he might not appreciate your notes and even less the occasional coffee stain or how you fold the corners for bookmarking. You should really get your own book and everybody can happily treat his book the way he likes. The world is great!

These two cases are - you guessed it - just like pass-by-reference and pass-by-value. The difference is whether the caller of the function can see changes that the callee makes.

Let's look at a code example. Say you have the following code snippet:

function largePrint(text):
  text = makeUpperCase(text)
  print(text)

myText = "Hello"
largePrint(myText)
print(myText)

and you run it in a pass-by-value language the output will be

HELLO
Hello

while in a pass-by-reference language the output will be

HELLO
HELLO

If you pass-by-reference the variable "myText" gets changed by the function. If you just pass the value of the variable the function cannot change it.

If you need another example, this one using websites got quite a few upvotes.

Okay, now that we have the basics down let's move on to...

What is pass-reference-by-value?

Let's use our book example again. Assume that you have decided to borrow the book from your friend although you know how much he loves his books. Let us also assume that you then - being the total klutz that you are - spilled a gallon of coffee over it. Oh, my... Not to worry, your brain starts up and realizes that you can just buy your friend a new book. It was in such good condition he won't even notice!

What does that story have to do with pass-reference-by-value?

Well, imagine on the other hand that your friend loved his books so muchs he doesn't want to lend it to you. He does however agree to bring the book and show it to you. The problem is, you can still spill a gallon of coffee over it but now you can't buy him a new book anymore. He would notice!

While this may sound dreadful (and probably has you sweating as if you had drunk that gallon of coffee) but it is actually quite common and a perfectly good way to share books call functions. It is sometimes called pass-reference-by-value.

How does the code example fare?

In a pass-reference-by-value function the output of our snippet is

HELLO
Hello

just like in the pass-by-value case. But if we change the code a little bit:

function largePrint(text):
  text.toUpperCase()
  print(text)

myText = "Hello"
largePrint(myText)
print(myText)

The output becomes:

HELLO
HELLO

just like in the pass-by-reference case. The reason is the same as in the book example: We can change the variable (or book) by calling text.toUpperCase() (or by spilling coffee on it). BUT we cannot change the object that the variable references anymore (text = makeUppercase(text) has no effect); in other words we cannot use a different book.

So summarizing we get:

pass-by-value:

You get your own book, your own copy of the variable and whatever you do with it the original owner will never know.

pass-by-reference:

You use the same book as your friend or the same variable as the caller. If you change the variable it changes outside your function.

pass-reference-by-value

You use the same book as your friend but he doesn't let it out of his sight. You can change the book but not EXchange it. In variable terms that means that you cannot change what the variable references but you can change that which the variable references.

So now when I show you the following Python code:

def appendSeven(list):
    list.append(7)

def replaceWithNewList(list):
    list = ["a", "b", "c"]

firstList = [1, 2, 3, 4, 5, 6]
print(firstList)
appendSeven(firstList)
print(firstList)

secondList = ["x", "y", "z"]
print(secondList)
replaceWithNewList(secondList)
print(secondList)

and tell you that the output is this

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
['x', 'y', 'z']
['x', 'y', 'z']

what do you say?

Indeed, you are correct! Python uses pass-reference-by-value! (And so is Java and Ruby and Scheme and...)

Conclusion:

Was this extremely difficult? No. Then why are so many people confused by it? (I'm not going to link to all the other questions about "How do I pass a variable by reference in Python" or "Is Java pass-by-reference?" and so on...)

Well, I see several reasons:

  1. Saying pass-value-by-reference is really long (and it behaves almost the same as pass-by-reference anyway so get of my back and stop splitting hairs!)
  2. There are nasty special cases that complicate the issue
    • like primitive types (which are often pass-by-value in pass-value-by-reference languages)
    • immutable types (which cannot be changed so passing them by-reference doesn't get you anywhere)
  3. Some people just like to use different names:
    • pass-by-copy for pass-by-value because if you paid attention, you need your own book, so a copy has to be made. (But that is an implementation detail, other tricks like copy-on-write might be used to only sometimes make a copy.)
    • pass-by-reference for pass-reference-by-value because, after all, you did get a reference and that means you can change the object that the caller had (even if you cannot EXchange it)
    • pass-by-reference for pass-reference-by-value because almost no language actually has pass-by-reference (notable Exceptions (that I know): C++, C# but you have to use special syntax)
    • pass-by-sharing for pass-reference-by-value (remember our book example? that name is actually way better and everybody should use it!)
    • pass-by-object for pass-reference-by-value because allegedly Barbara Liskov first named it and used that, so that's its birthname)
    • pass-by-pointer for pass-by-reference because C doesn't have pass-by-reference and that is how it allows you to accomplish the same thing.

If there is anything you think I should add, please let me know. If I made a mistake, PLEASE LET ME KNOW!

For those of you who still haven't got enough: http://en.wikipedia.org/wiki/Evaluation_strategy

Community
  • 1
  • 1
Sarien
  • 6,647
  • 6
  • 35
  • 55
  • This seems like an extremely complex, verbose explanation for a concept which is really not that tricky. A couple of code examples would probably be sufficient... – Oliver Charlesworth Jun 04 '13 at 23:22
  • @OliCharlesworth For a reference probably but I think examples are nice for first learning the concept. Please do add an answer if you like. – Sarien Jun 04 '13 at 23:26
  • Oh, I mean that you could probably get rid of all the verbose text analogies, and just stick with the comparative pseudocode examples. The analogies just muddy the concept. – Oliver Charlesworth Jun 04 '13 at 23:27