0% found this document useful (0 votes)
10 views20 pages

W2 Scope

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views20 pages

W2 Scope

Uploaded by

karaerdal123
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Variable Attributes: Scope

• The scope of a variable is the range of statements


over which it is visible
• The local variables of a program unit are those that
are declared in that unit
• The nonlocal variables of a program unit are those
that are visible in the unit but not declared there
• Global variables are a special category of nonlocal
variables
• The scope rules of a language determine how
references to names are associated with variables

Copyright © 2015 Pearson. All rights reserved. 1-1


Static Scope

• Based on program text


• To connect a name reference to a variable, you (or
the compiler) must find the declaration
• Search process: search declarations, first locally,
then in increasingly larger enclosing scopes, until
one is found for the given name
• Enclosing static scopes (to a specific scope) are
called its static ancestors; the nearest static
ancestor is called a static parent
• Some languages allow nested subprogram
definitions, which create nested static scopes (e.g.,
Ada, JavaScript, Common Lisp, Scheme, Fortran
2003+, F#, and Python)

Copyright © 2015 Pearson. All rights reserved. 1-2


Scope Example
big calls sub1
function big() {
sub1 calls sub2
function sub1() {
sub2 uses x
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}

Copyright © 2015 Pearson. All rights reserved. 1-3


Scope Example
big calls sub1
function big() {
sub1 calls sub2
function sub1() {
sub2 uses x
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}

– Static scoping
• Reference to x in sub2 is to big's x

Copyright © 2015 Pearson. All rights reserved. 1-4


Static Scope Example (C++)

int i = 1;
int main() {
i=2;
{
int i=3;
cout << i;
for (i=1; i<=3; i++) {
cout << i;
}
cout << i;
}
cout << i;
for (i=3; i<=5; i++) {
cout << i;
}
cout << i;
}

1-5
Scope (continued)

• Variables can be hidden from a unit by


having a "closer" variable with the same
name

Copyright © 2015 Pearson. All rights reserved. 1-6


Blocks

– A method of creating static scopes inside program


units--from ALGOL 60
– Example in C:
void sub() {
int count;
while (...) {
int count;
count++;
...
}

}

- Note: legal in C and C++, but not in Java


and C# - too error-prone

Copyright © 2015 Pearson. All rights reserved. 1-7


The LET Construct

• Most functional languages include some


form of let construct
• A let construct has two parts
– The first part binds names to values
– The second part uses the names defined in the first part
• In Scheme:
(LET (
(name1 expression1)

(namen expressionn)
)

Copyright © 2015 Pearson. All rights reserved. 1-8


The LET Construct (continued)

• In ML:
let
val name1 = expression1

val namen = expressionn
in
expression
end;

Copyright © 2015 Pearson. All rights reserved. 1-9


Declaration Order

• C99, C++, Java, and C# allow variable


declarations to appear anywhere a
statement can appear
– In C99, C++, and Java, the scope of all local
variables is from the declaration to the end of
the block
– In C#, the scope of any variable declared in a
block is the whole block, regardless of the
position of the declaration in the block
• However, a variable still must be declared before it
can be used

Copyright © 2015 Pearson. All rights reserved. 1-10


Declaration Order (continued)

• In C++, Java, and C#, variables can be


declared in for statements
– The scope of such variables is restricted to the
for construct

Copyright © 2015 Pearson. All rights reserved. 1-11


Global Scope

• C, C++, PHP, and Python support a


program structure that consists of a
sequence of function definitions in a file
– These languages allow variable declarations to
appear outside function definitions

• C and C++have both declarations (just


attributes) and definitions (attributes and
storage)
– A declaration outside a function definition
specifies that it is defined in another file
Copyright © 2015 Pearson. All rights reserved. 1-12
Global Scope (continued)

• Python
– A global variable can be referenced in functions,
but can be assigned in a function only if it has
been declared to be global in the function

Copyright © 2015 Pearson. All rights reserved. 1-13


Evaluation of Static Scoping

• Works well in many situations


• Problems:
– In most cases, too much access is possible
– As a program evolves, the initial structure is
destroyed and local variables often become
global; subprograms also gravitate toward
become global, rather than nested

Copyright © 2015 Pearson. All rights reserved. 1-14


Dynamic Scope

• Based on calling sequences of program


units, not their textual layout (temporal
versus spatial)
• References to variables are connected to
declarations by searching back through the
chain of subprogram calls that forced
execution to this point

Copyright © 2015 Pearson. All rights reserved. 1-15


Scope Example
big calls sub1
function big() {
sub1 calls sub2
function sub1() {
sub2 uses x
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}

– Static scoping
• Reference to x in sub2 is to big's x

Copyright © 2015 Pearson. All rights reserved. 1-16


Scope Example
big calls sub1
function big() {
sub1 calls sub2
function sub1() {
sub2 uses x
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}

– Static scoping
• Reference to x in sub2 is to big's x
– Dynamic scoping
• Reference to x in sub2 is to sub1's x


Copyright © 2015 Pearson. All rights reserved. 1-17
Scope Example

• Evaluation of Dynamic Scoping:


– Advantage:
- May not need parameter-passing
– Disadvantages:
1. While a subprogram is executing, its variables are
visible to all subprograms it calls
2. Impossible to statically type check
3. Poor readability- it is not possible to statically
determine the type of a variable

Copyright © 2015 Pearson. All rights reserved. 1-18


Scope and Lifetime

• Scope and lifetime are sometimes closely


related, but are different concepts
• Consider a static variable in a C or C++
function

Copyright © 2015 Pearson. All rights reserved. 1-19


Referencing Environments

• The referencing environment of a statement is the


collection of all names that are visible in the
statement
• In a static-scoped language, it is the local variables
plus all of the visible variables in all of the
enclosing scopes
• In a dynamic-scoped language, the referencing
environment is the local variables plus all visible
variables in all active subprograms
• A subprogram is active if its execution has begun but has not
yet terminated

Copyright © 2015 Pearson. All rights reserved. 1-20

You might also like