From my Googling, it appears that call by value-result is similar to call by reference in that it changes values in the caller, but it's different in that the changes don't take place until the callee exits, and that if the same variable is passed as more than one argument, it'll be treated as separate values in the callee instead of the same value as in call by reference.
Neither fact helps me explain why call by value-result produces different output than call by reference in the following code:
program Param (input, output);
var
a, b: integer;
procedure p (x, y : integer);
begin
x := x + 2 ;
a := x * y ; (1)
x := x + 1 (2)
end;
begin
a := 1 ;
b := 2 ;
p (a, b) ;
writeln (a)
end.
Edit: here's my understanding of things:
The insight here is that in CBR, in line (1), both a
and x
point to the same thing, so assigning to a
updates both a
and x
to x * y
which is 6. But in CBVR, a
and x
point to different things, so line 1 only updates a
to 6. x
remains 3. Then CBR updates a
right away so a
ends up being 7 outside p
. But CBVR updates a
to whatever x
is at the end of p
, which is 4, so even though a
was 6 in line (1), after p
exits it's changed to 4.
a
meant to be a global variable in your code? $\endgroup$