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



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

.



Relevant Pages

  • Re: newbie questions
    ... > Passing a pointer by value appears to me as passing a var by reference. ... Python doesn't support pass-by-reference in this standard use of the ... def clear: ...
    (comp.lang.python)
  • Re: Python advocacy in scientific computation
    ... int, you're passing a pointer to an int object, just ... pointer to a list object. ... It's just that Python ...
    (comp.lang.python)
  • Re: Type Hinting vs Type Checking and Preconditions
    ... Python community, both for tool enablement and for disambiguous ... Passing 14 to it will return 28, whereas passing "14" to it will return ... parameter to an int before it is multipled, ... little reason to have it crash. ...
    (comp.lang.python)
  • Re: [Swig-user] How to receive a FILE* from Python under MinGW?
    ... I'm not sure that your snippet really solves my problem. ... see from, you're just passing a filename instead of a file pointer, ... FILE* from Python and all the way into into a existing shared-library ... There is fairly well-known problem on MinGW with passing FILE pointers ...
    (comp.lang.python)
  • Re: newbie questions
    ... python is the first language I use that is like java 100% oriented ... and passing them local to the main objects. ... > instance variables, ...
    (comp.lang.python)