Scope
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Scope PowerPoint PPT Presentation


  • 83 Views
  • Uploaded on
  • Presentation posted in: General

Scope. 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

Download Presentation

Scope

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Scope

Scope


Scope

Scope

  • Variable scope defines the range of statementsover which a variable is visible

    • Local variables of a program unit X are variables thatare declared within X and therefore visible within X

    • Non-local variables of a program unit X are variablesthat are visible but not declared within X


Scope

Static Scope

  • Static scope indicates that the scope of a variablecan be determined before execution

    • How?

    • Search local declarations first,then search in increasinglylarger enclosing scopes, untila 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;

printf("%d\n", x);

{

int x = 15;

int z = 20;

printf("%d\n", x);

}

}

}


Scope

Static Scope

  • Static scope indicates that the scope of a variablecan 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)


Scope

scope of X can only be

determined at run time

Dynamic Scoping

  • Dynamic scoping defines a variable’s scope basedon 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

X : Integer;

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


Problem 8

Problem 8

  • Given this Ada code:

    • Assume execution ofthis code isMain calls Sub1Sub1 calls Sub2Sub2 calls Sub3

    • Using static scoping,which X is referencedin Sub1? In Sub2?In Sub3?

procedure Main is

X : Integer;

procedure Sub3; -- allows Sub1 to

-- to call Sub3

procedure Sub1 is

X : Integer;

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 usingdynamic scoping


Problem 81

Problem 8

SOLUTIONS

  • Given this Ada code:

    • Assume execution ofthis code isMain calls Sub1Sub1 calls Sub2Sub2 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

X : Integer;

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


Problem 82

Problem 8

SOLUTIONS

  • Given this Ada code:

    • Assume execution ofthis code isMain calls Sub1Sub1 calls Sub2Sub2 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

X : Integer;

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


Problem 9

Problem 9

  • Given this Ada code:

    • Assume programexecution usingstatic-scoping rules

    • What is the outputof this program?

    • What is the outputusing dynamic-scopingrules?

procedure Main is

X : Integer;

procedure SubA is

begin -- of SubA

Put(X); -- i.e. output X

end; -- of SubA

procedure SubB is

X : Integer;

begin -- of SubB

X := 10;

SubA

end; -- of SubB

begin -- of Main

X := 5;

SubB

end; -- of Main


Problem 91

SOLUTIONS

Problem 9

  • Given this Ada code:

    • Assume programexecution usingstatic-scoping rules

    • What is the outputof this program?

      • 5

    • What is the outputusing 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

X : Integer;

begin -- of SubB

X := 10;

SubA

end; -- of SubB

begin -- of Main

X := 5;

SubB

end; -- of Main


Problem 10

Problem 10

  • Given this Ada code:

    • Assume program executionusing static-scoping rules

    • For Sub1, Sub2, and Sub3,list the visible variables andthe program units within whichthey are declared

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


Problem 101

SOLUTIONS

Problem 10

  • Given this Ada code:

    • Using static-scoping rules, listvisible variables and the programunits 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)

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


Problem 11

Problem 11

  • Given this Ada code:

    • Assume program executionusing static-scoping rules

    • For Sub1, Sub2, and Sub3,list the visible variables andthe program units within whichthey are declared

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


Problem 111

SOLUTIONS

Problem 11

  • Given this Ada code:

    • Using static-scoping rules, listvisible variables and the programunits 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)

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


Problem 12

Problem 12

  • Given this C code:

    • For each of the markedpoints A, B, C, and D, list each visible variableand the number of thedeclaration statement

void doSomething()

{

int a, b, c; /** 1 **/

...

while ( ... )

{

int b, c, d; /** 2 **/

... Point A

while ( ... )

{

int c, d, e; /** 3 **/

... Point B

}

... Point C

}

... Point D

}


Problem 121

Problem 12

SOLUTIONS

  • Given this C code:

    • For points A, B, C, and D, list each visible variableand the number of thedeclaration 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

while ( ... )

{

int c, d, e; /** 3 **/

... Point B

}

... Point C

}

... Point D

}


Example

Example

  • Given this C-like code:

    • Assume dynamic-scoping rules

    • Given the following call sequences,what variables are visible duringexecution of the last function called?

      • Also specify the name of the functionin 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;

...

}


Example1

Example

SOLUTIONS

  • Given this C-like code:

    • Assume dynamic-scoping rules

    • Given the following call sequences,what variables are visible duringexecution 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;

...

}


Example2

Example

SOLUTIONS

  • Given this C-like code:

    • Assume dynamic-scoping rules

    • Given the following call sequences,what variables are visible duringexecution 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;

...

}


Example3

Example

SOLUTIONS

  • Given this C-like code:

    • Assume dynamic-scoping rules

    • Given the following call sequences,what variables are visible duringexecution 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;

...

}


Problem 14

Problem 14

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 duringexecution of the last function called?

      • Also specify the name of the functionin 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()


  • Login