Pascal program structure
last modified November 18, 2026 Free Pascal 3.2.2
Every Pascal program is built from the same parts: an optional program header,
a list of used units, declaration sections, and a main block between
begin and end.. This article describes those parts,
the rules for comments and semicolons, and the compiler directives that select
the language mode.
A complete program
The listing below has all the parts of a typical program: a program header,
a uses clause, constant, type, and variable declarations, a
function, and the main block. The function is declared before the main block
because Pascal requires a name to be declared before it is used.
program structure;
{$mode objfpc}{$H+}
uses SysUtils;
const
Greeting = 'Total:';
type
TNumber = Integer;
var
total: TNumber;
function Add(a, b: Integer): Integer;
begin
Result := a + b;
end;
begin
total := Add(2, 3);
WriteLn(Greeting, ' ', total);
end.
The header names the program. The uses clause makes the
SysUtils unit available, although this particular program uses
only WriteLn, which is part of the System unit and always
available. The const, type, and var
sections declare a constant, an alias for Integer, and a variable.
The function Add has two integer parameters and returns an
integer; its body assigns the result to the built-in variable
Result. The main block calls the function and prints the result.
$ fpc -gl structure.pas $ ./structure Total: 5
The program header
The header has the form program name;. The name follows the same
rules as any identifier: it starts with a letter or an underscore and continues
with letters, digits, or underscores. The name does not have to match the file
name. Free Pascal also accepts a source file without a header, but the header
documents the file and is used throughout this series.
The header is a declaration, not a statement, so it ends with a semicolon. The
very last token of the file is a full stop after end, and nothing
may follow it except whitespace or comments. A program that ends with
end; instead of end. is rejected by the compiler.
The program name shares the global namespace with types and constants. A
program named boolean hides the type Boolean, and a
declaration that uses the type then fails with a syntax error. Name a program
after its topic, not after a type or a unit.
The uses clause
A uses clause lists the units whose declarations the program
needs. The System unit is used automatically and must not be listed. Other
units, such as SysUtils, Classes, Math,
and StrUtils, are named in a comma-separated list. The clause
comes after the program header and before the first declaration section. A
program has one uses clause; a unit has one in its interface part
and another in its implementation part.
The order of the units matters when two of them export the same name: the
unit listed later hides the earlier one. This is rarely a problem in small
programs, but it explains why some code lists cthreads first in
threaded programs. The procedures article
introduces units from the caller's side, and the unit article in the next group
shows how to write one.
Declaration sections
Pascal groups declarations by kind. A const section defines
constants, a type section defines new type names, and a
var section defines variables. The usual order is
const, type, var, followed by
procedures and functions, but a section may be repeated, and Free Pascal
accepts some arrangements that the ISO standard rejects. Following the usual
order keeps a program portable and easy to read.
A declaration is visible from the point where it is written to the end of the block that contains it. The main block can use every declaration above it; a procedure can use the declarations that appear before the procedure, plus its own local declarations. The next article, variables and declarations, describes scope in more detail.
The main block and semicolons
The main block starts with begin and ends with end..
Statements are separated by semicolons. A semicolon is a separator, not a
terminator, so the last statement before end does not need one.
An extra semicolon is allowed and denotes an empty statement, which is why a
line containing only ;; compiles.
program semicolons;
{$mode objfpc}{$H+}
begin
WriteLn('one');
WriteLn('two');
;;
WriteLn('three')
end.
The first two statements end with a semicolon, the third is followed by an
empty statement, and the last one has no semicolon before end.
The program is valid and prints all three lines. A missing semicolon between
two statements, on the other hand, is a syntax error, because the compiler
would read the second statement as part of the first one.
$ fpc -gl semicolons.pas $ ./semicolons one two three
Comments
Pascal has three comment forms: a brace comment { ... }, a
parenthesis comment (* ... *), and a line comment
// ... that runs to the end of the line. The first two can span
several lines. Comments are ignored by the compiler, but a comment that starts
with a dollar sign is a compiler directive, not a comment.
program comments;
{$mode objfpc}{$H+}
begin
{ a brace comment }
(* a parenthesis comment *)
// a line comment
WriteLn('comments'); // after a statement
end.
The directives at the top of the file use the brace form with a dollar sign after the opening brace. They are processed by the compiler and do not appear in the executable. The other comments in the listing are ordinary comments and have no effect on the program.
$ fpc -gl comments.pas $ ./comments comments
Case insensitivity
Pascal is case-insensitive. Keywords, type names, and identifiers may be
written in any case, and two names that differ only in case denote the same
thing. This is different from C, Java, and most modern languages, where
Total and total are distinct variables.
program names2;
{$mode objfpc}{$H+}
var
TotalCount: Integer;
begin
totalcount := 5;
WriteLn('TotalCount = ', TotalCount);
end.
The variable is declared as TotalCount and assigned as
totalcount. Both spellings refer to the same storage location, so
the program prints the value that was assigned. Declaring both names in the
same scope would be a duplicate identifier error, not two variables.
$ fpc -gl names2.pas $ ./names2 TotalCount = 5
Compiler directives
A compiler directive is written between braces or between parenthesis-star markers and starts with a dollar sign. Directives change how the compiler reads the rest of the file. The two used at the top of every program in this series select the ObjFPC dialect and the AnsiString representation. A directive takes effect at the point where it appears, so it can be switched in the middle of a file.
program hswitch;
{$mode objfpc}{$H+}
var
a: string;
{$H-}
var
b: string;
begin
WriteLn('a: ', SizeOf(a));
WriteLn('b: ', SizeOf(b));
end.
The first variable is declared while {$H+} is active, so
string means AnsiString and occupies eight bytes on
this machine. The directive {$H-} then switches to
ShortString, so the second variable occupies 256 bytes: one length
byte plus 255 characters. The switch affects declarations that follow it, not
the ones already compiled. Because the default in ObjFPC mode is
{$H-}, a program that omits the directive gets short strings.
Every source file in this series therefore starts with
{$mode objfpc}{$H+}. The
variables article explains the consequences
for assignment and length.
$ fpc -gl hswitch.pas $ ./hswitch a: 8 b: 256
Ending a program
A program normally ends when the main block reaches end. and the
exit status is zero. The procedure Halt ends the program
immediately and takes an optional exit status. A non-zero status tells the
shell or the calling program that something went wrong.
program exitstatus;
{$mode objfpc}{$H+}
begin
WriteLn('before halt');
Halt(3);
WriteLn('after halt');
end.
The second WriteLn never runs, because Halt(3) stops
the program at that point. The exit status is 3, which the shell reports in
$? after the program finishes. Without an argument, Halt
uses status zero.
$ fpc -gl exitstatus.pas $ ./exitstatus before halt $ echo $? 3
Programs and units
A program is one compilation unit with a main block. A unit is a separate file
that exports declarations and has no main block; a program uses it through a
uses clause. Units are the Pascal way of splitting a large program
into modules, and they are compiled separately into .ppu and
.o files. The next group of articles introduces them after the
core language.
Next steps
The variables and declarations article explains declarations, initial values, constants, and scope. The data types article describes the types a declaration can use, and the literals article shows how values are written. After that the series continues with operators, conditionals, loops, numeric functions, and procedures and functions.
Source
In this article we have described the parts of a Pascal program, the program header, the uses clause, the declaration sections, the main block, comments, semicolons, case insensitivity, compiler directives, and the way a program ends. The series continues with Pascal variables and declarations.
Author
List all Pascal tutorials.