473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1138
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Andrew | last post by:
Hello, I understand that this is a good place to post suggestions about future changes to the language, and that the Microsoft developers who develop C# read this newsgroup. If there is a more...
17
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
17
by: Summercool | last post by:
I wonder which language allows you to change an argument's value? like: foo(&a) { a = 3 } n = 1 print n
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.