Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- procedure p(var x, y : Integer); // simulates call by value-result
- var
- xtmp, ytmp : Integer;
- procedure p_hidden(var x, y : Integer); // nested "hidden" procedure
- begin
- ... YOUR ORIGINAL CODE HERE ...
- end;
- begin
- xtmp := x; ytmp := y; // use auxiliary variables
- p_hidden(xtmp,ytmp); // call by ref the nested procedure
- x := xtmp; y := ytmp; // copy values before return
- end;
- a := 1; b := 2;
- p(a, b)
- //// procedure p (var x, y : integer);
- //// x is the same as a, y is the same as b
- //// begin
- a := a + 2 ; // a = 3
- a := a * b ; // a = 3 * 2 = 6
- a := a + 1 // a = a + 1 = 7
- //// end;
- writeln (a) // prints 7
- a := 1; b := 2;
- p(a, b)
- //// procedure p (valres x, y : integer); // by value result
- //// x is the same as a, y is the same as b like in a by reference call
- xt := a; yt := b; // xt and yt are two "new" variables
- //// begin
- xt := xt + 2 ; // xt = 3
- a := xt * yt ; // a = 3 * 2 = 6
- xt := xt + 1 // xt = 3 + 1 = 4
- //// end;
- //// the values of xt and yt are copied back to a and b (x and y) before return
- a := xt; // a = 4
- b := yt; // b = 2
- writeln (a) // prints 4
- //a := 1
- | 1 | <-- Variable a, with holding the value 1
- |_______|
- procedure f(x: Integer)...
- //f(a)
- a -> | 1 | -- copying --> | 1 | <- x
- |_______| |_______|
- //f(a)
- a -> | 1 | <- x -- for the duration of the call f(a)
- |_______|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement