Topic 3: Names, Bindings & Scope: Marriette Katarahweire
Topic 3: Names, Bindings & Scope: Marriette Katarahweire
Marriette Katarahweire
- The types of sum, total, and name are int, float, and string,
respectively. These are statically typed variables—their types
are fixed for the lifetime of the unit in which they are declared.
Disadvantages:
- High cost
- Less reliability
Type error detection by the compiler is difficult because any
var can be assigned a value of any type. Incorrect types of
right sides of assignments are not detected as errors; rather,
the type of the left side is simply changed to the incorrect
type.
Example:
i, x //Integer
y //floating-point array
i = x //what the user meant to type
i = y //what the user typed instead
Unlike the stack, the heap does not have size restrictions on
variable size (apart from the obvious physical limitations of
your computer).
Heap memory is slightly slower to be read from and written
to, because one has to use pointers to access memory on the
heap.
Unlike the stack, variables created on the heap are accessible
by any function, anywhere in your program. Heap variables
are essentially global in scope.
are bound to heap storage only when they are assigned values.
all their attributes are bound every time they are assigned
function big() {
function sub1() {
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}
function big() {
function sub1() {
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}
void sub() {
int count;
. . .
while (. . .) {
int count;
count++;
. . .
}
. . .
}
function big() {
function sub1() {
var x = 7;
sub2();
}
function sub2() {
var y = x;
}
var x = 3;
sub1();
}
void sub1() {
int a, b;
. . . <------------ 1
} /* end of sub1 */
void sub2() {
int b, c;
. . . . <------------ 2
sub1();
} /* end of sub2 */
void main() {
int c, d;
. . . <------------ 3
sub2();
} /* end of main */
g = 3; # A global
def sub1():
a = 5; # Creates a local
b = 7; # Creates another local
. . . <------------------------------ 1
def sub2():
global g; # Global g is now assignable here
c = 9; # Creates a new local
. . . <------------------------------ 2
def sub3():
nonlocal c: # Makes nonlocal c visible here
g = 11; # Creates a new local
. . . <------------------------------ 3