Connecting Tech Pros Worldwide Forums | Help | Site Map

Could you explain the Pass by value-result problem?

kinaxx@gmail.com
Guest
 
Posts: n/a
#1: Jun 5 '07
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 reading.


Robert Bauck Hamar
Guest
 
Posts: n/a
#2: Jun 5 '07

re: Could you explain the Pass by value-result problem?


kinaxx@gmail.com wrote:
Quote:
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)
This is the usual way of passing arguments in C++.
Quote:
2) pass by reference (inother words: call by reference)
This can be used in C++ with reference parameters.
Quote:
3) pass by value-result <- i have question this subject.
C++ does not support value-result, but they can be simulated.
Quote:
pass by value-result
passing mechanism
1.The values of the arguments are copied into the formal 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 main
Quote:
{
int a=5;
int b=10;
square(a,b);
printf("a = %d b = %d\n",a,b);
}
simulation in C++:
void square(int &x, int &y)
{
x *= x;
y *= y;
}

int main()
{
int a = 5;
int b = 10;

// copy the arguments to temporaries, one per formal parameter.
int tmp1 = a;
int tmp2 = b;
// send these as references, so that result can be retrieved
square(tmp1, tmp2);
// copy result back:
a = tmp1;
b = tmp2;
printf("a = %d b = %d\n",a,b);
}
Quote:
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?
Try to simulate pass as I showed. Then apply brain. We're not here to do
your homework.

--
rbh
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
Guest
 
Posts: n/a
#3: Jun 5 '07

re: Could you explain the Pass by value-result problem?


On 2007-06-05 17:48, Robert Bauck Hamar wrote:
Quote:
kinaxx@gmail.com wrote:
Quote:
>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)
>
This is the usual way of passing arguments in C++.
>
Quote:
>2) pass by reference (inother words: call by reference)
>
This can be used in C++ with reference parameters.
>
Quote:
>3) pass by value-result <- i have question this subject.
>
C++ does not support value-result, but they can be simulated.
>
Quote:
>pass by value-result
>passing mechanism
>1.The values of the arguments are copied into the formal 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 main
>
Quote:
>{
> int a=5;
> int b=10;
> square(a,b);
> printf("a = %d b = %d\n",a,b);
>}
>
simulation in C++:
void square(int &x, int &y)
{
x *= x;
y *= y;
}
>
int main()
{
int a = 5;
int b = 10;
>
// copy the arguments to temporaries, one per formal parameter.
int tmp1 = a;
int tmp2 = b;
// send these as references, so that result can be retrieved
square(tmp1, tmp2);
// copy result back:
a = tmp1;
b = tmp2;
printf("a = %d b = %d\n",a,b);
}
Why the temporaries? Just doing

int main()
{
int a = 5;
int b = 10;

square(a, b);

printf("a = %d b = %d\n",a,b);
}

will work just as well.

--
Erik Wikström
Robert Bauck Hamar
Guest
 
Posts: n/a
#4: Jun 5 '07

re: Could you explain the Pass by value-result problem?


Erik Wikström wrote:
Quote:
On 2007-06-05 17:48, Robert Bauck Hamar wrote:
Quote:
>kinaxx@gmail.com wrote:
Quote:
>>Hello,
>>now I'm learning progamming language in university.
[...]
Quote:
Quote:
Quote:
>>pass by value-result
>>passing mechanism
>>1.The values of the arguments are copied into the formal parameters.
>>2.The final values of the parameters are copied back out to the
>>arguments.
[...]
Quote:
Quote:
Quote:
>>ex)
>>Assume: pass by value-result
>>void square(int x,int y)
>>{
>> x*=x;
>> y*=y;
>>}
>>>
>>>
>>main()
>>
>int main
>>
Quote:
>>{
>> int a=5;
>> int b=10;
>> square(a,b);
>> printf("a = %d b = %d\n",a,b);
>>}
>>
>simulation in C++:
>void square(int &x, int &y)
>{
> x *= x;
> y *= y;
>}
>>
>int main()
>{
> int a = 5;
> int b = 10;
>>
> // copy the arguments to temporaries, one per formal parameter.
> int tmp1 = a;
> int tmp2 = b;
> // send these as references, so that result can be retrieved
> square(tmp1, tmp2);
> // copy result back:
> a = tmp1;
> b = tmp2;
> printf("a = %d b = %d\n",a,b);
>}
>
Why the temporaries?
Because that is how pass-by-value-result parameter passing might be
simulated in C++. As the OP asked about pass-by-value-result, and C++
doesn't support it, this is about the only topical answer in this NG.
Quote:
>Just doing
>
int main()
{
int a = 5;
int b = 10;
>
square(a, b);
>
printf("a = %d b = %d\n",a,b);
}
>
will work just as well.
In this particular example, yes. But not in the second example given by
the OP. My post tries to explain how pass-by-value-result works in code,
finding out how it differs from pass-by-reference is exactly what the
OP's home work assignment reads.

--
rbh
James Kanze
Guest
 
Posts: n/a
#5: Jun 6 '07

re: Could you explain the Pass by value-result problem?


On Jun 5, 4:50 pm, kin...@gmail.com wrote:
Quote:
now I'm learning progamming language in university.
but i have some question.
in textbook. says there are four passing Mechanism
Quote:
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
Quote:
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
Note that copy-in, copy-out is not present in C++, which makes
questions about it more or less off topic here. You might want
to ask in comp.compilers, where the experts for this sort of
thing hang out.
Quote:
ex)
Assume: pass by value-result
void square(int x,int y)
{
x*=x;
y*=y;
}
Quote:
main()
{
int a=5;
int b=10;
square(a,b);
printf("a = %d b = %d\n",a,b);
}
Quote:
output:
Quote:
a=25 b=100
Quote:
can understand! but what about this?
Quote:
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
}
Quote:
main()
{
int a=5;
p(a,a); // a == ??
}
Quote:
What value of a?
Whatever the language in question specifies it to be. In C++,
it is 5, because C++ uses pass by value. (Unless the parameters
are references, in which case it is pass by reference.) In
Fortran (which is designed so that both pass by reference and
copy-in, copy-out are legal), it would be undefined behavior
(although I don't think the Fortran standard uses exactly that
expression). A language requiring copy-in, copy-out could
specify the order of copying, however, and then it would be
defined.

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

Closed Thread