Program

Prior | Next

Program Structure
All procedures and functions must be declared before they can be used, as illustrated by this example:

procedure First(x : integer);
var
  s : string;
begin
  s := 'Value is ' + inttostr( x );
  writeln( s );
end;

function Second(r : real) : string;
begin
  First( 5 );
  writeln( r );
  Result := 'Success';
end;

var
  MyString : string;
begin {Main Program, execution starts here}
  MyString := Second( 5.6 );
  writeln( MyString );
end;

Execution starts at the begin..end block that is not part of a procedure or function definition.  The starting point has been marked with a comment so you can recognize it.  The main program calls a function named Second, which has to be defined ahead of the main program block.

The function named Second calls a procedure named First.  Therefore, the procedure named First has to be defined ahead of the function named Second.

You might want to think of this structure as building upward.  Write your main program block, and then write the definitions for procedures and functions as needed at the top of the script.  Always add new procedures and functions to the top of the script which will naturally define them ahead of the program statements that call them.

Prior | Next