1 / 49

Scope

Learn about the scope of identifiers in programming, including local and global variables, formal arguments, global constants, and functions. Understand the concept of masking and its impact on identifier access.

hfox
Download Presentation

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. Scope Global & Local Identifiers

  2. Scope Definition: the portions of a program where an identifier “is defined” (may be used).

  3. Local Variables Definition: a variable declared inside a Block. Scope: from the Declaration statement to the end of the Block

  4. Local Variables Example 1: void fun() { float f; int a; //scope of a begins ... } // scope of a ends void main() { ... a++; // syntax error: a undef. }

  5. Local Variables Example 2: void fun() { for (inti = 0; i < 3; i++) { cout << i; } // scope of i ends here cout << i; // syntax error: iundef }

  6. Local Variables Example 3: void fun(int x) { cout << i; // syntax error: iundef if (x < 0) { inti=x; // scope of i begins cout << i; // OK ... } // scope of i ends cout << i; // syntax error: iundef }

  7. Formal Arguments are Local // scope of arguments is entire function void fun(int x) // scope of x begins { ... } // scope of x ends void main() { ... x++; // syntax error: x undef. }

  8. Global Variables Definition: a variable declared outside any Block. Scope: from the Declaration statement to the end of the source file

  9. Global Variables Allocation: before main() begins Deallocation: after main() ends

  10. Global Variables using namespace std; int x = 3; // scope of x begins void fun() { ... x--; // OK } void main() { ... x++; // OK } // scope of x ends

  11. Global Variables but... Use of Global Variables is a Major Violation of Structured Programming! (almost as bad as TWTSNBU) [The Word That Shall Not Be Uttered]

  12. Global Variables Why are Global Variables a Major Violationof SP? 1. Inhibits Reuse of Functions int x; // global ... // 300 lines of code here // So Great! copy/paste/use in ANY program! void geniusFunction() { ... x = x * 2; // don’t forget to copy/paste ... // global declaration of x! }

  13. Global Variables Why are Global Variables a Major Violation of SP? 2. Side Effect:value of a global variable changes for no apparent reason int x; // global void fun() { ... x++; ... } void main() { x = 0; fun(); cout << x; // prints 1 } //where did x change??

  14. Global Constants Definition: a constant declared outside any block. Scope: same as Global Variables Allocation: same as Global Variables Deallocation: same as Global Variables

  15. Global Constants but... They are NOT considered a Major Violation of Structured Programming

  16. Global Constants Why are they not a Major Violation of SP? 1. No Side Effects since constants don’t change value 2. Promote Consistency value is the same for ALL functions But: They DO inhibit reuse, but techniques can be used to limit problems Therefore: Advantages outweigh Disadvantages

  17. Global Constants // same value of pi for all functions const double pi = 3.1415926; double circleArea(double r) { return pi * r * r; } double circumference(double r) { ... } double sphereVolume(double r) { ... } double sphereSurface(double r) { ... }

  18. Masking Definition: when a local identifier has the same name as a global (oll) identifier, blocking access to the global (oll) identifier. oll: “or less-local”

  19. Masking Also called Hole In Scope

  20. Masking int x = 0; // global x declared void fun1 () { x = 5; // changes global x } void fun2() { float x; // local x declared x = 3; // changes local x, } // not the global x void main() { x = 4; // changes global x }

  21. Masking void main() { int x = 0; // local x if (x <= 0) { float x; // more-local x x = 4; cout << x; // prints 4 } cout << x; // prints 0 }

  22. Global Functions Definition: a function declared outside any other any block. Scope: where the function may be invoked. - begins where the function is Declared or Prototyped - ends at the end of the source file.

  23. Local Functions Definition: a function declared inside the declaration of another function. Scope: inside the “outer” function only Not allowed by most C++ compilers but available in other languages

  24. Local Functions void globalFun() { void localFun() { // C++ syntax error ... // just cut/paste } // outside/before ... // globalFun(){ } localFun(); } // end globalFun()

  25. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  26. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  27. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  28. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  29. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  30. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  31. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  32. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  33. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  34. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; //note: y & z out of scope! } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  35. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  36. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  37. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  38. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; }

  39. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; } program ending...

  40. Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; } program ended

  41. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; } cout << x; }

  42. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; } cout << x; }

  43. Semantics: Masking void main() { int x = 0; if (x <= 0) { True! float x; x = 4; cout << x; } cout << x; }

  44. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; } cout << x; }

  45. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4;Changes the Most-Local cout << x; } cout << x; }

  46. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } cout << x; }

  47. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } locals declared in this block are deallocated cout << x; }

  48. Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } cout << x; }

  49. Vocabulary

More Related