CSE 341 -- Section Handout on Scoping
November 29, 1995
Section Handout on Scoping
Look at the following C code:
int x = 3;
int Eggplant(int a)
{
/* CHECKPOINT */
return a + x;
}
int Toaster( int x )
{
return Eggplant( x );
}
void main(void)
{
printf("%d\n",Toaster(5));
}
Suppose C function calls in C are implemented with stack frames which include static and dynamic links. Draw the state of the of the stack at the CHECKPOINT. Be sure to draw in the static and dynamic links.
Suppose that C is statically scoped. What is printed?
8
Suppose that C is dynamically scoped. What is printed then?
10
Suppose that C allowed you to nest functions, and consider the following code:
int x = 3;
int z = 4;
int Squid(int x)
{
int MegaSquid(int y)
{
if(y == 1) {
return MegaSquid(0);
} else {
/* CHECKPOINT */
return x + z;
}
}
MegaSquid(1);
}
int Clam(int z)
{
printf("%d\n",Squid(5));
}
main()
{
Clam(6);
}
Draw the state of the stack including static and dynamic links at the CHECKPOINT.
Does it matter if this program runs statically or dynamically scoped?
yes.
Static Scoping: 9
Dynamic Scoping: 11