1 / 27

Local vs Global variables

Local variable Known only to the function in which it is declared Cannot be accessed outside the function. Local vs Global variables. Global variable Known to all functions in the same file Can be known to other functions in other files

saxton
Download Presentation

Local vs Global variables

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. Local variable Known only to the function in which it is declared Cannot be accessed outside the function Local vs Global variables

  2. Global variable Known to all functions in the same file Can be known to other functions in other files Cannot be accessed inside a function if that function has a variable by the same name, unless the scope resolution operator is used Local vs Global variables

  3. #include <iostream.h>void Func(void); void main( ) { int Num = 1; // Num is a local variable cout << “in main, Num is” << Num << endl; Func( ); cout << “back in main, Num is still” << Num; } void Func(void) { int Num = 20; // Num is a local variable cout << “In Func, Num is” << Num << endl;}

  4. #include <iostream.h>void Func(void); void main( ) { int Num = 1; // Num is a local variable cout << “in main, Num is” << Num << endl; Func( ); cout << “back in main, Num is still” << Num; } void Func(void) { int a = 20; // Num is not known here cout << “In Func, Num is” << Num << endl;}

  5. Global variables Variables that are defined outside a function 2 void Func( ); // function prototype int Num =2; // global variable void main( ) { cout << “In main, Num is” << Num << endl; Func(); cout << “Back in main Num is ” << Num ; return 0; } // the first cout displays 2 for Num Num

  6. Global variables Variables that are defined outside a function Global above main 2 Num void Func( ) { cout << “In Func, Num is” << Num << endl; Num = 50; cout << “But, it is now changed to” << Num; } // the first cout displays 2 // the second cout displays 50

  7. Global variables Variables that are defined outside a function 50 Num void Func( ) { cout << “In Func, Num is” << Num << endl; Num = 50; cout << “But, it is now changed to” << Num; } // the first cout displays 2 // the second cout displays 50

  8. Global variables Variables that are defined outside a function void Func( ); // function prototype int Num =2; // global variable void main( ) { cout << “In main, Num is” << Num << endl; Func(); cout << “Back in main Num is ” << Num ; return 0; } // the first cout displays 2 for Num // the second cout displays 50 for Num 50 2 Num

  9. Global variables Variables that are defined outside a function void Func( ); // function prototype void main( ) { cout << “In main, Num is not visible”; Func(); cout << “Back in main Num is not visible” ; return 0; } int Num =2; // global variable defined between // main and Func

  10. Global variables Variables that are defined outside a function int Num =2; // global variable defined between // main and Func void Func( ) { cout << “In Func, Num is” << Num << endl; Num = 50; cout << “But, it is now changed to” << Num; } // the first cout displays 2 // the second cout displays 50

  11. Scope resolution operator :: int Val = 1; void main( ) { int Val = 100; cout << “The local variable is set to ” << Val << endl; // displays 100 cout << “The global variable is set to ” << ::Val ; // displays 1

  12. Scope resolution operator :: int Val = 1; void main( ) { int Val = 100; cout << “The local variable is set to ” << Val << endl; // displays 100 cout << “The global variable is set to ” << ::Val; // displays 1

  13. auto default type, automatic variables are the ones we’ve have been using extern tells the computer that the variable is defined elsewhere in the program register tells the computer to use one of the CPU’s registers static persists (it’s value remains) even after leaving function Storage classification of variables

  14. double Value = 3.5; void main( ) { extern double Value; // unnecessary in this use cout << “The value is” << Value << endl; register int Number; // use arithmetic/logic unit for (Number =2; Number <=120; Number*=2) cout << Number << endl; }

  15. void ShowLocal(void); void main( ) { ShowLocal( ); ShowLocal( ); return 0; } void ShowLocal(void) { int LocalNum=5; cout << “localNum is “ << LocalNum << endl; LocalNum = 99; } // displays 5 always

  16. void ShowStatic(void); void main( ) { for (int Count=0; Count < 5; Count++) ShowStatic( ); // function is called 5 times } void ShowStatic(void) { static int StatNum=5; // initialized only once cout << “StatNum is “ << StatNum << endl; StatNum ++; } // value persists // 5 6 7 8 9 will display for StatNum value

  17. Default arguments void DisplayStars(int = 10, int =1); void main( ) { DisplayStars( ); cout << endl; DisplayStars(5); cout << endl; DisplayStars(7,3); } void DisplayStars(int Cols, int Rows) { for (int Down = 0; Down < Rows; Down++) { for (int Across=0; Across < Cols;Across++) cout << ‘*’; cout << endl; } }

  18. Default arguments void DisplayStars(int = 10, int =1); void main( ) { DisplayStars( ); cout << endl; DisplayStars(5); cout << endl; DisplayStars(7,3); } void DisplayStars(int Cols, int Rows) { for (int Down = 0; Down < Rows; Down++) { for (int Across=0; Across < Cols;Across++) cout << ‘*’; cout << endl; } }

  19. Default arguments void DisplayStars(int = 10, int =1); void main( ) { DisplayStars( ); cout << endl; DisplayStars(5); cout << endl; DisplayStars(7,3); } void DisplayStars(int Cols, int Rows) { for (int Down = 0; Down < Rows; Down++) { for (int Across=0; Across < Cols;Across++) cout << ‘*’; cout << endl; } }

  20. Returning a value from a function A function may return a value back to the part of the program that called the function by using a return statement Argument Argument Argument Argument Return Value Function

  21. Returning a value from a function int Square (int); // function prototype void main(void) { int Value, Result; cout << “Enter a number and I will square it:”; cin >> Value; Result = Square (Value); cout << Value << “squared is” << Result;} int Square (int Number) { return Number * Number; }

  22. Write a program that asks the user to enter a number and calls a function which receives the number. The purpose of the function is to determine if the number is even or odd. The function will return a 0 or false if the number is odd or a 1 or true if the number is even.

  23. #include <iostream.h> bool IsEven(int); void main( ) { int Val; cout << “Enter an integer”; cin >> Val; if (IsEven(Val)) cout << Val << “is even\n”; else cout << Val << “is odd\n”; } 0 or 1

  24. 5 Val 5 bool IsEven(int Number) { if ( Number % 2 ) return 0; else return 1; } Number has a copy of whatever is inside Val in main

  25. 5 Val 5 bool IsEven(int Number) { if ( Number % 2 ) return 0; else return 1; } Number has a copy of whatever is inside Val in main 5/2 is 2 with a remainder of 1

  26. 5 Val 5 bool IsEven(int Number) { if ( Number % 2 ) return 0; else return 1; } Number has a copy of whatever is inside Val in main 5/2 is 2 with a remainder of 1

  27. 36 Val 36 bool IsEven(int Number) { if ( Number % 2 ) return 0; else return 1; } Number has a copy of whatever is inside Val in main 36/2 is 18 with a remainder of 0

More Related