Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Scoping and Binding Each of the following programs is written in a C-like not

ID: 3621550 • Letter: 1

Question

1. Scoping and Binding
Each of the following programs is written in a C-like notation, but is not C.
(a) Write in the boxes what would be printed if the language had each type
of semantics. Justify your answers.
void main() {
int x = 1;
void func2() {
x = x + 5;
}
void func1() {
int x = 9;
func2();
printf("%d ", x);
}
func1();
}
Static Scoping
Dynamic Scoping
(b) As in part (a), write in the boxes what the program would print under
each type of semantics.
int a = 8;
void foo() {
a = a + 2;
printf("%d ", a);
}
void baz(void (*f)()) {
int a = 3;
(*f)(); /* call foo */
}
void main() {
int a = 7;
baz(foo); /* capture foo ptr. */
}
Static Scoping
Dynamic scoping where the
environment is bound when the
function pointer is captured
Dynamic scoping where the
environment is bound when the function pointer is called

Explanation / Answer

k