From Programming Languages: Principles and Paradigms, by Maurizio Gabbrielli, Simone Martini
void fiefoo (valueresult int x, valueresult int y) { x = x+1; y = 1; } ... int i = 1; int[] A = new int[5]; A[1]=4; fiefoo(i,A[i]);
we are to execute
fiefoo
with parameters passed by value-result. On termination, we will haveA[1]
with value1
andi
with value2
, while the rest of the arrayA
has not been touched.
Why is it A[1]
instead of A[2]
whose value is 1
?
My thought is that when returning from the function fiefoo
, x
's value 2
is assigned to i
, so A[i]
is evaluated to be A[2]
, and y
's value 1
is assigned to A[i]
.
My thought is according to what the book says
It should be clear that, as in call by value, the following questions about evaluation order in call by return must be answered. If there is more than one result parameter, in which order (for example, from left to right) should the corresponding “backward assignment” from the actual to formal be performed?
Finally, when is the actual parameter’s l-value determined? It is reasonable to determine it both when the function is called and when it terminates. We can construct an example which gives different results if the l-value of the actual is determined at call time or when the procedure terminates.
Thanks.