0% found this document useful (0 votes)
34 views

Variables Scope

Scaopes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Variables Scope

Scaopes
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Dynamic Type Binding

Dynamic Type Binding (JavaScript and PHP)


Specified through an assignment
statement. For e.g., in JavaScript
list = [2, 4.33, 6, 8];
list = 17.3;
o Advantage: flexibility (generic program units)
o Disadvantages:
High cost (dynamic type checking and
interpretation)
Type error detection by the compiler is difficult

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;

Storage Bindings & Lifetime


o Allocation - getting a cell from some pool of
available cells
o Deallocation - putting a cell back into the pool

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)

If scalar, all attributes except address are


statically bound (i.e. before run time)
o local variables in C subprograms/functions and
Java methods

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);
}

Copyright 2009 Addison-Wesley. All rights reserved.


f() --> 2 f() ---> 4 1-26
Categories of Variables by Lifetimes
Explicit heap-dynamic -- Allocated and
deallocated by explicit directives, specified
by the programmer, which take effect
during execution
Referenced only through pointers or
references, e.g. dynamic objects in C++ (via
new and delete), all objects in Java
Advantage: provides for dynamic storage
management
Disadvantage: inefficient and unreliable

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; }

return Math.sqrt(square(a) + square(b));


}

Example credit: http://www.tutorialspoint.com/javascript/javascript_nested_functions.htm


1-32
Copyright 2009 Addison-Wesley. All rights reserved.
Scope (cont.)
Variables can be hidden from a unit by
having a "closer" variable with the
same name
procedure Big is:
X: Integer;
procedure Sub1 is
X: Integer;

Ada allows access to these "hidden"


variables
o E.g., Big.X, unit.name

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++;
}
}

Legal in C and C++, but not in Java and C#


- too error-prone
1-34
Copyright 2009 Addison-Wesley. All rights reserved.
Declaration Order
C99, C++, Java, and C# allow variable
declarations to appear anywhere a
statement can appear
o In C99, C++, and Java, the scope of all local
variables is from the declaration to the end of
the block
o 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

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;
}

In C++, Java, and C#, variables can be declared in


for statements
o The scope of such variables is restricted to the for construct

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

C and C++have both declarations (just


attributes) and definitions (attributes
and storage)
o A declaration outside a function definition specifies
that it is defined in another file

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
}

Prints orange first and then apple


1-39
Copyright 2009 Addison-Wesley. All rights reserved.
Global Scope (continued)

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

private static final int DAYS_IN_WEEK = 7;


private static final int COST_PER_MOVE = 25;
private static final double PI = 3.14159;

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

Update your website with relevant information


about your language

1-52
Copyright 2009 Addison-Wesley. All rights reserved.

You might also like