The Scope of Objects
As mentioned above, constants, variables and types are visible in the block in which they are declared. Objects can be redeclared only in a nested scope, such as in a nested procedure, but not in the same scope.
Note that you can declare record components with the same name if they are in different record types. The following example declares two records, each of which has the components "firstname" and "lastname":
## type
## Child = record
## firstname: varying[20] of Char;
## lastname: varying[20] of Char;
## age: Integer;
## end;
## Mother = record
## firstname: varying[20] of Char;
## lastname: varying[20] of Char;
## num_child: 1..10;
## children: array[1..10] of Child;
## end;
The following example shows several different declarations of the variable "a_var," illustrating how the same name can be redeclared in nested and parallel scopes, each time referring to a different type:
## procedure Proc_A(a_var: type_1);
## procedure Proc_B;
## var
## a_var: type_2;
## begin
{A_var is of type_2}
## end;
## function Func_C(a_var: type_3) : Integer;
## begin
{Var is of type_3}
## end;
## begin
{A_var is of type_1}
## end;
Take special care when using variables with a declare cursor statement. The scope of the variables used in such a statement must also be valid in the scope of the open statement for that same cursor. The preprocessor actually generates the code for the declare at the point that the open is issued, and at that time, evaluates any associated variables.
For example, in the following program fragment, even though the variable "number" is valid to the preprocessor at the point of the declare cursor statement, it is not a valid variable name for the Pascal compiler at the point that the open is issued.
## procedure Init_Cursor; { Example contains an error }
## var
## number: Integer;
## begin
{ Cursor declaration includes reference to "number" }
## declare cursor c1 for
## retrieve (employee.name, employee.age)
## where employee.num = number
...
## end; { Init_Cursor }
## procedure Process_Cursor;
## var
## ename: varying[15] of char;
## eage: Integer;
## begin
{ Opening the cursor evaluates invalid "number" }
## open cursor c1
## retrieve cursor c1 (ename, eage)
...
## end; { Process_Cursor }