Procedural Programming Languages cont…
Static
vs.
Dynamic
Part of program itself
More flexible
Specified in program
Specified during execution
Determined @ compile time
Determined @ runtime
·
Declarations
of identifiers (variables, classes, objects, etc..)
·
Scope of
identifiers
·
Binding of
use of identifiers to its description (multiple declarations of
identifiers)
·
Lifetime of
runtime objects – cleared out when life over
(Scope
& Lifetime inter-connected: Small Scope = Small
Lifetime)
For early programming languages (Assembler, Fortran, Cobol,…,):
·
Scope = Whole source
program
·
Lifetime of runtime objects =
Entire program
·
Declarations =
Explicit
&
Implicit
Int i = 10
i = 10
Problems with
Implicit:
·
Typo’s
·
Readability
Block Structured Programming
Languages
(Includes all successful programming languages)
a. Identifiers may have a local scope
b. Scopes may nest, inherit outer declarations
if(a!=b) {
int temp
\\ Create temp
temp = a
a = b
\\ Free temp
b = temp
if(a!=c) {
int temp
\\ If not redeclared, temp = __ will use previous
…
\\ declaration of temp from outer block
}
}
c. “Closest” declaration is used
d. Binding
Static
Binding
int
x, z
void
a() {
float x, y
print(x,y,z)
\\ Closest applies - x = float x (local decl.)
}
\\
y = float y (local
decl.)
\\
z = int
z
void
B() {
print(x,y,z)
\\ Can’t go into another function – x = int x
}
\\
y
= n/a (undeclared)
\\
z = int z
Dynamic
Binding
int
x
void
print() { \\ If
no local decl., goes through all previous calls
write(x)
}
bool
x
print()
\\
Used in C++, Java – Using virtual functions:
class
c {
void doit() {
printit();
}
void printit() {
println(“C Rules”);
}
}
class
c extends d {
void printit() {
println(“D Rules”);
}
void testit() {
doit();
}
}
d
dvar = new d();
dvar.testit();
Ø Static Binding – “C Rules” because closest, in same class
Ø In Java, C++ -- “D Rules” because closer in dynamic sense