On 2007-09-30 12:47, Summercool wrote:
> I wonder which language allows you to change an argument's value?
> like:
> 
> foo(&a) {
>   a = 3
> }
> 
> n = 1
> print n
> 
> foo(n)     # passing in n, not &n
> print n
> 
> and now n will be 3.  I think C++ and PHP can let you do that, using

Since you know C++ can do it why do you include c.l.c++? Honestly cross-
posting to groups discussing so many different groups is seldom a good
idea since the languages differ too much for a useful discussion.

> 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. I do not
know about Ruby, Python and Perl, but (AFAIK) the all have OO support so
I would be surprised if they used purely value semantics.

> is there any way to prevent a function from changing the argument's
> value?

Some languages, like C++ have the ability to make an argument const.

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

> thing, if we use a module, and call some functions in that module, and
> the module's author made some changes to his code, then we have no way
> of knowing what we pass in could get changed.  Of course, if it is in

As for knowing when a library function modifies an argument or not, well
that is why we have documentation.

> 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?

-- 
Erik Wikström
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to