Tutorial - Global Values

Up to now all named values, have been defined within the scope of a routine. These have been formal parameters and local variables.

Global Values

When the entire program is contained in a single source file there is a global scope that can contain values that are accessible to all the routines within the file. values can be either constants or variables. Named values are variables by default and only become constant if explicitly flagged as such with the 'Const' keyword. All values can contain initialisers. For constants there is little point in not having an initialiser. Routine names are global values.

The syntax of the declaration of a global variable is the same as for a local value. The key difference is where the declaration occurs. To be global it must occur outside any routine definition.

[ Const ] type identifier [ = expression ] [[ , identifier [ = expression ] ]] ;

Here are some examples :-

Const Number MaxAge = 100;
Number MaxSoFar = 0;
Number TwiceMax = 2 * MaxAge;
Logical ValueSet = False;
prog_globals_1.grs

The above examples illustrate a number of points. The values can be of any type. Also the initialisation expression can use values already initialised. You can have multiple declarations within the same statement but all values will have the same type and the same constant attribute.

Identifiers

So far we have named a number of objects :- Shapes, Functions, formal parameters, local variables and now global variables and constants ( and there are more things to come ). So it is time to discuss identifiers.

Any string of characters use to name some object is known as an "identifier". There are strict syntactic rules and optional conventions for forming identifiers. They must consist of just the standard ASCII alphabet, decimal digits and the underscore character '_', and it must start with a letter. The maximum length of an identifier is 30 characters. Names are case sensitive - so 'wibble', 'WIBBLE' and 'Wibble' are all distinct. An identifier cannot be the same as a keyword ( e.g. Begin, For, If etc. ). You cannot declare the same identifier twice in the same scope. A local declaration will hide a global ( you will probably never want to do this ).

By convention all the local identifiers ( local variables and formal parameters ) are all lower case and Global Identifiers are mixed case ( this means whenever you see an identifier you will know where to look for its declaration ).