Units

A Unit packages shared type declarations, functions, procedures, constants, variables and more for invocation by other units or the main program, equivalent to modules / packages in other programming languages.

Creating a Unit

A unit consists of two core sections:

interface Section: Exposed content accessible directly after being imported by other files;

implementation Section: Internal private code only visible to the current unit and inaccessible externally.

Example

Create a file named MyUnit.pas to hold a unit implementation.

unit MyUnit;

{$ifdef FPC}
{$mode objfpc}{$H+}{$J-}
{$endif}

// Interface section: Public declarations
interface

procedure MyProcedure(const A: Integer);
function MyFunction(const S: string): string;

// Implementation section: Private code logic
implementation

procedure MyProcedure(const A: Integer);
begin
  WriteLn('A + 10 is: ', A + 10);
end;

function MyFunction(const S: string): string;
begin
  Result := S + 'strings are automatically managed';
end;

end.Code language: JavaScript (javascript)

Observant readers may notice the trailing end. lacks a matching begin?

The grammatical structure of a Pascal unit differs from a program

program Main Program: Requires begin ... end. as the program entry point for execution

unit Unit: No global begin required

All units terminate with end. (trailing dot marks file end, no corresponding global begin)

  • Declare function/procedure prototypes, types, constants and global variables inside interface; external code may call them after using uses;
  • implementation holds full function implementations; local procedures and private variables defined here remain invisible to external units;
  • Unit files start with unit Name; and end with end..

Importing Units in Main Program

Our main program file is mydemo.pas

Load custom units with the uses keyword

{$ifdef FPC} {$mode objfpc}{$H+}{$J-} {$endif}
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif}

program MyProgram;

// Import custom unit MyUnit
uses
  MyUnit;

begin
  WriteLn(MyFunction('Note: '));
  MyProcedure(5);
  ReadLn;
end.Code language: PHP (php)

The MyUnit; line under uses denotes the unit name, not the .pas file name.

This name comes from the first line of MyUnit.pas: unit MyUnit;

While unit and filenames can differ, matching them is recommended. Even when mismatched, uses always references the unit name, not the filename.

Now compile and run; the main program invokes functions from the unit.

Now compile and run

After compilation, two new files appear in the project directory

.ppu (Pascal Unit File, unit compilation metadata file)

Only generated when compiling unit files; main program files do not produce .ppu files

  • Stores compiled metadata for the unit interface section: function prototypes, types, constants, exported symbols;
  • When other files run uses MyUnit, the compiler does not reparse the full MyUnit.pas source code and directly reads the .ppu file to quickly validate interfaces;
  • Greatly accelerates compilation for large projects; core file for FPC incremental compilation.

MyUnit.o: Binary machine instructions compiled from MyUnit.pas (full implementation section code)

mydemo.o: Machine code binary for the mydemo.pas main program

The compiler first translates each .pas file into independent binary .o object files; the linker then merges all .o files into a single executable .exe.

Leave a Reply

Your email address will not be published. Required fields are marked *