Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. program Param (input, output);
  2. var
  3. a, b: integer;
  4. procedure p (x, y : integer);
  5. begin
  6. x := x + 2 ;
  7. a := x * y ; (1)
  8. x := x + 1 (2)
  9. end;
  10. begin
  11. a := 1 ;
  12. b := 2 ;
  13. p (a, b) ;
  14. writeln (a)
  15. end.
  16.  
  17. procedure p(var x, y : Integer); // simulates call by value-result
  18. var
  19. xtmp, ytmp : Integer;
  20.  
  21. procedure p_hidden(var x, y : Integer); // nested "hidden" procedure
  22. begin
  23. ... YOUR ORIGINAL CODE HERE ...
  24. end;
  25.  
  26. begin
  27. xtmp := x; ytmp := y; // use auxiliary variables
  28. p_hidden(xtmp,ytmp); // call by ref the nested procedure
  29. x := xtmp; y := ytmp; // copy values before return
  30. end;
  31.  
  32. a := 1; b := 2;
  33. p(a, b)
  34. //// procedure p (var x, y : integer);
  35. //// x is the same as a, y is the same as b
  36. //// begin
  37. a := a + 2 ; // a = 3
  38. a := a * b ; // a = 3 * 2 = 6
  39. a := a + 1 // a = a + 1 = 7
  40. //// end;
  41. writeln (a) // prints 7
  42.  
  43. a := 1; b := 2;
  44. p(a, b)
  45. //// procedure p (valres x, y : integer); // by value result
  46. //// x is the same as a, y is the same as b like in a by reference call
  47. xt := a; yt := b; // xt and yt are two "new" variables
  48. //// begin
  49. xt := xt + 2 ; // xt = 3
  50. a := xt * yt ; // a = 3 * 2 = 6
  51. xt := xt + 1 // xt = 3 + 1 = 4
  52. //// end;
  53. //// the values of xt and yt are copied back to a and b (x and y) before return
  54. a := xt; // a = 4
  55. b := yt; // b = 2
  56. writeln (a) // prints 4
  57.  
  58. //a := 1
  59.  
  60. | 1 | <-- Variable a, with holding the value 1
  61. |_______|
  62.  
  63. procedure f(x: Integer)...
  64.  
  65. //f(a)
  66.  
  67. a -> | 1 | -- copying --> | 1 | <- x
  68. |_______| |_______|
  69.  
  70. //f(a)
  71.  
  72. a -> | 1 | <- x -- for the duration of the call f(a)
  73. |_______|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement