Scope of a Variable
The scope of a variable refers to where the variable can be used.
Throughout the whole program (Global) or limited to only one module (Local).
It is important to make sure that variables are only used or altered when the programmer wants them to be.
Global Variables
Global Variables (which we have used at N5) can be used throughout the whole program in every module. Although this is easier to program it does mean that
- variable is open to being accidentally over written or changed.
- use more memory within the RAM as they need to be stored throughout the duration of the program.
Local Variables
Local variables can only be used in a specific module.
This means that variables are:
- more secure and will not be changed unexpectedly
- take up less memory within the RAM as they only need to be stored for the duration of that specific module
- it allows the module to be more “portable”. This means that the module can be used again within other programs