|
|
 |
 |
 |
 |
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 read.
kin @gmail.com writes: > 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 .
You will get better advice in a more general newsgroup like comp.programming. This group discusses C in which only values can be passed to functions. -- Ben.
In article <1181054759.519035.95@x35g2000prf.googlegroups.com>, <kin @gmail.com> wrote: >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 This isn't a C question, because C uses (1).
>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 >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?
As it says in the text you quoted, "the order of copying the results may be important". If there's any programming language that uses call-by-value-and-result, presumably its specification will answer your question. Quite likely the answer will be "it's implementation defined". -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
kin @gmail.com wrote: > Hello, > now I'm learning progamming language in university. Which one? Because we do C here; we're not a general prog-lang newsgroup. You'd be better off in comp.lang.misc, I think, or possibly comp.compilers. > 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
C has pass by value, and that's your lot. [The apparent exception for arrays is just that; apparent; it's not part of the parameter-passing mechanism, it's part of the arrays-used-as-values-become-pointers-to-element-0 mechanism.] So your syntactically-C pass-by-value-result can't be answered here, because it's not a C question, but ... > main() > { > int a=5; > p(a,a); // a == ?? > } > What value of a?
That would depend on the /actual language/ claiming to have pass-by-value-result. Since we don't know what it is (but we do know it isn't C, and might admit to knowing it isn't C++), we can't say, and it would be atopical of us to do so. (fx:OT) If C mutated, Goldschmidt-like, to have pass-by-value-result arguments as an option -- an event I regard about as likely as railways to the moon or Buffy living a long and happy life -- then I'd expect it to say that if any of the value-result arguments to a function overlapped The Results Are Undefined. C's like that. -- There' no hortage of vowel on Uenet. Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
(Stuff also appearing, it transpires, in comp.programming.) PS It's not polite to multipost. Don't do it. -- A rock is not a fact. A rock is a rock. Hewlett-Packard Limited Cain Road, Bracknell, registered no: registered office: Berks RG12 1HN 690597 England
On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote: >C has pass by value, and that's your lot.
C also has 'pass by address' (a.k.a. pointers). -- Roland Pibinger "The best software is simple, elegant, and full of drama" - Grady Booch
Roland Pibinger said: > On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote: >>C has pass by value, and that's your lot. > C also has 'pass by address' (a.k.a. pointers).
In C, pointer expressions are passed by value, just like any other argument expressions. -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www.
Roland Pibinger wrote: > On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote: >>C has pass by value, and that's your lot. > C also has 'pass by address' (a.k.a. pointers).
My evil twin will be ready to point out that C does /not/ have "pass by address". Using pointers is just using pointers; there's no special parameter-passing machinery involved. Contrast eg VAR parameters in Pascal. -- Far-Fetched Hedgehog The shortcuts are all full of people using them.
In article <4665a408.39236@news.utanet.at>, Roland Pibinger <rpbg @yahoo.com> wrote: >>C has pass by value, and that's your lot. >C also has 'pass by address' (a.k.a. pointers). No, but you can pass pointers by value and dereference them in the called function to achieve the effect of call-by-reference. -- Richard -- "Consideration shall be given to the need for as many as 32 characters in some alphabets" - X3.4, 1963.
In article <f43t1e$19j @pc-news.cogsci.ed.ac.uk> rich @cogsci.ed.ac.uk (Richard Tobin) writes: > In article <1181054759.519035.95@x35g2000prf.googlegroups.com>, > <kin@gmail.com> wrote: > >3) pass by value-result <- i have question this subject . ... > >void p(int x,int y) ... > > x=sum; //sum will be returned via x > > y=prod; // prod will be returned via y ... > > p(a,a); // a == ?? ... > As it says in the text you quoted, "the order of copying the results > may be important". If there's any programming language that uses > call-by-value-and-result, presumably its specification will answer > your question. Quite likely the answer will be "it's implementation > defined". The only programming language I know that allows for pass by value-result is Fortran. But there the call p(a,a) is disallowed. (If two arguments are aliases of each other, assignment to them in a subroutine is not allowed.) -- dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131 home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Roland Pibinger wrote: > On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote: >> C has pass by value, and that's your lot. > C also has 'pass by address' (a.k.a. pointers).
No it doesn't. It allows you to simulate reference addressing by the use of * and passing pointers BY VALUE. -- <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
Chris Dollin wrote: > Roland Pibinger wrote: >> On Tue, 05 Jun 2007 15:54:44 +0100, Chris Dollin wrote: >>>C has pass by value, and that's your lot. >> C also has 'pass by address' (a.k.a. pointers). > My evil twin will be ready to point out that C does > /not/ have "pass by address".
I though we'd agreed not to both post to the same thread? But you're right. -- "No-one here is exactly what he appears." G'kar, /Babylon 5/ Hewlett-Packard Limited Cain Road, Bracknell, registered no: registered office: Berks RG12 1HN 690597 England
|
 |
 |
 |
 |
|