473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
17 1686
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.comwrite s:
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
Dennis Lee Bieber wrote:
On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
<se*********@mi ndprod.com.inva liddeclaimed the following in
comp.lang.pytho n:

>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 naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Oct 3 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
1997
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 appropriate place to post this, please let me know. So, here are a few things I'd like to see. 1. A way to declare a local variable as being readonly.
17
3981
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 http://forta.com/books/0672325667/
9
1153
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
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9524
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.