使用动态范围评估代码的输出

时间:2018-07-09 16:01:44

标签: pseudocode dynamic-scope

我已经尝试了很多次以评估它,并且还了解了它是如何计算的。我还是不太确定如何。输出应为A = 10B = 50J = 5K = 15I = 20,我认为不正确

代码评估:

我的堆栈

main [A = 10, B = -5]
p2   [I = uninitialized ,J = 5, K = uninitialized]
p1 [ I = 10, override B at main to 50, K = 15]
Why did we override B to 50? It's because B isn't declared at p1()
Then, beבause I,K are declared at p1() they won't override I,J, and thus
(because they're local variables) will be gone when we go back to p2()
so now Stack will look like this after p2 has finished:
main [ A = 10, B = 50 ]
p2 [ J = 5 , K = 0 + 5 = 5, I = 5 + 5 = 10 ]

我的输出:A = 10,B = 50,J = 5,K = 5,I = 10

期望的输出:A = 10,B = 50,J = 5,K = 15,I = 20

问题:程序结束前A,B,J,K,I的值应该是什么?在计算它们时我做错了什么?

代码:

program main(input,output);
var A,B: integer;

procedure p1();
   var I,J,K: integer;
   begin
          for I := 1 to 10 do B:= B + I;
          K := I + J;
   end

procedure p2()
   var I,J,K : integer;
   begin
         J := A + B;
         p1();
         K := J + I;
         I := K + J;
   end;

begin
      A := 10
      B := -5
        p2();

end;

0 个答案:

没有答案