471,348 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

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


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?

Sep 30 '07 #1
9 1078
On 30 sep, 12:47, Summercool <Summercooln...@gmail.comwrote:
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.

Sep 30 '07 #2
On Sep 30, 3:47 am, Summercool <Summercooln...@gmail.comwrote:
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)

Sep 30 '07 #3
..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
Sep 30 '07 #4
Erik Wikström wrote:
>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.
Neither C or Java has call by reference.

C pointers and Java references may work similarly in most cases
but it is still call by value.
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.
I can not see why OO should indicate anything about call by reference
support.
>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.
Usually it is not a good thing, because it makes the code much
more difficult to read.

But sometimes it is handy.

I think C# got it right.

It allows it but require an explicit marking of it in both formal
argument list and actual argument list.
>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?
C++ and Java are very different in this aspect.

Arne
Sep 30 '07 #5
On Sun, 30 Sep 2007 10:47:13 -0000, Summercool
<Su************@gmail.comwrote, quoted or indirectly quoted someone
who said :
>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?
Seem to me you could in FORTRAN and Pascal, and maybe even Algol. It
has been a while.

I have come to appreciate Java's strong isolation convention. It makes
it a lot easier to track down WHERE in the code a value could
potentially change and keeps those places to a minimum.

--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Oct 1 '07 #6
Neither C or Java has call by reference.
C pointers and Java references may work similarly in most cases
but it is still call by value.
so what? the references in c++ are passed by value too, its just a nice
interface to pointers.
At the end those parameters are pushed on the stack .. thats it.
Oct 1 '07 #7
On 2007-09-30 23:03, Arne Vajhøj wrote:
Erik Wikström wrote:
>>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.

Neither C or Java has call by reference.
I never said that, what I said was that C allows you to simulate it by
passing pointers instead, and since you always use references in Java
(except for primitive types) you always pass a reference.
C pointers and Java references may work similarly in most cases
but it is still call by value.
The difference I am trying to show is that between value semantics
(where a copy of the parameter is used in the function) verses reference
semantics where you pass a reference to the object. It is true that when
using a pointer in C you pass a copy of the pointer, but not of the
object it refers to. Similarly you might pass a copy of the variable
holding a reference in Java, but not a copy of the object it refers to.
Thus, in the function you will be working on the referred object, just
like when passing by reference in C++.
>know about Ruby, Python and Perl, but (AFAIK) the all have OO support so
I would be surprised if they used purely value semantics.

I can not see why OO should indicate anything about call by reference
support.
There is no direct connection, except that all OO programming languages
that I know of supports reference semantics. You seldom want to work on
copies of objects, rather you want to work on the original object and
for that you need to be able to refer to it, for that you need some kind
of reference.
>>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.

Usually it is not a good thing, because it makes the code much
more difficult to read.

But sometimes it is handy.

I think C# got it right.

It allows it but require an explicit marking of it in both formal
argument list and actual argument list.
I think you are confusing two different things here, one is the ability
to pass a reference to an object, instead of a copy of the object. This
is what C++ references allows you to do, just like Java references, C
pointers, and any other kind of references that I know about.

The other thing is to allow the function to change what a reference
refers to, to this not possible with C++ references, C pointers, Java
references, or most other reference types that I know about. You can do
this by using a reference to a pointer in C++, a pointer to a pointer in
C, or using the ref and out keywords in C#. Doing that can be confusing,
but often the semantics for doing so are pretty obvious.
>>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?

C++ and Java are very different in this aspect.
Might be, my question was about what aspect we are talking about.

--
Erik Wikström
Oct 1 '07 #8
Summercool <Su************@gmail.comwrites:
I wonder which language allows you to change an argument's value?
[...]
What about Java and Perl?
Perl will let you change the value of a passed-in object directly.
Others have already answered about Java.
is there any way to prevent a function from changing the argument's
value?
Make a copy of the object, and pass in the copy.

Without making a copy, of the languages I know, C++ comes closest to
supporting an unmodifiable argument passed by reference. Using a
const reference or const pointer indicates that the reference won't be
changed, but even that can be subverted by the function's author with
casting.

-----Scott.
Oct 1 '07 #9
Roedy Green wrote:
On Sun, 30 Sep 2007 10:47:13 -0000, Summercool
<Su************@gmail.comwrote, quoted or indirectly quoted someone
who said :
>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?
Seem to me you could in FORTRAN and Pascal, and maybe even Algol.
FORTRAN generally looked like call by reference, but was often actually
implemented element variables as copy-in at call time and copy-out at
return time. In standard older FORTRAN, the effective results were the
same. I don't know how things stand with modern Fortran, where some new
features have made the assumption that copy-in-copy-out is equivalent to
reference more dangerous. Ada is /defined/ as using copy-in-copy-out for
element variables.

ALGOL had a weird "by name" convention. The semantics were essentially
that, for each argument, a closure (historically called a "thunk") was
constructed, and every time the parameter was referenced, the
corresponding closure was called. Theoretically elegant, but hideous to
implement, and with bizarre side-effects in certain cases.
--
John W. Kennedy
Read the remains of Shakespeare's lost play, now annotated!
http://pws.prserv.net/jwkennedy/Doub...ood/index.html
Oct 6 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.