Beginner

Hello World in Pascal

Your first Pascal program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Pascal program.

The Code

Create a file named hello.pas:

program Hello;

begin
    WriteLn('Hello, World!');
end.

Understanding the Code

  • program Hello; - Declares the program name. This is optional in modern Pascal but good practice.
  • begin - Marks the start of the main program block.
  • WriteLn('Hello, World!'); - Outputs text to the console followed by a newline. Write would output without a newline.
  • end. - Marks the end of the program. Note the period (.) - this is required and different from the semicolon used elsewhere.

Why WriteLn?

Pascal distinguishes between:

  • Write - outputs text, cursor stays on same line
  • WriteLn - outputs text and moves to next line (“Write Line”)

This explicit naming is typical of Pascal’s emphasis on clarity.

Running with Docker

The easiest way to run Pascal without installing a compiler locally:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# Create the source file
cat > hello.pas << 'EOF'
program Hello;

begin
    WriteLn('Hello, World!');
end.
EOF

# Compile and run with Free Pascal
docker run --rm -v $(pwd):/app -w /app frolvlad/alpine-fpc \
    sh -c "fpc hello.pas && ./hello"

Running Locally

If you have Free Pascal installed:

1
2
3
4
5
# Compile
fpc hello.pas

# 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.

Expected Output

Hello, World!

A Slightly Longer Example

Let’s add some more Pascal features:

program HelloExtended;

var
    name: string;

begin
    Write('What is your name? ');
    ReadLn(name);
    WriteLn('Hello, ', name, '!');
    WriteLn('Welcome to Pascal programming.');
end.

This demonstrates:

  • var section - Declaring variables before use (required in Pascal)
  • string type - Text data type
  • ReadLn - Reading input from the user
  • Multiple arguments to WriteLn - Pascal can output several values in one call

Key Concepts

  1. Pascal is compiled - Source code (.pas) is compiled to a native executable
  2. Program structure - Programs have a specific structure: program, declarations, beginend.
  3. Case insensitive - WRITELN, WriteLn, and writeln are all the same
  4. Semicolons separate statements - But note: no semicolon before end
  5. Period ends the program - The final end. uses a period, not semicolon

Common Beginner Mistakes

Missing the final period

end;   { WRONG - should be end. }
end.   { CORRECT }

Semicolon before end

begin
    WriteLn('Hello');   { No semicolon needed here }
end.

While extra semicolons usually don’t cause errors, understanding that semicolons separate statements (rather than terminate them) helps with Pascal’s syntax.

Compiler Options

Useful Free Pascal flags:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Enable all warnings
fpc -vw hello.pas

# Optimize for speed
fpc -O3 hello.pas

# Delphi compatibility mode
fpc -Mdelphi hello.pas

# Debug information
fpc -g hello.pas

Historical Note

When Niklaus Wirth created Pascal in 1970, this simple “Hello World” program represented a significant step forward in programming language design. The clear, English-like syntax was intentionally designed to be readable and teachable - a stark contrast to the cryptic assembly language and complex ALGOL that preceded it.

Turbo Pascal later made this simplicity accessible to millions, running this program in under a second on hardware that would seem impossibly primitive today.

Next Steps

Continue to Variables and Data Types to learn about storing and manipulating data in Pascal.

Last updated: