local vs global variables
DESCRIPTION
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 - PowerPoint PPT PresentationTRANSCRIPT
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– Cannot be accessed inside a function if that function
has a variable by the same name, unless the scope resolution operator is used
#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;}
#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;}
Global variablesVariables that are defined outside a function
void Func( ); // function prototypeint Num =2; // global variablevoid 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
2
Num
Global variablesVariables that are defined outside a function
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
2
Num
Global above main
Global variablesVariables that are defined outside a function
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
50
Num
Global variablesVariables that are defined outside a function
void Func( ); // function prototypeint Num =2; // global variablevoid 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
Num2
Global variablesVariables that are defined outside a function
void Func( ); // function prototypevoid 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
Global variablesVariables that are defined outside a function
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
int Num =2; // global variable defined between // main and Func
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
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
Storage classification of variables
• 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
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; }
void main( ){ ShowLocal( ); ShowLocal( ); return 0;}void ShowLocal(void){ int LocalNum=5; cout << “localNum is “ << LocalNum << endl; LocalNum = 99; } // displays 5 always
void ShowLocal(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
void ShowStatic(void);
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; } }
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; } }
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; } }
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
FunctionReturn Value
Returning a value from a function
int Square (int); // function prototypevoid main(void){ int Value, Result; cout << “Enter a number and I will square it:”; cin >> Value; Result = Square (Value);
int Square (int Number){ return Number * Number; }
cout << Value << “squared is” << Result;}
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.
#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
bool IsEven(int Number){ if ( Number % 2 ) return 0; else return 1;}
Number has a copy of whatever is inside Val in main
5
5Val
bool IsEven(int Number){ if ( Number % 2 ) return 0; else return 1;}
Number has a copy of whatever is inside Val in main
5
5Val
5/2 is 2 with a remainder of 1
bool IsEven(int Number){ if ( Number % 2 ) return 0; else return 1;}
Number has a copy of whatever is inside Val in main
5
5Val
5/2 is 2 with a remainder of 1