473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9119
ki****@gmail.co m 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.co m 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.
Characteristic s 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.co m 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.co m 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 objektorientier ter Datenverarbeitu ng
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
2544
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 pointers in some cases... TIA
4
3409
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 42. They say that 42 is printed the second time since the value was wrapped in a class and therefore became passed by reference. (sorry for any typos I am a newbie here ;-)
14
2517
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 allows me to store a reference to the object I passed by val, to change that object and for the change to be reflected in the callers copy of the reference (confused?). Well, what is byref for in that case? I'm coming from a C++ background...
3
1409
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 value. EXAMPLE: <input name="eid" type="hidden" id="eid" value="<? echo $ses_id; ?>"> in other words what does the brackets pass to the function thats processing the form, basically how is the used.
1
3603
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" action="https://login.yahoo.com/config/login?" autocomplete="off" name="login_form"> <input type="hidden" name=".tries" value="1"> <input type="hidden" name=".src" value="ym"> <input type="hidden" name=".md5" value="">
11
1819
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 it works. How is it that setting a property to null alters another property? For example setting optionObject.options = null; seems to set optionObject.selectedIndex to -1 automagically. Can you attach "hooks" in javascript that get called...
4
2293
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. My interpretation: 1. Essentially pushes the value type on the stack
14
20412
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 passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
6
2714
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 pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
12
1504
by: raghukumar | last post by:
# include <iostream> class A { public: A() : i(1) {} int i; } ; class B: public A { public :
0
9639
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
9474
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,...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
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
8964
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
7486
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
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.