Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Could you explain pass by value-result problem??

49 views
Skip to first unread message

kin...@gmail.com

unread,
Jun 5, 2007, 10:59:02 AM6/5/07
to
Hello,
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism

1) pass by value (inother words : call by value)
2) pass by reference (inother words: call by reference)
3) pass by value-result <- i have question this subject .
4) pass by name


pass by value-result
passing mechanism
1.The values of the arguments are copied into the fomal parameters.
2.The final values of the parameters are copied back out to the
arguments.
Characteristics of the Pass by Value-Result
-AKA copy-in , copy-out(copy-restore)
-no alias problem occurs(differ to the pass by reference in this
point)
-The order of copying the results may be important


ex)
Assume: pass by value-result
void square(int x,int y)
{
x*=x;
y*=y;

}


main()
{
int a=5;
int b=10;
square(a,b);
printf("a = %d b = %d\n",a,b);


}


output:

a=25 b=100


can understand! but what about this?


void p(int x,int y)
{
int sum,prod;
sum=x+y;
prod=x*x;
x=sum; //sum will be returned via x
y=prod; // prod will be returned via y


}


main()
{
int a=5;
p(a,a); // a == ??


}


What value of a?

Thanks to read.

Pascal Bourguignon

unread,
Jun 5, 2007, 1:01:32 PM6/5/07
to
kin...@gmail.com writes:

> main()
> {
> int a=5;
> p(a,a); // a == ??
> }
>
>
> What value of a?

That's the problem! We cannot know what the value of a will be,
because it's not obvious. It depends on the order in which the
results are copied back to a. Notably, it doesn't depend on the order
of the x=sum;y=prod; assignments in the procedure, since result
copying is done only when the procedure _returns_. If the first
result is copied back first, then a will get the product. If the
first result is copied last, then a will get the sum.
That's exactly the value-result problem.

Note that most programming languages don't define the order in which
the arguments are evaluated, or results returned, including C, Scheme,
etc... and leave it up to the implementation to choose the order (it
may not even be always the same since this freedom may allow the
compiler to optimize even more some function calls).

--
__Pascal Bourguignon__ http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.

Steve O'Hara-Smith

unread,
Jun 5, 2007, 11:21:55 AM6/5/07
to
On Tue, 05 Jun 2007 07:59:02 -0700
kin...@gmail.com wrote:

> pass by value-result

Note this:

> -The order of copying the results may be important

Think clearly about it and your problem will be solved.

--
C:>WIN | Directable Mirror Arrays
The computer obeys and wins. | A better way to focus the sun
You lose and Bill collects. | licences available see
| http://www.sohara.org/

CBFalconer

unread,
Jun 5, 2007, 7:08:43 PM6/5/07
to
kin...@gmail.com wrote:
>
> now I'm learning progamming language in university.
> but i have some question.
> in textbook. says there are four passing Mechanism
>
> 1) pass by value (inother words : call by value)
> 2) pass by reference (inother words: call by reference)
> 3) pass by value-result <- i have question this subject .
> 4) pass by name
>
> pass by value-result
> passing mechanism
> 1.The values of the arguments are copied into the fomal parameters.
> 2.The final values of the parameters are copied back out to the
> arguments.

Wrong. The final values are discarded.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net


--
Posted via a free Usenet account from http://www.teranews.com

Ben Bacarisse

unread,
Jun 6, 2007, 12:05:21 AM6/6/07
to
CBFalconer <cbfal...@yahoo.com> writes:

> kin...@gmail.com wrote:
>>
>> now I'm learning progamming language in university.
>> but i have some question.
>> in textbook. says there are four passing Mechanism
>>
>> 1) pass by value (inother words : call by value)
>> 2) pass by reference (inother words: call by reference)
>> 3) pass by value-result <- i have question this subject .
>> 4) pass by name
>>
>> pass by value-result
>> passing mechanism
>> 1.The values of the arguments are copied into the fomal parameters.
>> 2.The final values of the parameters are copied back out to the
>> arguments.
>
> Wrong. The final values are discarded.

What, then, is the difference between this and pass by value?

--
Ben.

CBFalconer

unread,
Jun 6, 2007, 3:13:55 AM6/6/07
to

Nothing. That is "pass by value". How do you return a result when
the original value was the result of an expression, for example?

Chris Dollin

unread,
Jun 6, 2007, 4:58:13 AM6/6/07
to
CBFalconer wrote:

> Ben Bacarisse wrote:
>> CBFalconer <cbfal...@yahoo.com> writes:
>>> kin...@gmail.com wrote:
>>>>
>>>> now I'm learning progamming language in university.
>>>> but i have some question.
>>>> in textbook. says there are four passing Mechanism
>>>>
>>>> 1) pass by value (inother words : call by value)
>>>> 2) pass by reference (inother words: call by reference)
>>>> 3) pass by value-result <- i have question this subject .
>>>> 4) pass by name
>>>>
>>>> pass by value-result
>>>> passing mechanism
>>>> 1.The values of the arguments are copied into the fomal parameters.
>>>> 2.The final values of the parameters are copied back out to the
>>>> arguments.
>>>
>>> Wrong. The final values are discarded.
>>
>> What, then, is the difference between this and pass by value?
>
> Nothing. That is "pass by value". How do you return a result when
> the original value was the result of an expression, for example?

I would expect the language to disallow the call in that case.
As an alternative, it could write the expression value into
a temp and use that. There are several options and, I suspect,
a [1] language for each of them.

[1] For arbitrarily large values of 1.

--
The shortcuts are all full of people using them.

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Ben Bacarisse

unread,
Jun 6, 2007, 10:07:41 AM6/6/07
to
CBFalconer <cbfal...@yahoo.com> writes:

> Ben Bacarisse wrote:
>> CBFalconer <cbfal...@yahoo.com> writes:
>>> kin...@gmail.com wrote:
>>>>
>>>> now I'm learning progamming language in university.
>>>> but i have some question.
>>>> in textbook. says there are four passing Mechanism
>>>>
>>>> 1) pass by value (inother words : call by value)
>>>> 2) pass by reference (inother words: call by reference)
>>>> 3) pass by value-result <- i have question this subject .
>>>> 4) pass by name
>>>>
>>>> pass by value-result
>>>> passing mechanism
>>>> 1.The values of the arguments are copied into the fomal parameters.
>>>> 2.The final values of the parameters are copied back out to the
>>>> arguments.
>>>
>>> Wrong. The final values are discarded.
>>
>> What, then, is the difference between this and pass by value?
>
> Nothing. That is "pass by value".

There is a parameter passing mechanism, conventionally called pass by
value-result, that is different from passing by value, and it behaves
as the OP described it. Are you saying that what the OP is being
taught is wrong -- that there is no such mechanism?

> How do you return a result when
> the original value was the result of an expression, for example?

The same question applies to call by reference. The usual solution is
that the compiler only allows expressions that denote lvalues to be
used in such situations.

Your sig (as my news feed sees it) is either 9 lines long, or the
links you keep posting are not in your sig. Would you consider
trimming to changing it/them?

--
Ben.

CBFalconer

unread,
Jun 6, 2007, 11:45:08 AM6/6/07
to
Ben Bacarisse wrote:
> CBFalconer <cbfal...@yahoo.com> writes:
>
... snip ...

>
> There is a parameter passing mechanism, conventionally called pass by
> value-result, that is different from passing by value, and it behaves
> as the OP described it. Are you saying that what the OP is being
> taught is wrong -- that there is no such mechanism?
>
>> How do you return a result when the original value was the result
>> of an expression, for example?
>
> The same question applies to call by reference. The usual solution
> is that the compiler only allows expressions that denote lvalues to
> be used in such situations.

Then use pass-by-reference. The so-called "value-result" gains
nothing at all. It does expend extra effort, in that the caller
has to remember and address and do an extra final transfer.

If you really want to enhance complexity, consider pass-by-name.

Stuart

unread,
Jun 6, 2007, 1:00:15 PM6/6/07
to
"CBFalconer" <cbfal...@yahoo.com> wrote in message
news:4666D684...@yahoo.com...

> Ben Bacarisse wrote:
>> CBFalconer <cbfal...@yahoo.com> writes:
>>
> ... snip ...
>>
>> There is a parameter passing mechanism, conventionally called pass by
>> value-result, that is different from passing by value, and it behaves
>> as the OP described it. Are you saying that what the OP is being
>> taught is wrong -- that there is no such mechanism?
>>
>>> How do you return a result when the original value was the result
>>> of an expression, for example?
>>
>> The same question applies to call by reference. The usual solution
>> is that the compiler only allows expressions that denote lvalues to
>> be used in such situations.
>
> Then use pass-by-reference. The so-called "value-result" gains
> nothing at all. It does expend extra effort, in that the caller
> has to remember and address and do an extra final transfer.

It does allow certain issues concerning aliasing of parameters to be
resolved (though anyone doing this deserves the world of hurt they might
get). Consider:

procedure P(X : in T;
Y : in out T) is
C : constant T := {whatever};
begin
Y := Y + C;
Y := Y + X;
end P;

...
P(A, A);

Passing by reference could give
A = (A'+C)+(A'+C), where A' is the original value of A
whereas passing by value-result gives
A = (A'+C)+A'
which would be more in keeping with the original behaviour of the
subprogram.

Whether using pass-by-reference is simpler or not does not eliminate the
existence of this other mechanism. A reasonable language would not burden
the user with the details of the passing mechanism. The choice of mechanism
would be, in a large part, driven by the rules of the language being used.

--
Stuart


Chris Dollin

unread,
Jun 7, 2007, 4:29:48 AM6/7/07
to
CBFalconer wrote:

> Ben Bacarisse wrote:
>> CBFalconer <cbfal...@yahoo.com> writes:
>>
> ... snip ...
>>
>> There is a parameter passing mechanism, conventionally called pass by
>> value-result, that is different from passing by value, and it behaves
>> as the OP described it. Are you saying that what the OP is being
>> taught is wrong -- that there is no such mechanism?
>>
>>> How do you return a result when the original value was the result
>>> of an expression, for example?
>>
>> The same question applies to call by reference. The usual solution
>> is that the compiler only allows expressions that denote lvalues to
>> be used in such situations.
>
> Then use pass-by-reference. The so-called "value-result" gains
> nothing at all.

False:

(a) if the argument is accessed multiple times in the procedure
body, it avoids dereferencing each time;

(b) if the argument size is small -- eg comparable with a pointer --
it's as cheap to pass the value in as it is to pass the
reference

> If you really want to enhance complexity, consider pass-by-name.

I don't know any recent languages that have pass-by-name; that
piece of functionality is more likely to be provided by cheap
closures nowadays. (Insert pointed gestures towards any language
that doesn't have them.) (And indeed "cheap closures" is one way
pbn was implemented.)

--
"We did not have time to find out everything /A Clash of Cymbals/
we wanted to know." - James Blish

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Ace

unread,
Jun 12, 2007, 8:39:13 PM6/12/07
to
Its all a question of memory. Think of 2 terms here: stack and heap.

When you pass a parameter by value, you are working with a local copy of the
value represented by the parameter, i.e, you are on the stack.

When a parameter is passed by reference, you are working on the actual value
in your computer's RAM.

I see you're learning C.
<kin...@gmail.com> wrote in message
news:1181055542....@a26g2000pre.googlegroups.com...

Chris Dollin

unread,
Jun 13, 2007, 5:09:49 AM6/13/07
to
Ace wrote:

> Its all a question of memory.

It's a question of language semantics.

> Think of 2 terms here: stack and heap.

Or don't.

> When you pass a parameter by value, you are working with a local copy of the
> value represented by the parameter,

Yes.

> i.e, you are on the stack.

Not necessarily: it depends on the language. (The copy might be heap-allocated,
or in a register, or in static store that the compiler can prove won't be
shared.)

> When a parameter is passed by reference, you are working on the actual value
> in your computer's RAM.

Not necessarily: it depends on the language. (Reference's need not be
machine pointers, although it is the usual way. And, possibly nitpicking,
the value might be in /ROM/ -- call-by-reference doesn't /require/ that
the referenced object be updatable.)

--

0 new messages