From: Xavier Noria Date: 2007-09-30T20:29:49+09:00 Subject: Re: which language allows you to change an argument's value? On Sep 30, 2007, at 12:50 PM, Summercool wrote: > 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? Java can't do that, Perl _only_ works that way. > 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? In general it is bad practice to change the state of the arguments you receive, as bad practice as changing the arguments themselves. A function shouldn't do that unless it documents it, unless it makes sense and it is expected by the caller. For example, that's what allows you to write subroutines like chomp in Perl: chomp $str; You expect $str to be chomped and since this is procedural style you can implement that with pass-by-reference, and that's fine. Otherwise you'd need to pass a reference to $str to be able to change the string through the reference (what you do in C for integers) which is cumbersome, and in fact would be a workaround for pass-by- value. In Perl there's no way to prevent a subroutine from changing the values in the caller except passing readonly stuff like literals. But, you know, people just don't do that. The same way people don't start changing argument's state gratuitously in Ruby. Both are analogous bad practices. -- fxn