Beginner

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:

ExtensionPurpose
.dprDelphi Project - main program file
.pasPascal unit file (modules/libraries)
.dfmDelphi Form file (visual form definitions)
.dpkDelphi 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:

1
2
3
4
5
6
# Pull the Free Pascal image
docker pull freepascal/fpc:3.2.2-slim

# Compile and run with Delphi mode
docker run --rm -v $(pwd):/app -w /app freepascal/fpc:3.2.2-slim \
    sh -c "fpc -Mdelphi hello.dpr && ./hello"

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 /app
  • freepascal/fpc:3.2.2-slim - The Free Pascal compiler image
  • fpc -Mdelphi hello.dpr - Compile in Delphi mode
  • ./hello - Run the compiled executable

Running Locally

If you have Free Pascal installed locally:

1
2
3
4
5
# Compile in Delphi mode
fpc -Mdelphi hello.dpr

# Run
./hello

Installing Free Pascal

macOS (Homebrew):

1
brew install fpc

Ubuntu/Debian:

1
sudo apt install fpc

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 printf
  • string type - Delphi’s dynamic string (not fixed-length like standard Pascal)

Compiler Modes in Free Pascal

Free Pascal supports multiple Pascal dialects:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Delphi mode (most compatible with commercial Delphi)
fpc -Mdelphi myprogram.dpr

# Object Pascal mode (FPC's native mode)
fpc -Mobjfpc myprogram.pas

# Turbo Pascal mode (compatibility with TP 7)
fpc -Mtp myprogram.pas

# ISO Pascal mode (strict standards compliance)
fpc -Miso myprogram.pas

For Delphi code, always use -Mdelphi to ensure maximum compatibility.

Key Differences from Pascal

FeatureStandard PascalDelphi
StringsFixed-lengthDynamic, reference-counted
Unitsvariesuses clause with interface/implementation
OOPNoneFull class support
ExceptionsNonetry/except/finally
GenericsNoneFull 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:

  • .dpr for programs
  • .pas for units

Missing the final period

end;   { WRONG - semicolon }
end.   { CORRECT - period ends program }

Compiler Options

Useful Free Pascal flags for Delphi development:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Delphi mode with all warnings
fpc -Mdelphi -vw hello.dpr

# Optimize for speed
fpc -Mdelphi -O3 hello.dpr

# Debug build with line info
fpc -Mdelphi -g -gl hello.dpr

# Check for Delphi compatibility issues
fpc -Mdelphi -Sc hello.dpr

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
Last updated: