This section describes rules on scope of global variables defined in the main code and local variables defined in procedures.
Variable Scope - The area of source code where a variable is accessible.
If you are not using procedures, variable scope is very simple. The scope of a variable is: from the statement
where it is defined to the last statement of the code.
If you are using procedures, variable scope gets more complex. Here are some basic rules:
1. Global - If a variable is defined in the main code, its scope is from the statement where it is defined
to the last statement of the entire code including all procedures.
2. Local - If a variable is defined in a procedure code, its scope is from the statement where it is defined
to the last statement of the procedure.
3. Collision - If a variable is explicitly defined in a procedure code has the same name as a variable defined
in the main code, the variable of the main code become in-accessible within this procedure.
There are some interesting consequences of those rules:
The nice thing about rule #1 is that variables defined the main code are automatically accessible in all procedures.
You don't have to pass them as reference arguments to share them in a procedure.
The bad thing about rule #2 is that if you are using temporary variable in a procedure without explicit
declaration, you could accidentally change the value of a global variable of the same name.
Rule #3 helps us to avoid the bad impact of rule #3, if you declare all temporary variables explicitly in procedures.