By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,730 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,730 IT Pros & Developers. It's quick & easy.

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

P: n/a

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
Share this Question
Share on Google+
17 Replies


P: n/a
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

P: n/a
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

P: n/a
..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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Dennis Lee Bieber wrote:
On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
<se*********@mindprod.com.invaliddeclaimed the following in
comp.lang.python:

>Seem to me you could in FORTRAN and Pascal, and maybe even Algol. It
has been a while.
Everything in classic FORTRAN is a passed as a reference -- even
constant arguments are passed as a reference to the memory location
containing that constant (which is why it was possible in very early
FORTRANs to have "a = 1 + 1" yield something other than "2" if preceded
by, say, "call mutate(1)" where mutate looks like:

subroutine mutate(arg)
arg = arg * 2
end

)
However, some implementations passed /and returned/ elementary arguments
by value, as Ada does. (The object code was typically faster that way,
and FORTRAN semantics were such that the difference was almost
impossible to observe.)
--
John W. Kennedy
"The pathetic hope that the White House will turn a Caligula into a
Marcus Aurelius is as nave as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Oct 3 '07 #10

P: n/a
In article <1P*************@newsfe12.lga>,
John W. Kennedy <jw*****@attglobal.netwrote:
Dennis Lee Bieber wrote:
On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
<se*********@mindprod.com.invaliddeclaimed the following in
comp.lang.python:

Seem to me you could in FORTRAN and Pascal, and maybe even Algol. It
has been a while.
Everything in classic FORTRAN is a passed as a reference -- even
constant arguments are passed as a reference to the memory location
containing that constant (which is why it was possible in very early
FORTRANs to have "a = 1 + 1" yield something other than "2" if preceded
by, say, "call mutate(1)" where mutate looks like:

subroutine mutate(arg)
arg = arg * 2
end

)

However, some implementations passed /and returned/ elementary arguments
by value, as Ada does.
In FORTRAN/Fortran, don't they call that "copy-in/copy-out"? Same
thing, just nitpicking a little about terminology, I hope correctly.
(The object code was typically faster that way,
and FORTRAN semantics were such that the difference was almost
impossible to observe.)
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Oct 5 '07 #11

P: n/a
bl****@myrealbox.com wrote:
In article <1P*************@newsfe12.lga>,
John W. Kennedy <jw*****@attglobal.netwrote:
>However, some implementations passed /and returned/ elementary arguments
by value, as Ada does.
In FORTRAN/Fortran, don't they call that "copy-in/copy-out"? Same
thing, just nitpicking a little about terminology, I hope correctly.
Back in the day, IBM called it "by copy". I'm not acquainted with modern
versions of the language.
--
John W. Kennedy
"The whole modern world has divided itself into Conservatives and
Progressives. The business of Progressives is to go on making mistakes.
The business of the Conservatives is to prevent the mistakes from being
corrected."
-- G. K. Chesterton
Oct 5 '07 #12

P: n/a
In article <mz*************@newsfe12.lga>,
John W. Kennedy <jw*****@attglobal.netwrote:
bl****@myrealbox.com wrote:
In article <1P*************@newsfe12.lga>,
John W. Kennedy <jw*****@attglobal.netwrote:
However, some implementations passed /and returned/ elementary arguments
by value, as Ada does.
In FORTRAN/Fortran, don't they call that "copy-in/copy-out"? Same
thing, just nitpicking a little about terminology, I hope correctly.

Back in the day, IBM called it "by copy". I'm not acquainted with modern
versions of the language.
Huh. I'd have said I was remembering the terminology from back
when I was actively using the language, when it was still FORTRAN
and my then-current place of employment still regarded the F77
standard as a bit too new-fangled to be relied on. But I'm sure
your memory's at least as good as mine, and I can't quite figure
out how to research which term might have been used when ....

--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
Oct 6 '07 #13

P: n/a
On Oct 4, 12:35 am, "John W. Kennedy" <jwke...@attglobal.netwrote:
Dennis Lee Bieber wrote:
On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
<see_webs...@mindprod.com.invaliddeclaimed the following in
comp.lang.python:
Seem to me you could in FORTRAN and Pascal, and maybe even Algol. It
has been a while.
Everything in classic FORTRAN is a passed as a reference -- even
constant arguments are passed as a reference to the memory location
containing that constant (which is why it was possible in very early
FORTRANs to have "a = 1 + 1" yield something other than "2" if preceded
by, say, "call mutate(1)" where mutate looks like:
subroutine mutate(arg)
arg = arg * 2
end
)
However, some implementations passed /and returned/ elementary
arguments by value, as Ada does. (The object code was
typically faster that way, and FORTRAN semantics were such
that the difference was almost impossible to observe.)
The Fortran standard is carefully worded to allow either pass by
reference, or copy-in, copy-out. IIRC, IBM's Fortran-H used
copy-in/copy-out, but most others used call by reference.

Straight copy was never allowed by the standard, and I've never
heard of a pre-standard implementation which used it either.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Oct 7 '07 #14

P: n/a
On Oct 7, 2:59 pm, Jerry Coffin <jcof...@taeus.comwrote:
4.7.3.2. Name Replacement (call by name)

Any formal parameter not quoted in the value list is replaced,
throughout the procedure body, by the corresponding actual parameter,
after enclosing this latter in parentheses wherever syntactically
possible. Possible conflicts between identifiers inserted through this
process and other identifiers already present within the procedure will
be avoided by suitable systematic changes of the formal or local
identifiers involved.
This sounds rather like macro expansion as found in languages ranging
from TeX to various flavors of shell and make.
The universe is a figment of its own imagination.
I can't recall -- what was the formal name in philosophy for this
thesis?

Oct 8 '07 #15

P: n/a
Lew
Jerry Coffin wrote:
>The universe is a figment of its own imagination.
bb****@gmail.com wrote:
I can't recall -- what was the formal name in philosophy for this
thesis?
Zen.

--
Lew
Oct 8 '07 #16

P: n/a
On Oct 8, 4:14 am, James Kanze <james.ka...@gmail.comwrote:
On Oct 8, 6:58 am, bbo...@gmail.com wrote:
On Oct 7, 2:59 pm, Jerry Coffin <jcof...@taeus.comwrote:
4.7.3.2. Name Replacement (call by name)
Any formal parameter not quoted in the value list is replaced,
throughout the procedure body, by the corresponding actual parameter,
after enclosing this latter in parentheses wherever syntactically
possible. Possible conflicts between identifiers inserted through this
process and other identifiers already present within the procedure will
be avoided by suitable systematic changes of the formal or local
identifiers involved.
This sounds rather like macro expansion as found in languages ranging
from TeX to various flavors of shell and make.

Not really. [snip loads]
You seem to be meaning to disagree with me, but it's unclear exactly
what you are asserting that contradicts anything that I said.

Oct 10 '07 #17

P: n/a
nebulou...@gmail.com wrote:
On Oct 8, 4:14 am, James Kanze <james.ka...@gmail.comwrote:
On Oct 8, 6:58 am, bbo...@gmail.com wrote:
On Oct 7, 2:59 pm, Jerry Coffin <jcof...@taeus.comwrote:
4.7.3.2. Name Replacement (call by name)
Any formal parameter not quoted in the value list is replaced,
throughout the procedure body, by the corresponding actual parameter,
after enclosing this latter in parentheses wherever syntactically
possible. Possible conflicts between identifiers inserted through this
process and other identifiers already present within the procedure will
be avoided by suitable systematic changes of the formal or local
identifiers involved.
This sounds rather like macro expansion as found in languages ranging
from TeX to various flavors of shell and make.
Not really. [snip loads]
You seem to be meaning to disagree with me,
Yes and no. Algol's name replacement is definitly different
than the usual macro replacement in shells, make and TeX,
since the argument gets expanded each time it is used, and
not once when the function is called. On the other hand,
such languages often do have the capacity to delay
expansion; they also have the capability of treating any
data string as part of the program, so you can pass an
argument as a string, and then execute it multiple times in
a function. In short, you can easily simulate Algol's name
replacement in such languages.

Of course, if you pass a functional object in C++, you can
also get much of the same effect as well.

--
James Kanze (GABI Software) mailto:ja*********@gmail.com
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34

Oct 10 '07 #18

This discussion thread is closed

Replies have been disabled for this discussion.