434,933 Members | 1,223 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,933 IT Pros & Developers. It's quick & easy.

Could you explain the Pass by value-result problem?

 P: n/a 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 Replies

 P: n/a 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

 P: n/a 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 Mechanism1) 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-resultpassing mechanism1.The values of the arguments are copied into the formal parameters.2.The final values of the parameters are copied back out to thearguments.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 thispoint)-The order of copying the results may be important ex)Assume: pass by value-resultvoid 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

 P: n/a 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-resultpassing mechanism1.The values of the arguments are copied into the formal parameters.2.The final values of the parameters are copied back out to thearguments. [...] >>ex)Assume: pass by value-resultvoid 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

 P: n/a 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 discussion thread is closed

Replies have been disabled for this discussion. 