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

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Sep 30 07:21:41 EDT 2007


En Sun, 30 Sep 2007 07:47:13 -0300, Summercool <Summercoolness at gmail.com>  
escribi�:

> 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
> their reference (alias) mechanism.  And C, Python, and Ruby probably
> won't let you do that.  What about Java and Perl?
>
> is there any way to prevent a function from changing the argument's
> value?
>
> 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

C++ lets you use const references - and any well written code should use  
const& whenever the argument is not to be modified. A function/method may  
have two overloaded versions, with and without the const modifier.
So, if the argument *is* to be modified, there is no point in avoiding it  
(unless the interfase is not well designed in the first place)

In Python, rebinding a name inside a function does not have any effects in  
the caller. That is,

def foo(a):
   a = 3

n = 1
foo(n)
print n

will still print 1, not 3.

-- 
Gabriel Genellina




More information about the Python-list mailing list