On Sep 30, 3:47 am, Summercool <[EMAIL PROTECTED]> 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 > 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 > 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 > Java, Python, and Ruby, and we pass in a reference to object (not C+ > +'s meaning of alias reference), so the object can get changed, but > that can be expected, vs passing in n, when n = 1. Even when it is > Ruby, when everything is an object, passing n in when n = 1 won't ever > make n become 3. Is there a way to prevent it from happening in the > languages that allows it?
Some would say that in truely good OO design, the state should be protected by the object (not the constness of the reference to the object) -- http://mail.python.org/mailman/listinfo/python-list