Presentation is loading. Please wait.

Presentation is loading. Please wait.

Scope.

Similar presentations


Presentation on theme: "Scope."— Presentation transcript:

1 Scope

2 Scope Variable scope defines the range of statements over which a variable is visible Local variables of a program unit X are variables that are declared within X and therefore visible within X Non-local variables of a program unit X are variables that are visible but not declared within X

3 Static Scope Static scope indicates that the scope of a variable can be determined before execution How? Search local declarations first, then search in increasingly larger enclosing scopes, until a valid declaration is found Variable x becomes hidden Not valid in Java or C# main() { /** C **/ int x = 5; printf("%d\n", x); { int x = 10; int y = 20; int x = 15; int z = 20; }

4 Static Scope Static scope indicates that the scope of a variable can be determined before execution JavaScript and PHP do not support nested static scopes But JavaScript and PHP do support function-level scope var abc = false; if ( s == "HELLO" ) { abc = true; var abc = 500; document.write("abc is " + abc) } document.write("abc is still " + abc)

5 Dynamic Scoping Dynamic scoping defines a variable’s scope based on the calling sequences of subprograms Used in APL, SNOBOL4, early versions of LISP procedure Main is X : Integer; procedure Sub1 is begin -- of Sub1 ... X ... end; -- of Sub1 procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Main end; -- of Main Call Main, which calls Sub1 Call Main, which calls Sub2, which then calls Sub1 scope of X can only be determined at run time

6 repeat using dynamic scoping
Problem 8 Given this Ada code: Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using static scoping, which X is referenced in Sub1? In Sub2? In Sub3? procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main repeat using dynamic scoping

7 SOLUTIONS Problem 8 Given this Ada code:
Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using static scoping Sub1 references Sub1.X Sub2 references Sub1.X Sub3 references Main.X procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main

8 SOLUTIONS Problem 8 Given this Ada code:
Assume execution of this code is Main calls Sub1 Sub1 calls Sub2 Sub2 calls Sub3 Using dynamic scoping Sub1 references Sub1.X Sub2 references Sub1.X Sub3 references Sub1.X procedure Main is X : Integer; procedure Sub3; -- allows Sub1 to -- to call Sub3 procedure Sub1 is procedure Sub2 is begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main

9 Problem 9 Given this Ada code:
Assume program execution using static-scoping rules What is the output of this program? What is the output using dynamic-scoping rules? procedure Main is X : Integer; procedure SubA is begin -- of SubA Put(X); -- i.e. output X end; -- of SubA procedure SubB is begin -- of SubB X := 10; SubA end; -- of SubB begin -- of Main X := 5; SubB end; -- of Main

10 SOLUTIONS Problem 9 Given this Ada code:
Assume program execution using static-scoping rules What is the output of this program? 5 What is the output using dynamic-scoping? 10 procedure Main is X : Integer; procedure SubA is begin -- of SubA Put(X); -- i.e. output X end; -- of SubA procedure SubB is begin -- of SubB X := 10; SubA end; -- of SubB begin -- of Main X := 5; SubB end; -- of Main

11 Problem 10 Given this Ada code:
procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Assume program execution using static-scoping rules For Sub1, Sub2, and Sub3, list the visible variables and the program units within which they are declared

12 SOLUTIONS Problem 10 Given this Ada code:
procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 ... end; -- of Sub2 begin -- of Sub1 end; -- of Sub1 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub1: A (Sub1), Y (Sub1), Z (Sub1), X (Main) Sub2: A (Sub2), B (Sub2), Z (Sub2), Y (Sub1), X (Main) Sub3: A (Sub3), X (Sub3), W (Sub3), Y (Main), Z (Main)

13 Problem 11 Given this Ada code:
procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, X, W : Integer; procedure Sub3 is A, B, Z : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Sub2 end; -- of Sub2 begin -- of Main end; -- of Main Given this Ada code: Assume program execution using static-scoping rules For Sub1, Sub2, and Sub3, list the visible variables and the program units within which they are declared

14 SOLUTIONS Problem 11 Given this Ada code:
procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, X, W : Integer; procedure Sub3 is A, B, Z : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Sub2 end; -- of Sub2 begin -- of Main end; -- of Main Given this Ada code: Using static-scoping rules, list visible variables and the program units in which they are declared Sub1: A (Sub1), Y (Sub1), Z (Sub1), X(Main) Sub2: A (Sub2), X (Sub2), W (Sub2), Z (Main), Y (Main) Sub3: A (Sub3), B (Sub3), Z (Sub3), X (Sub2), W (Sub2), Y (Main)

15 Problem 12 Given this C code:
For each of the marked points A, B, C, and D, list each visible variable and the number of the declaration statement void doSomething() { int a, b, c; /** 1 **/ ... while ( ... ) int b, c, d; /** 2 **/ Point A int c, d, e; /** 3 **/ Point B } Point C Point D

16 SOLUTIONS Problem 12 Given this C code:
For points A, B, C, and D, list each visible variable and the number of the declaration statement A: a (1), b (2), c (2), d (2) B: a (1), b (2), c (3), d (3), e (3) C: a (1), b (2), c (2), d (2) D: a (1), b (1), c (1) void doSomething() { int a, b, c; /** 1 **/ ... while ( ... ) int b, c, d; /** 2 **/ Point A int c, d, e; /** 3 **/ Point B } Point C Point D

17 Example Given this C-like code: Assume dynamic-scoping rules
Given the following call sequences, what variables are visible during execution of the last function called ? Also specify the name of the function in which each variable was defined main() calls f1(), which calls f3() main() calls f2(), which calls f3(), which calls f1() main() calls f3(), which calls f1() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

18 SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules
Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f1(), which calls f3() In f3(): d, e, f declared in f3() b, c declared in f1() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

19 SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules
Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f2(), which calls f3(), which calls f1() In f1(): b, c, d declared in f1() e, f declared in f3() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

20 SOLUTIONS Example Given this C-like code: Assume dynamic-scoping rules
Given the following call sequences, what variables are visible during execution of the last function called ? main() calls f3(), which calls f1() In f1(): b, c, d declared in f1() e, f declared in f3() a declared in main() void main() { int a, b, c; ... } void f1() { int b, c, d; void f2() { int c, d, e; void f3() { int d, e, f;

21 Problem 14 Given this Ada code: Assume dynamic-scoping rules
procedure Main is X, Y, Z : Integer; procedure Sub1 is A, Y, Z : Integer; begin -- of Sub1 ... end; -- of Sub1 procedure Sub2 is A, B, Z : Integer; begin -- of Sub2 end; -- of Sub2 procedure Sub3 is A, X, W : Integer; begin -- of Sub3 end; -- of Sub3 begin -- of Main end; -- of Main Given this Ada code: Assume dynamic-scoping rules Given the following call sequences, what variables are visible during execution of the last function called ? Also specify the name of the function in which each variable was defined main() calls Sub1(), which calls Sub2(), which calls Sub3() main() calls Sub1() , which calls Sub3() main() calls Sub2(), which calls Sub3() , which calls Sub3()


Download ppt "Scope."

Similar presentations


Ads by Google