Scope

Prior | Next

Scope
Scope refers to some part of your program in which a given function or variable is known to the compiler.  Functions and procedures have scope from the point that they are defined downward in your program.  That is why they must be defined before they are called.

A global variable is in scope at all points in your program.  Global variables are declared in the var section at the top of a program, before functions and procedures are defined.

A variable local to some procedure only has scope within that procedure.  Local variables are declared in the var section in the procedure or function definition.  Example:

var {variables declared here have global scope}
  grInterestRate : real;
  gsFileName : string;

procedure PrintReport;
var {variables declared here are local to PrintReport}
  i : integer;
begin
  {statements go here}
end;

begin {main program block, execution starts here}
  grInterestRate := 0.035;
  PrintReport
end;

If you try to access a local variable outside of the procedure or function where it is defined, the compiler displays an Unknown Identifier error.  Variables local to a procedure or function can be used again as local variables in another procedure or function.

Prior | Next