Sunday, March 20, 2011

Basic Program Structure and Syntax

A FreePascal program has the following general form.


PROGRAM ProgramName (FileList);
TYPE { Type declarations }
type = value;
CONST { Constant declarations }
constant = value;
VAR { Variable declarations }
variable = value;
FUNCTION FunctionName;
PROCEDURE ProcedureName (parameters);
{ Functions and Procedures }
BEGIN
{ Main statements }
END.


Every program begins with a program header. The header statement must be terminated by a semi-colon. The header is followed by type definitions, constant and variable declarations. Each of these are terminated by a semi-colon. Combined, the header, type, constant and variable structures make up the Declaration body of a FreePascal program.

Functions, procedures and the Main structures make up the Program body of a FreePascal program. Functions and procedures have names in their header which is terminated by a semi-colon. The main part of the program is terminated by a full-stop.

The general form of a procedure declaration is


PROCEDURE ProcedureName (parameter declarations);
TYPE { Type declarations }
type = value;
CONST { Constant declarations }
constant = value;
VAR { Variable declarations }
variable = value;
(PROCEDURE and FUNCTION declarations)
PROCEDURE ProcedureName (parameters);
{ Functions and Procedures }
BEGIN
{ Procedure statements }
END;


In other words, a procedure is a program within a program.