Scope of a Variable

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. In order to do that some variables are not allowed to be changed throughout the whole program and can only be used in specified modules.

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 the variable is open to being accidentally over written or changed. Global variables also 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 much more secure and will not be changed unexpectedly during other parts of the programs operation. Local variables take up less memory within the RAM as they only need to be stored for the duration of that specific module and can then be deleted once that module is complete. If a module uses local variables it allows the module to be more “portable”. This means that the module can be used again within other programs without having to be reprogrammed.