Units and Modular Programming
A Pascal unit is a separately compiled module that groups related constants, types, variables, procedures, and functions under a single name, declared with the keyword unit instead of program. Rather than writing one enormous source file, you split a system into units such as MathUtils, StringHelpers, or DatabaseIO, each compiled once and reused across many programs. This mirrors how professional software is built: small, well-defined pieces that can be tested, versioned, and swapped independently of the whole.
Cricket analogy: Think of a unit like a specialist bowling attack — Bumrah handles the new ball, Ashwin handles the middle overs — each bowler is a self-contained module the captain plugs into the attack rather than one bowler trying to do everything for 50 overs.
Anatomy of a Unit
Every unit has two mandatory sections: interface, which declares what other code may see and use (procedure/function headers, public types, exported constants), and implementation, which contains the actual code bodies and any private helper routines not listed in the interface. Optionally, a unit can include initialization and finalization blocks that run automatically when the unit is loaded and unloaded, useful for setting up resources like opening a log file or a lookup table before any calling code runs.
Cricket analogy: The interface section is like a team sheet handed to the umpire before the match — only the players it names can take the field — while the implementation is the actual technique each player practiced in the nets that the opposition never sees.
unit MathUtils;
interface
function Factorial(n: Integer): Int64;
function IsPrime(n: Integer): Boolean;
implementation
function Factorial(n: Integer): Int64;
begin
if n <= 1 then
Factorial := 1
else
Factorial := n * Factorial(n - 1);
end;
function IsPrime(n: Integer): Boolean;
var
i: Integer;
begin
IsPrime := True;
if n < 2 then
begin
IsPrime := False;
Exit;
end;
for i := 2 to Trunc(Sqrt(n)) do
if n mod i = 0 then
begin
IsPrime := False;
Exit;
end;
end;
initialization
Writeln('MathUtils loaded');
end.Using Units and the uses Clause
A program or another unit pulls in a module with a uses clause listing unit names separated by commas, for example uses MathUtils, StringHelpers;. The compiler locates each unit's compiled form (a .tpu in Turbo Pascal or .ppu/.o in Free Pascal), links it in, and only needs to recompile a unit if its source changed, which dramatically speeds up rebuilding large projects compared to recompiling everything from scratch every time.
Cricket analogy: Selecting players from the wider domestic circuit for the national squad is like a uses clause pulling proven, already-'compiled' talent into the main XI instead of training fresh recruits from zero each series.
Because unit compilation is incremental, large Pascal projects compile fast even with dozens of units — only units whose source changed since the last build get recompiled, while unchanged units are simply relinked from their existing compiled form.
Scope, Visibility, and Modular Design Benefits
Because only the identifiers listed in a unit's interface section are visible to code that uses it, modular design naturally enforces encapsulation: internal helper functions, private counters, and implementation details stay hidden, reducing naming collisions across a large codebase and letting a unit's internals be rewritten freely as long as its public interface contract stays the same. This separation of 'what' from 'how' is the same principle behind information hiding in later object-oriented languages.
Cricket analogy: A bowling coach can completely redesign a bowler's run-up and wrist position in the nets, but as long as the ball still arrives on a good length on match day, the 'interface' the batsman sees is unchanged.
Two units cannot both directly reference each other in their interface sections — this creates a circular dependency that Free Pascal and Turbo Pascal will reject at compile time. If two units genuinely need each other's declarations, move the shared cross-references into the implementation section (where circular uses is allowed) or factor the shared pieces into a third, lower-level unit that both depend on.
- A unit begins with
unit UnitName;and containsinterfaceandimplementationsections. - Only declarations in the
interfacesection are visible to code that uses the unit; everything inimplementationis private. initializationandfinalizationblocks run automatically when a unit is loaded and unloaded.- The
usesclause imports one or more units, and the compiler links in already-compiled versions when unchanged. - Modularity enforces encapsulation, reduces naming collisions, and allows internals to change without breaking callers.
- Circular references between two units' interface sections are not allowed and must be resolved via the implementation section or a shared lower-level unit.
Practice what you learned
1. Which two sections are mandatory in every Pascal unit?
2. What keyword is used to import a unit into a program or another unit?
3. Where should code go if it should NOT be visible to programs that use the unit?
4. What happens when two units try to reference each other directly in their interface sections?
5. What is the purpose of a unit's `initialization` block?
Was this page helpful?
You May Also Like
Object Pascal Basics
Get started with Object Pascal's classes, methods, constructors, and inheritance — the object-oriented extension of standard Pascal.
File Handling in Pascal
Master reading and writing text, typed, and untyped files in Pascal, along with safe error handling for I/O operations.
Exception Handling in Pascal
Learn how try..except and try..finally let Object Pascal programs handle errors gracefully and guarantee cleanup.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics