Re: pass by reference



"Chris Smith" <cdsmith@xxxxxxx> wrote in message
news:47f8654d$0$4409$c3e8da3@xxxxxxxxxxxxxxxxxxxx
Andreas Leitgeb wrote:
[ SNIP ]

By the (at least as "very") definition of "reference" alone, one might
expect different behaviour of Java already. No matter if it is passed or
not. Java doesn't really have "references" the way C++ has.

I'm not sure what C++ has to do with this.

There really isn't a widely used definition of a reference. The word is
used differently by different languages, and it's best to just adopt the
definition from the language in question. C++ means one thing; Java
another; CAML yet another. On the other hand, pass-by-reference is a
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
concept with a well-understood definition.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Namely: the lvalue of the
formal parameter is the same as the lvalue of the actual parameter.
[ SNIP ]

Apparently not so well-understood, Chris, not if we (and tens of thousands,
and hundreds of thousands, and millions, of others) are having this
discussion. I spent the past few days doing some fairly intense research
(Googling) on pass by reference, pass by value (also keywords "call by ...",
"pointer", "evaluation strategy", etc etc) and so forth. I tried to
categorize sources as summary and explanatory: "summary" sources had a quick
definition of a term used in this thread, and "explanatory" sources actually
had some discussion of the topics. Naturally I also applied some measure of
quality control, without trying to apply a personal bias based on my own
perspective (which has already been expressed).

(Note: I'll use PBV and PBR to abbreviate "pass by value" and "pass by
reference".)

And the fact is, "call/pass by value/reference" is _not_ something with a
well-understood definition, not when it comes to categorizing things like
"pass pointer by value". Like it or not, while people do understand PBV to
mean copying the value of the actual argument into the formal parameter
variable, they (myself included) also make a difference between that actual
argument being the thing of interest (a primitive or object) or being a
reference to the thing of interest.

I'd like to think that most folks, including everyone participating in this
thread, grasps the difference between passing a pointer by value and passing
a reference...the swap() function example, so to speak. In one you can
actually change the value of the thing passed as it visible in the caller,
and in the other you cannot. Yes, you can use pointers to modify the value
of the thing pointed at, in the calling code, but that's still not modifying
the actual parameter passed (the pointer) as it exists in the calling code.

However, lots of people (again, myself included) do describe "passing a
pointer by value" as PBR, insofar as being able to modify the pointed-to
thing is very often described as PBR. And the reason this terminology is
common because when passing a pointer by value you do in fact have a
reference to the object. I'm trying not to be argumentative here, I'm just
pointing out (pun not intended) that there are at least two different usages
of the word "reference" here, one somewhat less technically precise than the
other, but both are (arguably) valid. It depends on the context.

I myself am perfectly happy to use the more precise language to keep Java
people happy - it's no sweat off my back. But equally well I'm not going to
correct a C programmer who says "pass by reference" (when it's "pass pointer
by value") when he clearly knows what it is he or she is doing.

AHS


.



Relevant Pages

  • Re: ByVal vs. ByRef
    ... But I still don't understand why I have to pass the TreeNode argument ByVal. ... I understand why passing it ByVal works. ... What is "the Value" of a reference variable? ... A "pointer" to the object. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Pass by reference / pass by value
    ... Also mentioned, but perhaps not quite as clear, is that Java ... >exactly is the difference between pass by reference and passing a reference ... There is a difference between passing by reference ... such as passing a pointer to a pointer. ...
    (comp.lang.java.programmer)
  • Re: pointer/ref question
    ... reference" to generically indicate we're conveying the address of an ... to /me/ one can describe both of these situations as "passing ... the term "pass by pointer" for the first case when teaching C++, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: The Java no pointer big fat lie!
    ... A pointer is a memory address in C, no more, no less. ... >>not what a reference in Java is. ... reference types, it wouldn't be a good comparison. ... My statement is correct with reference to the C++ language. ...
    (comp.lang.java.programmer)
  • Re: How to pass a reference to a CComBSTR
    ... >> This is not passing by reference - you are passing pointers. ... CComBSTR tempCComBSTR); ... Approach B (passing by pointer) ...
    (microsoft.public.vc.atl)