Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions Scope local global Global Resolution Operator part 5.

Similar presentations


Presentation on theme: "Functions Scope local global Global Resolution Operator part 5."— Presentation transcript:

1 Functions Scope local global Global Resolution Operator part 5

2

3 int x = 3; // global because before main
void main(void) { // no variables local to main( ) void myfunction( ); // prototype cout <<"x = "<<x<<" before the function call.\n"; myfunction( ); cout <<"x = "<<x<<" after the function call.\n"; } void myfunction( ) { int r; // local to myfunction( ) r = x++; cout <<"r = "<<r<<" within the function.\n"; // what happens to global x?

4 Scope OUTPUT x = 3 before the function call.
r = 3 within the function. x = 4 after the function call.

5 What will be the output? Do x or y change?
void swap(int, int); // a global function void main(void) { int x = 5, y = 10; 1. cout <<“Main-before swap, x: “<<x<<" y: "<<y<< '\n'; swap(x, y); 2. cout <<"Main-after swap, x: "<<x<<" y: "<<y<<'\n'; } void swap(int x, int y) { int temp; 3. cout <<"Swap-before swap, x: "<<x<<" y: "<<y<<'\n'; temp = x; x = y; y = temp; 4. cout <<"Swap-after swap, x: "<<x<<" y: "<<y<<'\n'; What will be the output? Do x or y change?

6 Scope OUTPUT 1. Main-before swap: x: 5 y: 10
3. Swap-before swap: x: 5 y: 10 4. Swap-after swap: x: 10 y: 5 2. Main-after swap: x: 5 y: 10

7 Scope within a block - Ex. 1 predict the output
void scope2(void); // function prototype void main(void) { int v=100; cout <<"v BEFORE function = "<<v<<'\n'; scope2(); cout <<"v AFTER function = "<<v<<'\n'; } slide 1 of 2

8 Scope within a block - Ex. 1 predict the output
1. void scope2(void) //function header 2. { double v = 5.5; 3. int k, j; 4. cout << "v outside block = " << v<<'\n'; 5. for (k=1; k<=3; k++) 6. { int v = 17; // initialized in function 7. for (j=1; j<=2; j++) 8. { v++; 9. cout << "v inside block = " << v<<'\n'; 10. } 11. } 12. cout << "v outside block = " << v<<'\n'; 13. } slide 2 of 2

9 } } Scope within a block j loops k loop * OUTPUT 100 BEFORE
5.5 outside 18 inside 19 inside outside 100 AFTER } k loop j loops } *

10 Variable Storage Classes - Section 6.5
Local auto static register ý while (l l l) { int k = 1; k++; l l l } What we’ve seen so far Sticks around between calls register - same as auto except memory in high speed registers while (l l l) { static int k = 1; k++; l l l } * * *

11 Variable Storage Classes
Global static extern ý Hidden from other files an extern variable’s scope is extended into a separate file See Section 6.5 and attempt Exercise 6.6 on page 286

12 Scope & Storage Classes - an example
int x = 1; // global variable main() { int x = 5; // local variable to main cout << "local x in outer scope of main is " << x << endl; { // start new scope int x = 7; cout << "local x in inner scope of main is " <<x<< endl; } // end new scope from Deitel & Deitel 1e, fig 3.12

13 Scope & Storage Classes - an example
(continued) a(); // a has automatic local x b(); // b has static local x c(); // c uses global x a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value cout << "local x in main is " << x << endl; return 0; } // end of main() from Deitel & Deitel 1e, fig 3.12

14 Scope & Storage Classes - an example
void a(void) { // automatic x int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; ++x; cout << "local x in a is " << x << " before exiting a" << endl; } from Deitel & Deitel 1e, fig 3.12

15 Scope & Storage Classes - an example
void b(void) { static int x = 50; // Static initialization only // first time b is called cout << endl << "local static x is " << x << " on entering b" << endl; ++x; cout << "local static x is " << x << " on exiting b" << endl; } from Deitel & Deitel 1e, fig 3.12

16 Scope & Storage Classes - an example
void c(void) { // uses x from nearest enclosing scope (global) cout << endl << "global x is " << x << " on entering c" << endl; x *= 10; cout << "global x is " << x << " on exiting c" << endl; } from Deitel & Deitel 1e, fig 3.12

17 Scope & Storage Classes - an example
b(); c(); a(); // a has automatic local x b(); // b has static local x c(); // c uses global x a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value from Deitel & Deitel 1e, fig 3.12 *

18 Scope & Storage Classes - an example
OUTPUT local x in outer scope of main is 5 local x in inner scope of main is 7 local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 50 on entering b local static x is 51 on exiting b global x is 1 on entering c global x is 10 on exiting c from Deitel & Deitel 1e, fig 3.12

19 Scope & Storage Classes - an example
local x in a is 25 after entering a local x in a is 26 before exiting a local static x is 51 on entering b local static x is 52 on exiting b global x is 10 on entering c global x is 100 on exiting c local x in main is 5 from Deitel & Deitel 1e, fig 3.12 What would the output be if each of the functions were called again?

20 Global (Scope) Resolution Operator ::
double bignum = ; // global void main(void) { double bignum = 120.3; // local cout<< bignum << “ = bignum, local\n” cout<< ::bignum << “ = bignum, global\n”; } OUTPUT = bignum, local = bignum, global * *

21 The Scope Resolution Operator ::
void a(void) { int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; ++x; cout << "local x in a is " << x << " before exiting a" << endl; } from Deitel & Deitel 1e, fig 3.12 << ‘\t’ << ::x << endl; What will print here? *

22 Misuse of Globals Globals allow programmers to “jump around” the safeguards provided by functions Do not make all variables global! Disastrous in larger programs with many user-defined functions Sometimes used in large programs to define a few variables and constants that must be shared by many functions Small programs almost never need globals

23 Common Errors Passing incorrect data types
Using the same variable name for different variables ex. local - in both the calling and called functions global - must use ::

24 Common Errors Wrong positioning of the called function prototype
Terminating a function header with a ; Forgetting the data type of a function’s parameter Remember the NOT rule: Number Order Type

25 Debugging Prevention - plan first! Valuation tables Display values

26 “Heavier-than-air flying machines are impossible.” Lord Kelvin,
End Note 1 “Heavier-than-air flying machines are impossible.” Lord Kelvin, President of the Royal Society, 1895


Download ppt "Functions Scope local global Global Resolution Operator part 5."

Similar presentations


Ads by Google