ZetCode

Pascal units

last modified November 18, 2026 Free Pascal 3.2.2

A unit is a separately compiled Pascal source file that exports procedures, functions, types, constants, and variables to other code. Units are how Pascal programs are split into modules: the System unit is always present, SysUtils and Classes come with the compiler, and every larger project adds its own. This article shows how a unit is written, how a program uses it, how initialization and finalization sections work, and how the compiler finds and compiles the unit files.

What a unit is

A program has a main block between begin and end. and is compiled into an executable. A unit has no main block. It starts with the keyword unit and a name, and it is divided into an interface part and an implementation part. The interface declares what the unit makes available; the implementation contains the code and everything that stays private. Other code uses the unit by naming it in a uses clause.

The compiler turns a unit into two files: an object file with the machine code and a .ppu file with the interface information. The .ppu file is what a program needs at compile time. The unit name must match the file name, because the compiler looks for name.pas or name.pp when it sees uses name.

A first unit

The unit below exports two functions. Only their headers appear in the interface; the bodies are written again in the implementation. The unit ends with end., just like a program, but there is no main block before it.

mathutils.pas
unit mathutils;
{$mode objfpc}{$H+}

interface

function Square(n: Integer): Integer;
function Cube(n: Integer): Integer;

implementation

function Square(n: Integer): Integer;
begin
  Result := n * n;
end;

function Cube(n: Integer): Integer;
begin
  Result := n * n * n;
end;

end.

The interface repeats the full parameter list and the result type, so a caller can be compiled without reading the implementation. The implementation repeats the header and then supplies the body. A function that is declared in the interface but never implemented is reported by the compiler when the unit is built.

The next program uses the unit. The uses clause comes directly after the program header, and the functions are called as if they were declared in the program itself.

usemath.pas
program usemath;
{$mode objfpc}{$H+}

uses mathutils;

begin
  WriteLn('Square(5) = ', Square(5));
  WriteLn('Cube(3)   = ', Cube(3));
end.

The unit is compiled first, because the program needs its .ppu file. The compiler can also build the unit automatically when the program is compiled, as long as the source file is in the same directory or in a unit search path. The two commands below make the order explicit.

$ fpc -gl mathutils.pas
$ fpc -gl usemath.pas
$ ./usemath
Square(5) = 25
Cube(3)   = 27

Interface and implementation

A unit can keep state that the rest of the program cannot touch. The next unit counts calls in a private variable, which is declared in the implementation part. Only the three functions appear in the interface, so the counter itself cannot be changed from outside.

counter.pas
unit counter;
{$mode objfpc}{$H+}

interface

function Next: Integer;
function Total: Integer;

implementation

var
  count: Integer;

function Next: Integer;
begin
  Inc(count);
  Result := count;
end;

function Total: Integer;
begin
  Result := count;
end;

initialization
  WriteLn('counter: initializing');
  count := 100;

finalization
  WriteLn('counter: finalizing, last value ', count);
  Flush(Output);

end.

The variable count is visible to both functions but not to the program that uses the unit. The initialization section runs before the main block, and the finalization section runs when the program ends. The call to Flush(Output) is necessary because the run-time library flushes the standard output before it runs the finalization sections; text written after that point would otherwise stay in the buffer and be lost.

usecounter.pas
program usecounter;
{$mode objfpc}{$H+}

uses counter;

begin
  WriteLn('next = ', Next);
  WriteLn('next = ', Next);
  WriteLn('total = ', Total);
  WriteLn('program ending');
end.

The first call returns 101, because the initialization section has already set the counter to 100. The two following calls return 102. The finalization line appears after the last line of the main block, when the program is shutting down.

$ fpc -gl counter.pas
$ fpc -gl usecounter.pas
$ ./usecounter
counter: initializing
next = 101
next = 102
total = 102
program ending
counter: finalizing, last value 102

Qualified names and name clashes

Two units may export the same name. The unit that is listed later in the uses clause wins, and the earlier one is hidden. A qualified name, written as unitname.identifier, selects one of them explicitly. The next two units both export a procedure called Hello.

ua.pas
unit ua;
{$mode objfpc}{$H+}

interface

procedure Hello;

implementation

procedure Hello;
begin
  WriteLn('A');
end;

end.

The first unit prints A. It has no state and no initialization section, so the whole interface fits in one line.

ub.pas
unit ub;
{$mode objfpc}{$H+}

interface

procedure Hello;

implementation

procedure Hello;
begin
  WriteLn('B');
end;

end.

The program lists both units and calls each procedure by its qualified name. Without the qualifier, Hello would mean the version from ub, because ub is listed last.

qual.pas
program qual;
{$mode objfpc}{$H+}

uses ua, ub;

begin
  ua.Hello;
  ub.Hello;
end.

Both calls print their own letter. The qualification is optional when there is no clash, and it is the simplest way to resolve one without changing the order of the uses clause.

$ fpc -gl ua.pas
$ fpc -gl ub.pas
$ fpc -gl qual.pas
$ ./qual
A
B

Include files

An include file is not a unit. It is a source fragment that the compiler inserts where the {$I} directive appears. Include files usually hold constants, type declarations, or platform-specific code that several sources share. The file below contains only a constant declaration; it has no unit or program header and no final end..

settings.inc
const
  Answer = 42;

The program includes the file at the top level and then uses the constant in the main block. The compiler treats the inserted text as if it had been typed at that point.

incmain.pas
program incmain;
{$mode objfpc}{$H+}
{$I settings.inc}

begin
  WriteLn('answer = ', Answer);
end.

The include file is found relative to the source file or through the include search path. Because the text is inserted before the main block, the constant is visible to the program. An include file that declares a procedure must also provide its body somewhere, either in the same include file or in the source that includes it.

$ fpc -gl incmain.pas
$ ./incmain
answer = 42

Units and the compiler

The compiler searches for units in the current directory and in the directories listed with -Fu. When it finds a unit source and no current .ppu, it compiles the unit first. The resulting .ppu and .o files are written next to the source unless an output directory is set with -FU. A program that uses a unit does not link the unit source again; it links the compiled object file.

A unit is recompiled when its source is newer than its .ppu file. Editing an interface and rebuilding only the program may therefore produce errors that disappear after the unit is rebuilt. The safe habit is to build the unit first, or to let a build tool such as make or fpmake track the dependencies. The next group of articles uses units for arrays, strings, records, and classes, and each one compiles its units before the program that uses them.

Next steps

The arrays article describes static arrays, the dynamic arrays article shows arrays that grow at run time, and the strings article collects the string types and their operations. The records article introduces the composite type that units and classes are built from.

Source

Free Pascal Reference guide

Free Pascal: units

Pascal on Wikipedia

In this article we have written a unit with an interface and an implementation, used it from a program, kept private state, run initialization and finalization sections, resolved name clashes with qualified names, and included a source fragment with {$I}. The series continues with Pascal arrays.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Pascal tutorials.