Prior | Next

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;
  {statements go here}

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

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