Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > which language allows you to change an argument's value?

Reply
Thread Tools

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

 
 
Summercool
Guest
Posts: n/a
 
      09-30-2007

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?

 
Reply With Quote
 
 
 
 
rolkA
Guest
Posts: n/a
 
      09-30-2007
On 30 sep, 12:47, Summercool <(E-Mail Removed)> wrote:
> I wonder which language allows you to change an argument's value?
> like:
>
> foo(&a) {
> a = 3
>
> }
>
> ...
> is there any way to prevent a function from changing the argument's
> value?
> ...
> Is there a way to prevent it from happening in the
> languages that allows it?


Hi,
Of course in C++, functions that don't modify argument's value should
(i'd rather say MUST) wait for a CONST reference or a value :

// const ref
foo(const T& a) {
a = 3; // error

}
// value
foo(T a) {
a = 3; // ok, but modify only the formal parameter : the argument
(aka actual parameter) is not changed

}

Now if you want to prevent a function (from a library you are using)
to modify it... Erm well, you shouldn't : a good librairy will never
wait for a non-const argument if it doesn't need to be modified. So in
this case "Is there a way to prevent it from happening" is
unpertinent : you could copy the object, but if the purpose of the
fucntion was to modify it, it's pointless.

 
Reply With Quote
 
 
 
 
7stud --
Guest
Posts: n/a
 
      09-30-2007
SpringFlowers AutumnMoon wrote:
> we have no way
> of knowing what we pass in could get changed.


Sure you do. You look at the function's signature. In order to use
someone else's library, you have to know the function's signature. And
the signature explicitly tells you whether the value you pass in could
be changed.



--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      09-30-2007
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


 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      09-30-2007
On 2007-09-30 12:47, Summercool 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


Since you know C++ can do it why do you include c.l.c++? Honestly cross-
posting to groups discussing so many different groups is seldom a good
idea since the languages differ too much for a useful discussion.

> their reference (alias) mechanism. And C, Python, and Ruby probably
> won't let you do that. What about Java and Perl?


C will let you do it with pointers (it is just a syntactical difference
from references in this case) and Java's references allows it. I do not
know about Ruby, Python and Perl, but (AFAIK) the all have OO support so
I would be surprised if they used purely value semantics.

> is there any way to prevent a function from changing the argument's
> value?


Some languages, like C++ have the ability to make an argument const.

> 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


Being able to pass the actual object instead of a copy is highly
desirable for two reasons. In most languages only one return value is
allowed for a function so the ability to change parameters allows you to
artificially return more without having to wrap them in constructs. The
second reason is that for large objects the performance hit of having to
create a copy each time you call a function can be forbidding.

> 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


As for knowing when a library function modifies an argument or not, well
that is why we have documentation.

> Java, Python, and Ruby, and we pass in a reference to object (not C+
> +'s meaning of alias reference)


In what way does the C++ reference differ from those in Java, Python,
and Ruby in this situation?

--
Erik Wikström
 
Reply With Quote
 
David B. Williams
Guest
Posts: n/a
 
      09-30-2007
> And C, Python, and Ruby probably won't let you do that...

From my understanding, in Ruby all argument passing is by value.
However, the value passed may not be what you expect... The value that
is passed is that of the reference, not the value that the reference
refers to. So, for example:

def foo(arg)
arg = 3
end

a = 'Mike'

foo(a)

This will not change the value of a in the top level. Why? Because foo
reassigns the argument reference arg to 3, a different object than
'Mike.' However:

def bar(arg)
arg[0] = 'b'
end

a = 'Mike'

bar(a)

This will change a from 'Mike' to 'bike.' Why? Because instead of
reassigning arg to a new object, the object itself ('Mike') has been
modified through the method call []. So, if the method simply reassigns
the reference, there will be no change in the original (it still refers
to the same object). But, if the method modifies that object, the
original reference will reflect that modification, since it is still
referencing the now modified object.

It is customary in Ruby to append an exclamation mark (!) to indicate
methods that actually modify an object in place. So, bar! would have
been a more appropriate name for the second method.

Hope that helps.

David B. Williams
http://www.cybersprocket.com
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Summercool
Guest
Posts: n/a
 
      09-30-2007
On Sep 30, 4:18 am, 7stud -- <(E-Mail Removed)> wrote:
> SpringFlowers AutumnMoon wrote:
> > we have no way
> > of knowing what we pass in could get changed.

>
> Sure you do. You look at the function's signature. In order to use
> someone else's library, you have to know the function's signature. And
> the signature explicitly tells you whether the value you pass in could
> be changed.


do you mean in C++? I tried to find signature in two C++ books and it
is not there. Google has a few results but it looks something like
prototype. Is signature the same as the function prototype in the .h
file? If so, don't we usually just include <___.h> and forget about
the rest. Documentation is fine although in some situation, the
descriptions is 2 lines, and notes and warnings are 4, 5 times that,
and the users' discussing it, holding different opinion is again 2, 3
times of that length. I think in Pascal and C, we can never have an
argument modified unless we explicitly allow it, by passing in the
pointer (address) of the argument.

also i think for string, it is a bit different because by default,
string is a pointer to char or the address of the first char in C and C
++. So it is like passing in the address already. it is when the
argument n is something like 1 that makes me wonder.


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      09-30-2007
On Sep 30, 3:47 am, Summercool <(E-Mail Removed)> 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)

 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      09-30-2007
On 2007-09-30 18:49, Summercool wrote:
> On Sep 30, 4:18 am, 7stud -- <(E-Mail Removed)> wrote:
>> SpringFlowers AutumnMoon wrote:
>> > we have no way
>> > of knowing what we pass in could get changed.

>>
>> Sure you do. You look at the function's signature. In order to use
>> someone else's library, you have to know the function's signature. And
>> the signature explicitly tells you whether the value you pass in could
>> be changed.

>
> do you mean in C++? I tried to find signature in two C++ books and it
> is not there. Google has a few results but it looks something like
> prototype. Is signature the same as the function prototype in the .h
> file?


A signature is what is required to identify a function and includes
return type, name of function and the types of the parameters, while it
looks just like a prototype it is not. A prototype is something you
write to satisfy your compiler while a signature identifies a function.
Below are some examples of signatures, only the last can modify the
values of its parameter.

void foo(int, float)
std::string bar(const std::string&, int, int)
void baz(std::string&)

> If so, don't we usually just include <___.h> and forget about
> the rest. Documentation is fine although in some situation, the
> descriptions is 2 lines, and notes and warnings are 4, 5 times that,
> and the users' discussing it, holding different opinion is again 2, 3
> times of that length.


Unless you read the documentation how do you know which files to
include? And what documentation are you reading which does not clearly
specify the functionality of the functions described?

> I think in Pascal and C, we can never have an
> argument modified unless we explicitly allow it, by passing in the
> pointer (address) of the argument.


In C++ the arguments cannot be modified unless you either pass a pointer
to a non-const object or a non-const reference, so it is just as
explicit. (Notice that it is possible to cast the constness away, but
doing is extremely dangerous and should not be done.)

> also i think for string, it is a bit different because by default,
> string is a pointer to char or the address of the first char in C and C
> ++. So it is like passing in the address already.


No. A string in C++ is a string, a char array or a pointer to a char is
something different.

> it is when the
> argument n is something like 1 that makes me wonder.



Get a good book on whatever language you are interested in (I do not
know which it is since you are all over the place) and read up on that
languages references, if you still do not understand after that ask your
questions in the group discussing that language.

--
Erik Wikström
 
Reply With Quote
 
Michael Fesser
Guest
Posts: n/a
 
      09-30-2007
..oO(Summercool)

>I think in Pascal and C, we can never have an
>argument modified unless we explicitly allow it, by passing in the
>pointer (address) of the argument.


Pascal also allows passing by reference, which is done with the keyword
'var' when declaring the function parameters. Object Pascal also allows
const parameters.

Micha
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
which language allows you to change an argument's value? Summercool Java 29 10-10-2007 08:35 AM
which language allows you to change an argument's value? Summercool C++ 29 10-10-2007 08:35 AM
which language allows you to change an argument's value? Summercool Python 17 10-06-2007 10:08 PM
"Your drive allows you to change the region 4 more time(s)." Michael Mason DVD Video 4 02-22-2006 03:26 AM
Which digital camera allows you infinite record time when connected to a computer? Anoushka Digital Photography 5 03-03-2004 11:46 AM



Advertisments