1 / 5

Scope of Variables

Scope of Variables. Scope. Scope of a variable is the range of statements in which the variable is visible in a program A variable is visible in a statement if the variable name could be used in the location the statement occupies

elgin
Download Presentation

Scope of Variables

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Scope of Variables

  2. Scope • Scope of a variable is the range of statements in which the variable is visible in a program • A variable is visible in a statement if the variable name could be used in the location the statement occupies • Scope rules determine how variable references throughout a program are resolved

  3. Static Scope • COBOL introduced typed variables with global scope • ALGOL 60 popularized the use of static scoping using begin and end to create blocks of code that could be nested

  4. ALGOL 60 Program begin integer x, y; x = 3; printint(x); if (x < 5) begin integer x = 3; printint(x) end; printint(x) end

  5. Dynamic Scoping • Dynamic scoping uses a program call sequence to determine the scope of a variable {int x = 0; Define int foo () { return x; } Define int goo () { int x = 1; return foo(); } goo();} • Calling goo() with static scoping returns 0 • Calling goo() with dynamic scoping returns 1 since a stack is used to maintain the bindings of each variable as a program block is entered

More Related