Take the 2-minute tour ×

Can you confirm my answer for the following code:

procedure main
var x,y,z;

    procedure sub1
        var x,z 
        x := 6;
        z := 7;
        x := y*z + x;
    procedure sub2
        var x,y
        x := 1;
        y := x+z+2;
    x := 1; y:=3; z:=5;

I got:

  • static:

  • dynamic:


Is that correct?

share|improve this question
The program refuses to compile here. Var blocks within begin..end ? sub2 being called for declaration? Doing sub1(); is not proper pascal too, and while most compilers will accept it, your teacher might not. What is print() ? –  Marco van de Voort Apr 2 '11 at 15:38
The above code isn't valid Pascal. Surely you have written it incorrectly. –  Andreas Rejbrand Apr 2 '11 at 18:11
I agree with the comments about the wrong syntax constructs used, but I think the question is more theoretical in nature than practical. The example is clear, albeit syntactically incorrect (however, there should probably be some sort of disclaimer on that head). –  Andriy M Apr 3 '11 at 20:32
add comment

2 Answers

I've no idea what static vs dynamic means. Pascal always uses the variable in the innermost scope. If you use that, then 8,27 is the result. I don't know how you came to the other result (everything global?)

share|improve this answer
add comment

If Pascal supported dynamic scoping, then your analysis would be correct, as far as it goes. The z variable declared in sub1 would shadow the one declared in main, even within sub2. But the x declared in sub2 would not affect the value of the x declared in sub1, so sub1 still uses the original value 6 when it reads x after calling sub2.

Your analysis is incomplete, though. There should be three values printed each time, not just two. The third value printed should be 8 in both cases.

share|improve this answer
add comment

Your Answer


