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:
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 == ??
}
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