Static vs Dynamic Scope Mark Boady
Scopes Static Scope Dynamic Scope Determine Variable Scope at compile time Dynamic Scope Determine Variable Scope at run time Static Scope is most likely what you are used to
Example 1 int x; int main(){ x=2; f(); g(); h(); } Void f() { int x=3; h();} Void g() { int x=4; h();} Void h() { printf(“%d ”,x);} Static Prints out: 2 2 2 Dynamic Prints out: 3 4 2
Example 2 Int m,n; Void hardy(){ printf(“in hardy n =%d”,n); } Void laurel(int n){ printf(“in laurel m=%d\n”,m); printf(“in laurel n=%d\n”,n); Hardy(); } Int main(){ m=50; n=100; printf(“In Main n=%d”,n); Laurel(1); hardy();
Example 2 Static Answer in main program -- n = 100 in laurel -- m = 50 in laurel -- n = 1 in hardy -- n = 100
Example 2 Dynamic Answer in main program -- n = 100 in laurel -- m = 50 in laurel -- n = 1 in hardy -- n = 1 in hardy -- n = 100