which language allows you to change an argument's value?

Erik Wikström Erik-wikstrom at telia.com
Mon Oct 1 12:52:03 CEST 2007


On 2007-09-30 23:03, Arne Vajhøj wrote:
> Erik Wikström wrote:
>>> their reference (alias) mechanism.  And C, Python, and Ruby probably
>>> won't let you do that.  What about Java and Perl?
>> 
>> C will let you do it with pointers (it is just a syntactical difference
>> from references in this case) and Java's references allows it.
> 
> Neither C or Java has call by reference.

I never said that, what I said was that C allows you to simulate it by
passing pointers instead, and since you always use references in Java
(except for primitive types) you always pass a reference.

> C pointers and Java references may work similarly in most cases
> but it is still call by value.

The difference I am trying to show is that between value semantics
(where a copy of the parameter is used in the function) verses reference
semantics where you pass a reference to the object. It is true that when
using a pointer in C you pass a copy of the pointer, but not of the
object it refers to. Similarly you might pass a copy of the variable
holding a reference in Java, but not a copy of the object it refers to.
Thus, in the function you will be working on the referred object, just
like when passing by reference in C++.

>> know about Ruby, Python and Perl, but (AFAIK) the all have OO support so
>> I would be surprised if they used purely value semantics.
> 
> I can not see why OO should indicate anything about call by reference
> support.

There is no direct connection, except that all OO programming languages
that I know of supports reference semantics. You seldom want to work on
copies of objects, rather you want to work on the original object and
for that you need to be able to refer to it, for that you need some kind
of reference.

>>> isn't "what i pass in, the function can modify it" not a desireable
>>> behavior if i am NOT passing in the address of my argument?  For one
>> 
>> Being able to pass the actual object instead of a copy is highly
>> desirable for two reasons. In most languages only one return value is
>> allowed for a function so the ability to change parameters allows you to
>> artificially return more without having to wrap them in constructs. The
>> second reason is that for large objects the performance hit of having to
>> create a copy each time you call a function can be forbidding.
> 
> Usually it is not a good thing, because it makes the code much
> more difficult to read.
> 
> But sometimes it is handy.
> 
> I think C# got it right.
> 
> It allows it but require an explicit marking of it in both formal
> argument list and actual argument list.

I think you are confusing two different things here, one is the ability
to pass a reference to an object, instead of a copy of the object. This
is what C++ references allows you to do, just like Java references, C
pointers, and any other kind of references that I know about.

The other thing is to allow the function to change what a reference
refers to, to this not possible with C++ references, C pointers, Java
references, or most other reference types that I know about. You can do
this by using a reference to a pointer in C++, a pointer to a pointer in
C, or using the ref and out keywords in C#. Doing that can be confusing,
but often the semantics for doing so are pretty obvious.

>>> Java, Python, and Ruby, and we pass in a reference to object (not C+
>>> +'s meaning of alias reference)
>> 
>> In what way does the C++ reference differ from those in Java, Python,
>> and Ruby in this situation?
> 
> C++ and Java are very different in this aspect.

Might be, my question was about what aspect we are talking about.

-- 
Erik Wikström



More information about the Python-list mailing list