Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static vs Dynamic Scope

Similar presentations


Presentation on theme: "Static vs Dynamic Scope"— Presentation transcript:

1 Static vs Dynamic Scope
Mark Boady

2 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

3 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: Dynamic Prints out: 3 4 2

4 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();

5 Example 2 Static Answer in main program -- n = 100 in laurel -- m = 50
in laurel -- n = 1 in hardy -- n = 100

6 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


Download ppt "Static vs Dynamic Scope"

Similar presentations


Ads by Google