Hello World in Delphi
Your first Delphi program - the classic Hello World example with Docker setup using Free Pascal in Delphi mode
Every programming journey starts with Hello World. Let’s write our first Delphi program using Free Pascal’s Delphi compatibility mode.
The Code
Create a file named hello.dpr:
program Hello;
{$APPTYPE CONSOLE}
begin
WriteLn('Hello, World!');
end.
Understanding the Code
program Hello;- Declares the program name. In Delphi, this matches the filename (without extension).{$APPTYPE CONSOLE}- A compiler directive that creates a console application (as opposed to a GUI application). This ensures output goes to the terminal.begin- Marks the start of the main program block.WriteLn('Hello, World!');- Outputs text to the console followed by a newline.end.- Marks the end of the program. The period is required.
Delphi File Extensions
Delphi uses specific file extensions:
| Extension | Purpose |
|---|---|
.dpr | Delphi Project - main program file |
.pas | Pascal unit file (modules/libraries) |
.dfm | Delphi Form file (visual form definitions) |
.dpk | Delphi Package (component libraries) |
For console programs, .dpr is the standard extension, though Free Pascal will also accept .pas.
Running with Docker
The easiest way to run Delphi code without installing a compiler locally is using Free Pascal in Delphi compatibility mode:
| |
Docker Command Breakdown
docker run --rm- Run container and remove it when done-v $(pwd):/app- Mount current directory into container at /app-w /app- Set working directory to /appfreepascal/fpc:3.2.2-slim- The Free Pascal compiler imagefpc -Mdelphi hello.dpr- Compile in Delphi mode./hello- Run the compiled executable
Running Locally
If you have Free Pascal installed locally:
| |
Installing Free Pascal
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
Windows: Download from freepascal.org or install Lazarus IDE which includes FPC.
Commercial Delphi
For the full Delphi experience, Embarcadero offers:
- Delphi Community Edition - Free for individuals and small businesses
- Delphi Professional/Enterprise - Commercial licenses with full features
Download from embarcadero.com.
Expected Output
Hello, World!
Delphi vs Standard Pascal
While our Hello World looks almost identical to standard Pascal, Delphi adds the {$APPTYPE CONSOLE} directive. Here’s why:
// Without {$APPTYPE CONSOLE}, Delphi creates a GUI application
// The console window may not appear, and WriteLn may not work as expected
// With the directive, we explicitly request a console application
{$APPTYPE CONSOLE}
This distinction exists because Delphi was designed primarily for GUI development. Console applications are the exception, not the rule.
A Delphi-Style Example
Let’s use some Delphi-specific features:
program HelloExtended;
{$APPTYPE CONSOLE}
uses
SysUtils; // For Format function
var
Name: string;
begin
Write('What is your name? ');
ReadLn(Name);
// Using Delphi's Format function (like printf)
WriteLn(Format('Hello, %s!', [Name]));
WriteLn('Welcome to Delphi programming.');
end.
This demonstrates:
uses SysUtils;- Importing a unit (Delphi’s module system)Format()- Delphi’s formatted string function, similar to printfstringtype - Delphi’s dynamic string (not fixed-length like standard Pascal)
Compiler Modes in Free Pascal
Free Pascal supports multiple Pascal dialects:
| |
For Delphi code, always use -Mdelphi to ensure maximum compatibility.
Key Differences from Pascal
| Feature | Standard Pascal | Delphi |
|---|---|---|
| Strings | Fixed-length | Dynamic, reference-counted |
| Units | varies | uses clause with interface/implementation |
| OOP | None | Full class support |
| Exceptions | None | try/except/finally |
| Generics | None | Full generic support (modern Delphi) |
Common Beginner Mistakes
Forgetting {$APPTYPE CONSOLE}
program NoConsole;
begin
WriteLn('This may not appear!'); // GUI app has no console
end.
Using .pas extension with program keyword
While Free Pascal accepts this, Delphi convention is:
.dprfor programs.pasfor units
Missing the final period
end; { WRONG - semicolon }
end. { CORRECT - period ends program }
Compiler Options
Useful Free Pascal flags for Delphi development:
| |
Historical Note
When Borland released Delphi 1.0 in 1995, it transformed Windows development overnight. The combination of visual design and compiled performance was unprecedented. Anders Hejlsberg’s team had created something special - a tool that made professional Windows development accessible while never sacrificing performance.
The language was so well-designed that C#, released by Microsoft in 2000 with Hejlsberg as lead architect, borrows heavily from Delphi concepts including properties, interfaces, and component-based design.
Next Steps
Continue to Variables and Data Types to learn about Delphi’s type system and its extensions beyond standard Pascal.
Running Today
All examples can be run using Docker:
docker pull freepascal/fpc:3.2.2-slim