Re: which language allows you to change an argument's value?
- From: "Gabriel Genellina" <gagsl-py2@xxxxxxxxxxxx>
- Date: Sun, 30 Sep 2007 08:21:41 -0300
En Sun, 30 Sep 2007 07:47:13 -0300, Summercool <Summercoolness@xxxxxxxxx> 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
.
- Prev by Date: Re: Can you please give me some advice?
- Next by Date: Re: Optparse and help formatting?
- Previous by thread: Can you please give me some advice?
- Next by thread: s.split() on multiple separators
- Index(es):
Relevant Pages
|
|