1 / 6

Static vs Dynamic Scope

Static vs Dynamic Scope. Mark Boady. Scopes. Static 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. i nt x; i nt main(){ x=2; f(); g(); h(); } Void f() { int x=3; h();}

Télécharger la présentation

Static vs Dynamic Scope

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Static vs Dynamic Scope Mark Boady

  2. Scopes • Static 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: 2 2 2 Dynamic Prints out: 3 4 2

  4. Example 2 Intm,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 • 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

More Related