Scope of Variables and Functions
- Scope of a variable/function refers to the area of the program where
the variable/function has a valid existence
- Global Variables/functions:
- Valid everywhere in the program
- It is desirable to minimize the number of global variables
- Declared outside the body of any function
- Created and initialized before the program is started
- Remain in existence until the program terminates
- Local Variables/Functions:
- Scope limited to the area within the block in which they are
declared
- They are inaccessible outside their block
- Allocated from the stack. Too many local variables may cause
stack overflow
- Variables are initialized when program control moves into the
block
- Variables are temporary in nature. Also called automatic
variables
- Static Variables:
- Static variables are permanent in nature
- They are initialized only once - when the program control first
moves into the block
- Their scope is restricted to the block within which they are
defined
- Example ...
- If two variables (one global, and the other local) with the same name
are defined, the local one takes precedence over the global one, within
its scope. Example...
Back to Previous Page
Document:
Local Date:
Last Modified On: