Home » Coding » c++ » which language allows you to change an argument's value?
which language allows you to change an argument's value? [message #16606] Sun, 30 September 2007 06:47 Go to next message
Summercool  is currently offline Summercool
Messages: 17
Registered: September 2007
Junior Member
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?
Re: which language allows you to change an argument's value? [message #16608 is a reply to message #16606 ] Sun, 30 September 2007 06:58 Go to previous messageGo to next message
rolkA  is currently offline rolkA
Messages: 5
Registered: September 2007
Junior Member
On 30 sep, 12:47, Summercool <Summercooln...@gmail.com> 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.
Re: which language allows you to change an argument's value? [message #16613 is a reply to message #16606 ] Sun, 30 September 2007 07:50 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
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
Re: which language allows you to change an argument's value? [message #16632 is a reply to message #16606 ] Sun, 30 September 2007 12:49 Go to previous messageGo to next message
Summercool  is currently offline Summercool
Messages: 17
Registered: September 2007
Junior Member
On Sep 30, 4:18 am, 7stud -- <dol...@excite.com> 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.
Re: which language allows you to change an argument's value? [message #16633 is a reply to message #16606 ] Sun, 30 September 2007 13:12 Go to previous messageGo to next message
Daniel Pitts  is currently offline Daniel Pitts
Messages: 3
Registered: August 2007
Junior Member
On Sep 30, 3:47 am, Summercool <Summercooln...@gmail.com> 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)
Re: which language allows you to change an argument's value? [message #16637 is a reply to message #16632 ] Sun, 30 September 2007 13:53 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
On 2007-09-30 18:49, Summercool wrote:
> On Sep 30, 4:18 am, 7stud -- <dol...@excite.com> 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
Re: which language allows you to change an argument's value? [message #16638 is a reply to message #16632 ] Sun, 30 September 2007 13:55 Go to previous messageGo to next message
Jerry Stuckle  is currently offline Jerry Stuckle
Messages: 4
Registered: September 2007
Junior Member
Summercool wrote:
> On Sep 30, 4:18 am, 7stud -- <dol...@excite.com> 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.
>

You need to get more C++ books :-). You generally won't find them in
basic books, but some of the more advanced ones talk about function
signatures.

A C++ function's signature is dependent on the function name, number of
parameters being passed, and the type of each parameter. This is passed
onto the linker. In any C++ program, every function signature must be
unique.

But in this case he's a little incorrect. You *could* look at the
function's signature, but it's much easier just to look at the
function's declaration.

> 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.
>
>


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: which language allows you to change an argument's value? [message #16644 is a reply to message #16632 ] Sun, 30 September 2007 15:15 Go to previous messageGo to next message
Michael Fesser  is currently offline Michael Fesser
Messages: 2
Registered: September 2007
Junior Member
..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
Re: which language allows you to change an argument's value? [message #16647 is a reply to message #16606 ] Sun, 30 September 2007 15:36 Go to previous messageGo to next message
Bryan Olson  is currently offline Bryan Olson
Messages: 42
Registered: August 2007
Member
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
> their reference (alias) mechanism. And C, Python, and Ruby probably
> won't let you do that. What about Java and Perl?

I think you've missed how Python works, and probably others.
A Python function receives a reference to the argument, and
can modify the object if the object is mutable.

Nevertheless, assigning to the parameter's name will not
change the passed object. This function does nothing:

def clear_list_wrong(lst):
lst = [] # useless re-binding of local name

This version empties the passed list:

def clear_list(lst):
del lst[:]


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

Sure. First choice: Don't change the value in the function.
Alternatives include making a copy to use as the argument.

> 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.

Don't rely on undocumented behavior. Modules worth using are by
good programmers. Authors of library modules tend to be zealous
about not breaking client code.


--
--Bryan
Re: which language allows you to change an argument's value? [message #16649 is a reply to message #16637 ] Sun, 30 September 2007 15:49 Go to previous messageGo to next message
Jerry Stuckle  is currently offline Jerry Stuckle
Messages: 4
Registered: September 2007
Junior Member
Erik Wikström wrote:
> On 2007-09-30 18:49, Summercool wrote:
>> On Sep 30, 4:18 am, 7stud -- <dol...@excite.com> 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.
>

No, the function's signature does not include the return type.

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

Actually, these are prototypes. The signature is what's passed onto the
linker - perhaps something like "?foo@@YAXHM>@Z". It's also generally
compiler dependent for C++ mangled names.

Also, I should clarify - the C++ function signature includes parameter
information. The C function signature is only dependent on the function
name and is almost always the name preceded by an underscore).

You can see the function signatures in a map file if you leave the names
mangled.
..
>> 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?
>

Exactly.


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Re: which language allows you to change an argument's value? [message #16652 is a reply to message #16613 ] Sun, 30 September 2007 17:03 Go to previous messageGo to next message
arne  is currently offline arne
Messages: 7
Registered: August 2007
Junior Member
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
Re: which language allows you to change an argument's value? [message #16662 is a reply to message #16606 ] Sun, 30 September 2007 21:53 Go to previous messageGo to next message
Roedy Green  is currently offline Roedy Green
Messages: 12
Registered: August 2007
Junior Member
On Sun, 30 Sep 2007 10:47:13 -0000, Summercool
<Summercoolness@gmail.com> wrote, 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
Re: which language allows you to change an argument's value? [message #16678 is a reply to message #16662 ] Mon, 01 October 2007 04:17 Go to previous messageGo to next message
Dennis Lee Bieber  is currently offline Dennis Lee Bieber
Messages: 55
Registered: August 2007
Member
On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
<see_website@mindprod.com.invalid> declaimed 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

)
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/
Re: which language allows you to change an argument's value? [message #16688 is a reply to message #16652 ] Mon, 01 October 2007 05:15 Go to previous messageGo to next message
Sascha Bohnenkamp  is currently offline Sascha Bohnenkamp
Messages: 3
Registered: October 2007
Junior Member
> 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.
Re: which language allows you to change an argument's value? [message #16700 is a reply to message #16652 ] Mon, 01 October 2007 06:52 Go to previous messageGo to next message
Erik-wikstrom  is currently offline Erik-wikstrom
Messages: 412
Registered: July 2007
Senior Member
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
Re: which language allows you to change an argument's value? [message #16707 is a reply to message #16632 ] Mon, 01 October 2007 08:25 Go to previous messageGo to next message
James Kanze  is currently offline James Kanze
Messages: 598
Registered: July 2007
Senior Member
On Sep 30, 6:49 pm, Summercool <Summercooln...@gmail.com> wrote:
> On Sep 30, 4:18 am, 7stud -- <dol...@excite.com> 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.

That's not really correct. It's more a case that if you don't
know what a function does, you don't use it. Regardless of the
language, calling a function without knowing what it does is a
sure recepe for disaster.

> 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.

In C, that's partially true; no part of the expressions involved
in the function invocation will be modified through the function
argument unless the address is specifically taken (or the
argument has array type). The situation in Pascal, C++, Ada and
most other languages is different; Pascal and C++ have reference
parameters (called VAR in Pascal), as well as value paramters,
Ada has in, inout and out parameters.

The situation in Java is somewhat more complicated; formally,
Java only has pass by value, and there's no way a function can
modify any of the arguments. In practice, however, if the
expression has an object type, the "value" is a pointer (called
reference in Java) to the object; if the object is a well
defined "value" type (e.g. java.lang.String), it will be
immutable, but entity objects don't follow this rule, and not
all value objects (e.g. java.awt.Dimension) are well designed.

> 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
> ++.

Strings in C++ are class types, with full value semantics.

> So it is like passing in the address already. it is when the
> argument n is something like 1 that makes me wonder.

C++ requires an lvalue unless the reference is to a const, so
you cannot pass 1 to a C++ function and expect/worry about it
being changed. The same holds for most modern languages, I
think, although the mechanisms involved may differ. (I'd be
very surprised, for example, if Ada allowed anything but what in
C++ would be an lvalue to be passed to an inout or an out
parameter.) In most modern languages, however, you can
circumvent the controls with enough indirections.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Re: which language allows you to change an argument's value? [message #16741 is a reply to message #16606 ] Mon, 01 October 2007 13:28 Go to previous messageGo to next message
Scott Gifford  is currently offline Scott Gifford
Messages: 19
Registered: September 2007
Junior Member
Summercool <Summercoolness@gmail.com> writes:

> 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.
Re: which language allows you to change an argument's value? [message #17055 is a reply to message #16678 ] Wed, 03 October 2007 18:35 Go to previous messageGo to next message
jwkenne  is currently offline jwkenne
Messages: 6
Registered: September 2007
Junior Member
Dennis Lee Bieber wrote:
> On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
> <see_website@mindprod.com.invalid> declaimed 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 naïve as the fear that ultimate power inevitably
corrupts."
-- James D. Barber (1930-2004)
Re: which language allows you to change an argument's value? [message #17250 is a reply to message #17055 ] Fri, 05 October 2007 08:36 Go to previous messageGo to next message
blmblm  is currently offline blmblm
Messages: 2
Registered: October 2007
Junior Member
In article <1PUMi.21$WQ1.20@newsfe12.lga>,
John W. Kennedy <jwkenne@attglobal.net> wrote:
> Dennis Lee Bieber wrote:
> > On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
> > <see_website@mindprod.com.invalid> declaimed 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.
Re: which language allows you to change an argument's value? [message #17304 is a reply to message #17250 ] Fri, 05 October 2007 16:57 Go to previous messageGo to next message
jwkenne  is currently offline jwkenne
Messages: 6
Registered: September 2007
Junior Member
blmblm@myrealbox.com wrote:
> In article <1PUMi.21$WQ1.20@newsfe12.lga>,
> John W. Kennedy <jwkenne@attglobal.net> wrote:
>> 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
Re: which language allows you to change an argument's value? [message #17362 is a reply to message #17304 ] Sat, 06 October 2007 12:51 Go to previous messageGo to next message
blmblm  is currently offline blmblm
Messages: 2
Registered: October 2007
Junior Member
In article <mzxNi.27$wu6.11@newsfe12.lga>,
John W. Kennedy <jwkenne@attglobal.net> wrote:
> blmblm@myrealbox.com wrote:
> > In article <1PUMi.21$WQ1.20@newsfe12.lga>,
> > John W. Kennedy <jwkenne@attglobal.net> wrote:
> >> 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.
Re: which language allows you to change an argument's value? [message #17370 is a reply to message #16662 ] Sat, 06 October 2007 18:08 Go to previous messageGo to next message
jwkenne  is currently offline jwkenne
Messages: 6
Registered: September 2007
Junior Member
Roedy Green wrote:
> On Sun, 30 Sep 2007 10:47:13 -0000, Summercool
> <Summercoolness@gmail.com> wrote, 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/Double%20Falshood/index.html
Re: which language allows you to change an argument's value? [message #17388 is a reply to message #17055 ] Sun, 07 October 2007 06:05 Go to previous messageGo to next message
James Kanze  is currently offline James Kanze
Messages: 598
Registered: July 2007
Senior Member
On Oct 4, 12:35 am, "John W. Kennedy" <jwke...@attglobal.net> wrote:
> Dennis Lee Bieber wrote:
> > On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
> > <see_webs...@mindprod.com.invalid> declaimed 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:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Re: which language allows you to change an argument's value? [message #17389 is a reply to message #17388 ] Sun, 07 October 2007 06:10 Go to previous messageGo to next message
alfps  is currently offline alfps
Messages: 315
Registered: July 2007
Senior Member
* James Kanze:
> On Oct 4, 12:35 am, "John W. Kennedy" <jwke...@attglobal.net> wrote:
>> Dennis Lee Bieber wrote:
>>> On Mon, 01 Oct 2007 01:53:47 GMT, Roedy Green
>>> <see_webs...@mindprod.com.invalid> declaimed 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.

This is all to simple. Please discuss Algol pass-by-name. Much more
interesting, and a good candidate for inclusion in C++ -- at least, if
we're going to achieve the goal of PL/1-killer! :-)

Cheers,

- Alf (off-topic)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Re: which language allows you to change an argument's value? [message #17406 is a reply to message #17389 ] Sun, 07 October 2007 14:59 Go to previous messageGo to next message
Jerry Coffin  is currently offline Jerry Coffin
Messages: 135
Registered: August 2007
Senior Member
In article <13ghc3ros2h3231@corp.supernews.com>, alfps@start.no says...

[ ... ]

> This is all to simple. Please discuss Algol pass-by-name. Much more
> interesting, and a good candidate for inclusion in C++ -- at least, if
> we're going to achieve the goal of PL/1-killer! :-)

The Algol 60 standard is deceptively simple looking. The entire "Revised
Report on the Algorithmic Language" is only 17 pages -- about half as
long as the _index_ of the 2003 version of the C++ standard! Of that,
about a page and a half is devoted to a history of the committee
meetings and such.

Anyway, Algol supported both call by value and call by name. Since it's
raeasonably short I'll quote the entire section:

// Start of quote

4.7.3.1. Value assignment (call by value)

All formal parameters quoted in the value part of the procedure
declaration heading are assigned the values (cf. section 2.8 Values and
Types) of the corresponding actual parameters,these assignments being
considredd as being performed explicitly before entering the procedure
body. The effect is a though an aditional block embracing the procedure
body were created in which these assignments were made to variables
local to this fictitious block with types as given in their
corresponding specifications (cf. section 5.4.5). As a consequence,
variables called by value ar considered as nonlocal to the body of the
procedure, but local to the fictitious block (cf. section 5.4.3).

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.

// End of quote

If you're going to compare to C++, 'export' virtually springs to mind --
something that initially seems fairly inoccuous, but is virtually
impossible to implement, and probably doesn't accomplish what you want
when/if you do manage to get it "right".

--
Later,
Jerry.

The universe is a figment of its own imagination.
Re: which language allows you to change an argument's value? [message #17418 is a reply to message #17406 ] Mon, 08 October 2007 00:58 Go to previous messageGo to next message
bbound  is currently offline bbound
Messages: 2
Registered: October 2007
Junior Member
On Oct 7, 2:59 pm, Jerry Coffin <jcof...@taeus.com> wrote:
> 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?
Re: which language allows you to change an argument's value? [message #17419 is a reply to message #17418 ] Mon, 08 October 2007 01:09 Go to previous message
Lew  is currently offline Lew
Messages: 17
Registered: August 2007
Junior Member
Jerry Coffin wrote:
>> The universe is a figment of its own imagination.

bbound@gmail.com wrote:
> I can't recall -- what was the formal name in philosophy for this
> thesis?

Zen.

--
Lew
Previous Topic:Legal to override same function in different bases?
Next Topic:Free books
Goto Forum:
  


Current Time: Fri May 16 02:41:54 EDT 2008

Total time taken to generate the page: 0.16973 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 2.7.7.
Copyright ©2001-2007 FUD Forum Bulletin Board Software