473,400 Members | 2,145 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,400 software developers and data experts.

Could you explain the Pass by value-result problem?

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.

Jun 5 '07 #1
4 9087
ki****@gmail.com wrote:
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++.
2) pass by reference (inother words: call by reference)
This can be used in C++ with reference parameters.
3) pass by value-result <- i have question this subject.
C++ does not support value-result, but they can be simulated.
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
{
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);
}
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
Jun 5 '07 #2
On 2007-06-05 17:48, Robert Bauck Hamar wrote:
ki****@gmail.com wrote:
>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++.
>2) pass by reference (inother words: call by reference)

This can be used in C++ with reference parameters.
>3) pass by value-result <- i have question this subject.

C++ does not support value-result, but they can be simulated.
>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
>{
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
Jun 5 '07 #3
Erik Wikström wrote:
On 2007-06-05 17:48, Robert Bauck Hamar wrote:
>ki****@gmail.com wrote:
>>Hello,
now I'm learning progamming language in university.
[...]
>>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.
[...]
>>ex)
Assume: pass by value-result
void square(int x,int y)
{
x*=x;
y*=y;
}
main()

int main
>>{
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.
>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
Jun 5 '07 #4
On Jun 5, 4:50 pm, 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.
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.
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?
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:ja*********@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

Jun 6 '07 #5

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

Similar topics

14
by: bo | last post by:
And why and where one should use one vs. the other? Verbally, it seems like semantics to me--but obviously there is some actual difference that makes references different and or preferable over...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
3
by: Chris H | last post by:
I have been looking through this script and came across something that I dont quite understand how it functions or is used. Basically its brackets that are added on at the end of a form field...
1
by: Andrew | last post by:
Hello, friends, I am implementing web app security using asp.net 1.1, and I found the following source code from Yahoo! Mail login page: <form method="post"...
11
by: Faisal Vali | last post by:
Hi - I'm new to javascript and I was reading the book Javascript Professional Projects - there is a fragment that has me a little perplexed, and I was wondering if anyone could explain why and how...
4
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. ...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
6
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as...
12
by: raghukumar | last post by:
# include <iostream> class A { public: A() : i(1) {} int i; } ; class B: public A { public :
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
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,...
0
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...

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.