My teacher has provided the following pseudo-code, and says that the output using static scope is 1 2 3
, but the output using dynamic scope is 2 3 4
.
The Challenge is in Static Scope we use a=1, b=2, c=3 without attention to main or no, use a=1, b=2, c=4? Just in Static Scope without including C Rules.
void fun1(void);
void fun2(void);
int a=1, b=2, c=3;
int main() {
c=4;
fun1();
return 0;
}
void fun1() {
int a=2, b=3;
fun2();
}
void fun2(){
printf("%d%d%d", a,b,c);
}
If dynamic scope were possible in C, then lookup of the variables
a
,b
andc
inside offun2
would use the dynamic environment.This, in turn, depends on how the function got actually called. Since it's called from
fun1
the variable bindings of that scope will be used (thusa = 2
andb = 3
). Becausefun1
had been called frommain
, which set up the bindingc = 4
, the output would be2 3 4
.Another example:
Would print
Perhaps actually seeing the difference helps you. Clojure supports both dynamic and lexical scoping (this is the correct terminology, btw):
(live)
Note that Clojure tries to be as purely functional as possible, thus none of the code above is a assignment (in other words: the values of the variables aren't modified once assigned)