Variables Scope
Variables Scope
1-20
Copyright 2009 Addison-Wesley. All rights reserved.
Variable Attributes (cont.)
Type Inferencing (ML, Miranda, and
Haskell)
o Rather than by assignment statement, types are
determined (by the compiler) from the context of
the reference
o fun circumference(r) = 3.1415 * r * r;
1-21
Copyright 2009 Addison-Wesley. All rights reserved.
Variable Attributes (cont.)
The lifetime of a variable is the time
during which it is bound to a
particular memory cell
1-22
Copyright 2009 Addison-Wesley. All rights reserved.
Categories of Variables by Lifetimes
Static
Stack-dynamic
Explicit heap-dynamic
Implicit heap-dynamic
In-class activity
1-23
Copyright 2009 Addison-Wesley. All rights reserved.
Categories of Variables by Lifetimes
Static--bound to memory cells before
execution begins and remains bound
to the same memory cell throughout
execution, e.g., C and C++ static
variables
o Advantages: efficiency (direct addressing),
history-sensitive subprogram support
o Disadvantage: lack of flexibility (no recursion)
1-24
Copyright 2009 Addison-Wesley. All rights reserved.
Categories of Variables by Lifetimes
Stack-dynamic--Storage bindings are
created for variables when their declaration
statements are elaborated.
(A declaration is elaborated when the
executable code associated with it is
executed)
1-25
Copyright 2009 Addison-Wesley. All rights reserved.
Categories of Variables by Lifetimes
Advantage: allows recursion; conserves
storage
Disadvantages:
o Overhead of allocation and deallocation
o Subprograms cannot be history sensitive
o Inefficient references (indirect addressing)
History sensitive example
int f() {
static int a = 0;
a += 2;
printf("%d\n",a);
}
1-27
Copyright 2009 Addison-Wesley. All rights reserved.
Categories of Variables by Lifetimes
Implicit heap-dynamic--Allocation
and deallocation caused by
assignment statements
o all variables in APL; all strings and arrays in Perl,
JavaScript, and PHP
Advantage: flexibility (generic code)
Disadvantages:
o Inefficient, because all attributes are dynamic
o Loss of error detection
1-28
Copyright 2009 Addison-Wesley. All rights reserved.
Variable Attributes: Scope
The scope of a variable is the range of
statements over which it is visible
The nonlocal variables of a program
unit are those that are visible but not
declared there
The scope rules of a language
determine how references to names
are associated with variables
1-29
Copyright 2009 Addison-Wesley. All rights reserved.
Static Scope
Based on program text
Scope of a variable can be statically
determined (prior to execution)
Find the variable declaration to
connect a name reference
Search process: search declarations,
first locally, then in increasingly larger
enclosing scopes, until one is found
for the given name
1-30
Copyright 2009 Addison-Wesley. All rights reserved.
Static Scope
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, Fortran 2003, and PHP)
1-31
Copyright 2009 Addison-Wesley. All rights reserved.
Static Scope
Javascript nested subprograms
function hypotenuse(a, b) {
function square(x) { return x*x; }
1-33
Copyright 2009 Addison-Wesley. All rights reserved.
Blocks
A method of creating static scopes inside
program units--from ALGOL 60
Example in C:
void sub() {
int count;
while (...) {
int count;
count++;
}
}
1-35
Copyright 2009 Addison-Wesley. All rights reserved.
Declaration Order (continued)
In C#,
void F() {
int x = 1, y, z = x * 2;
}
is the same as
void F() {
int x; x = 1;
int y;
int z; z = x * 2;
}
1-36
Copyright 2009 Addison-Wesley. All rights reserved.
Global Scope
C, C++, PHP, and Python support a
program structure that consists of a
sequence of function definitions in a file
o These languages allow variable declarations to
appear outside function definitions
1-37
Copyright 2009 Addison-Wesley. All rights reserved.
Global Scope (continued)
PHP
o Programs are embedded in XHTML markup
documents, in any number of fragments, some
statements and some function definitions
o The scope of a variable declared in a function is
local to the function
o The scope of a variable implicitly declared
outside functions is from the declaration to the
end of the program, but skips over any
intervening functions
Global variables can be accessed in a function
through the $GLOBALS array or by declaring it global
1-38
Copyright 2009 Addison-Wesley. All rights reserved.
Global Scope
PHP
$fruit = apple;
function basket() {
$fruit = orange;
print $fruit
$gfruit = $GLOBALS[fruit]
print $gfruit
}
Python
o 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
1-40
Copyright 2009 Addison-Wesley. All rights reserved.
Evaluation of Static Scoping
Works well in many situations
Problems:
o Allows more access to variables than is
necessary
o Program evolution - As a program evolves, the
initial structure is destroyed and local variables
often become global; subprograms also gravitate
toward becoming global, rather than nested
1-41
Copyright 2009 Addison-Wesley. All rights reserved.
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
1-42
Copyright 2009 Addison-Wesley. All rights reserved.
Scope Example
Big
- declaration of X
Sub1
- declaration of X -
...
call Sub2
... Big calls Sub1
Sub1 calls Sub2
Sub2 Sub2 uses X
...
- reference to X -
...
...
call Sub1
1-43
Copyright 2009 Addison-Wesley. All rights reserved.
Ada Scope Example
Static scoping
o Reference to X is to Big's X
Dynamic scoping
o Reference to X is to Sub1's X
1-44
Copyright 2009 Addison-Wesley. All rights reserved.
Evaluation of Dynamic Scoping:
Advantage: convenience
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
1-45
Copyright 2009 Addison-Wesley. All rights reserved.
Scope and Lifetime
Scope and lifetime are sometimes
closely related, but are different
concepts
For example: A static variable in a
C/C++ function
1-46
Copyright 2009 Addison-Wesley. All rights reserved.
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
1-47
Copyright 2009 Addison-Wesley. All rights reserved.
Referencing Environments
A subprogram is active if its execution
has begun but has not yet terminated
In a dynamic-scoped language, the
referencing environment is the local
variables plus all visible variables in all
active subprograms
1-48
Copyright 2009 Addison-Wesley. All rights reserved.
Named Constants
A named constant is a variable that is
bound to a value only when it is bound
to storage
1-49
Copyright 2009 Addison-Wesley. All rights reserved.
Named Constants
Advantages:
o readability and modifiability
o Used to parameterize programs
o The binding of values to named constants
can be either static (called manifest
constants) or dynamic
1-50
Copyright 2009 Addison-Wesley. All rights reserved.
Named Constants
Languages:
o FORTRAN 95: constant-valued expressions
o Ada, C++, and Java: expressions of any kind
o C# has two kinds, readonly and const
the values of const named constants are
bound at compile time
The values of readonly named constants are
dynamically bound
1-51
Copyright 2009 Addison-Wesley. All rights reserved.
Summary
Naming conventions
Variables are characterized by the attributes:
name, address, value, type, lifetime, scope
Binding type
Variable lifetimes: static, stack dynamic,
explicit heap dynamic, implicit heap dynamic
Strong typing vs weak typing
1-52
Copyright 2009 Addison-Wesley. All rights reserved.