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.Writewould 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 lineWriteLn- 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:
| |
Running Locally
If you have Free Pascal installed:
| |
Installing Free Pascal
macOS (Homebrew):
| |
Ubuntu/Debian:
| |
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:
varsection - Declaring variables before use (required in Pascal)stringtype - Text data typeReadLn- Reading input from the user- Multiple arguments to
WriteLn- Pascal can output several values in one call
Key Concepts
- Pascal is compiled - Source code (
.pas) is compiled to a native executable - Program structure - Programs have a specific structure:
program, declarations,begin…end. - Case insensitive -
WRITELN,WriteLn, andwritelnare all the same - Semicolons separate statements - But note: no semicolon before
end - 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:
| |
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.