Search code examples
language-agnosticpass-by-referencepass-by-value

How is 'pass by reference' implemented without actually passing an address to a function?


I am well aware of the fact that in C and C++ everything is passed by value (even if that value is of reference type). I think (but I'm no expert there) the same is true for Java.

So, and that's why I include language-agnostic as a tag, in what language can I pass anything to a function without passing some value?

And if that exists, what does the mechanism look like? I thought hard about that, and I fail to come up with any mechanism that does not involve the passing of a value.

Even if the compiler optimizes in a way that I don't have a pointer/reference as a true variable in memory, it still has to calculate an address as an offset from the stack (frame) pointer - and pass that.

Anybody who could enlighten me?


Solution

  • How is 'pass by reference' implemented without actually passing an address to a function?

    Within the context of the C languages, the short answers are:

    • In C, it is not.
    • In C++, a type followed by an ampersand (&) is a reference type. For instance, int& is a reference to an int. When passing an argument to a function that takes reference type, the object is truly passed by reference. (More on this in the scholarly link below.)

    But in truth, most of the confusion is semantics. Some of the confusion could be helped by:

    • 1) Stop using the word emulated to describe passing an address.
    • 2) Stop using the word reference to describe address

    Or

    • 3) Recognize that within the context of the C/C++ languages, in the phrase pass-by-reference, the word reference is defined as: value of address.

    Beyond this, there are many examples of illusions and concepts created to convey impossible ideas. The concept of non-emulated pass-by-reference is arguably one of them, no matter how many scholarly papers or practical discussions.

    This one (scholarly paper category) is yet another that presents a distinction between emulated and actual pass-by-reference in a discussion using both C & C++, but who's conclusions stick closely to reality. The following is an excerpt:

    ...Somehow, it is only a matter of how the concept of “passing by reference” is actually realized by a programming language: C implements this by using pointers and passing them by value to functions whereas C++ provides two implementations. From a side, it reuses the same mechanism derived from C (i.e., pointers + pass by value). On the other hand, C++ also provides a native “pass by reference” solution which makes use of the idea of reference types. Thus, even in C++ if you are passing a pointer à la C, you are not truly passing by reference, you are passing a pointer by value (that is, of course, unless you are passing a reference to a pointer! e.g., int*&). Because of this potential ambiguity in the term “pass by reference”, perhaps it’s best to only use it in the context of C++ when you are using a reference type.

    But as you, and others have already noted, in the concept of passing anything via an argument, whether value or reference, that something must by definition have a value.