Skip to main content

Hello World (first programs) · Beginner · 13 min

Hello World in Pascal with Free Pascal: fpc and procedures

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Free Pascal 3.2.2 (fp-compiler 3.2.2+dfsg-49)

You will install Free Pascal 3.2.2, compile a Hello World program into a native executable with fpc, then read a name from the keyboard into a declared variable and greet it from your own procedure.

On this page

Pascal was designed by Niklaus Wirth around 1970 to teach programming clearly, and that purpose still shows. Every variable is declared, with its type, before the code that uses it. Assignment is written :=, so it never gets confused with the = that compares two values. The compiler refuses to put text into a number. These rules feel strict on day one and save you hours later.

Pascal is not only a teaching language. Delphi, its commercial descendant, has run desktop and business software for decades, and a lot of that software is still maintained: accounting, point-of-sale and stock systems built in the 1990s and 2000s. Lazarus, a free IDE built on Free Pascal, makes Windows, Linux and macOS desktop apps from the same source.

Free Pascal (FPC) is an open-source compiler. It turns your .pas file straight into a native executable for your processor. On Linux it does not even need the C library: the program it produces talks to the kernel through Free Pascal's own runtime library.

You work in a throwaway folder, ~/hello-pascal-demo, and delete it at the end.

Step 1 · Install Free Pascal

On Ubuntu 26.04 the compiler is in the fp-compiler package. It also pulls in the linker that fpc uses:

Bash
sudo apt update
sudo apt install -y fp-compiler

Ask the compiler for its version:

Bash
fpc -iV

On Windows, download the installer from freepascal.org (Download, then Windows) and run it; it adds fpc to your PATH, so the commands below work in a new Command Prompt or PowerShell window, with hello.exe instead of ./hello. On macOS, install Apple's command line tools first with xcode-select --install, because fpc uses Apple's linker, then run the macOS package from the same download page. WSL on Windows also works: install Ubuntu and follow the Ubuntu commands.

If you plan to build desktop apps later, Lazarus (lazarus-ide.org) bundles Free Pascal with a form designer. You do not need it for this tutorial; a text editor and a terminal are enough.

Check it worked

fpc -iV prints 3.2.2 and nothing else. If you see fpc: command not found, the install did not finish; run the apt install line again and read its last lines for an error.

Step 2 · Write hello.pas

Make the demo folder and create the source file:

Bash
mkdir -p ~/hello-pascal-demo
cd ~/hello-pascal-demo
cat > hello.pas <<'PAS'
program Hello;

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

You can type the same five lines into a text editor and save them as hello.pas instead. Here is what each part does:

  • program Hello; names the program. Free Pascal treats this line as optional, but it is conventional and tells the reader what the file is. The semicolon separates it from what follows.
  • begin and end mark the main block: the statements that run when the program starts. Pascal uses words where C-family languages use { and }.
  • WriteLn('Hello, World!'); writes the text and then a line break. WriteLn comes from the System unit, which every program gets automatically, so there is nothing to import. Strings go in single quotes. Pascal ignores case in names, so writeln and WRITELN mean the same thing; WriteLn is the usual style.
  • end. ends the program. The full stop is not decoration: it tells the compiler this is the end of the whole program, and the compiler ignores anything after it. Every other end in a Pascal program is followed by a semicolon or nothing; only the last one takes a full stop.

In Pascal the semicolon separates statements rather than ending them, so the one after WriteLn(...) is optional when the next word is end. Most people write it anyway so they can add lines without thinking.

Check it worked

cat hello.pas prints the five lines exactly as above, ending with end. on its own line.

Step 3 · Compile and run it

Compile the file, then run the program it produces:

Bash
cd ~/hello-pascal-demo
fpc hello.pas
./hello
  • fpc hello.pas compiles and links in one command. The executable takes its name from the source file, so you get hello (or hello.exe on Windows) without passing an output name.
  • ./hello runs the program from the current folder. The ./ is needed because the shell does not look in the current folder for commands.

fpc prints a short report as it works: something like Free Pascal Compiler version 3.2.2+dfsg-49 [...] for x86_64, then Compiling hello.pas, Linking hello and a line counting the lines compiled. With the newer linker in Ubuntu 26.04 you may also see warning: link.res contains output sections; did you forget -T?. That warning is harmless; the program is built correctly.

Now look at what the build left behind, and at how the program is linked:

Bash
cd ~/hello-pascal-demo
ls
Bash
ldd ./hello

ls shows hello, hello.o and hello.pas. hello.o is the object file, the machine code for your program before linking. ldd reports not a dynamic executable: the program carries everything it needs and loads no shared libraries, so you can copy that single file to another 64-bit Linux machine and run it there.

Check it worked

./hello prints Hello, World!. Edit the text between the quotes, run fpc hello.pas again, then ./hello, and you see your new text. If you run ./hello without recompiling, you still get the old text: a compiled language only changes when you compile.

Step 4 · Read a name and greet it from a procedure

Now a program that asks for a name. It shows three things that make Pascal Pascal: a var block that declares a variable before any code runs, := for assignment, and a procedure of your own, declared before the main block that calls it.

Bash
cd ~/hello-pascal-demo
cat > greet.pas <<'PAS'
program Greet;

var
  Name: string;

procedure SayHello(const Who: string);
begin
  WriteLn('Hello, ', Who, '!');
end;

begin
  Write('What is your name? ');
  ReadLn(Name);
  if Name = '' then
    Name := 'stranger';
  SayHello(Name);
end.
PAS
fpc greet.pas

Line by line:

  • var opens the declarations section. Name: string; says there is a variable called Name and it holds text. You cannot use a variable you have not declared, and you cannot declare one in the middle of the main block.
  • procedure SayHello(const Who: string); declares a procedure: a named block of statements you can call. Who is its parameter, also typed as string. const promises the procedure will not change it, which lets the compiler pass it without making a copy.
  • The procedure has its own begin ... end;. This end takes a semicolon, not a full stop, because the program is not over.
  • Write is like WriteLn without the line break, so the cursor waits on the same line as the question.
  • ReadLn(Name); reads one line from the keyboard into Name, without the newline you typed.
  • if Name = '' then compares Name with the empty string. Here = asks a question; it does not change anything.
  • Name := 'stranger'; assigns a value. := is the only way to put a value into a variable.
  • SayHello(Name); calls your procedure. WriteLn accepts several values separated by commas and writes them one after another.

Run it and type your name when asked:

Bash
./greet

Then give it a name from a pipe, a name with accents and tone marks, and no input at all:

Bash
cd ~/hello-pascal-demo
echo "Ada" | ./greet
printf 'Adébáyọ̀\n' | ./greet
./greet < /dev/null

In Free Pascal's default mode, string means a short string of at most 255 bytes. Bytes, not letters: names in Yoruba, Igbo, Amharic or Arabic take more bytes than they have letters in UTF-8, the encoding Linux terminals use. A normal name fits easily. For longer text, add {$H+} on the line after program Greet; and string becomes a long string with no practical limit.

Check it worked

The piped runs print What is your name? Hello, Ada! and What is your name? Hello, Adébáyọ̀!, with the accents intact. The run with no input prints What is your name? Hello, stranger!, because ReadLn left Name empty and the if filled it in.

Step 5 · Try Delphi mode

Free Pascal can compile code written for other Pascal dialects. It calls them modes. The two you will meet most are objfpc, which Lazarus uses for new projects, and delphi, for code written for Delphi. Switch mode with a directive at the top of the file. Directives are comments that start with $:

Bash
cd ~/hello-pascal-demo
cat > modes.pas <<'PAS'
program Modes;

{$mode delphi}

begin
  WriteLn('Compiled in Delphi mode.');
  WriteLn('The largest Integer is ', High(Integer));
end.
PAS
fpc modes.pas
./modes

High(Integer) gives the largest value an Integer can hold. In Delphi mode Integer is 32 bits, so it prints 2147483647. In the default mode Integer is 16 bits, and the same line prints 32767. Delphi mode also turns string into a long string, like {$H+}. The same choice is available on the command line as fpc -Mdelphi modes.pas.

Check it worked

The program prints Compiled in Delphi mode. and The largest Integer is 2147483647. Delete the {$mode delphi} line, compile and run again, and the number becomes 32767.

Step 6 · Recognise the errors beginners hit

Make each mistake on purpose so you know the message when it happens by accident. fpc reports the file name, then the line and column in brackets.

A missing final full stop. Copy hello.pas without the full stop and compile it:

Bash
cd ~/hello-pascal-demo
sed 's/^end\.$/end/' hello.pas > nostop.pas
fpc nostop.pas

You get something like Fatal: Syntax error, "." expected but "end of file" found. The compiler reached the end of the file still waiting for the program to finish.

Double quotes around text. Pascal strings use single quotes. Write the string with double quotes:

Bash
cd ~/hello-pascal-demo
sed "s/'/\"/g" hello.pas > quotes.pas
fpc quotes.pas

fpc stops with an illegal character error pointing at the first ". To put a single quote inside a string, double it: 'It''s working' prints It's working.

Text in a number. Strong typing means a variable only accepts values of its type:

Bash
cd ~/hello-pascal-demo
cat > typed.pas <<'PAS'
program Typed;

var
  Age: Integer;

begin
  Age := 'twenty';
  WriteLn(Age);
end.
PAS
fpc typed.pas

You see something like Error: Incompatible types: got "Constant String" expected "SmallInt". SmallInt is the 16-bit integer that Integer stands for in the default mode. The program is never built, so the mistake cannot reach anyone who runs it.

Two more you will meet: writing = where you meant := (the compiler reads it as a comparison and complains), and putting a semicolon straight before else in an if statement, which ends the if early and gives a syntax error at else.

Check it worked

You have seen the "." expected, illegal character and Incompatible types messages in your own terminal, and none of the three commands produced a program: ls nostop quotes typed reports that all three files do not exist.

Step 7 · Clean up

Delete the demo folder and everything in it:

Bash
cd ~
rm -rf ~/hello-pascal-demo

Leave Free Pascal installed if you plan to carry on. To remove it, run sudo apt remove fp-compiler.

Check it worked

ls ~/hello-pascal-demo reports No such file or directory.

Next steps

  • Meet another strongly typed language from the same tradition, built for safety-critical systems: Hello World in Ada.
  • Compare fpc with a compiler that does link against the C library, and see each build stage: Hello World in C.
  • Try the other language behind long-lived business software: Hello World in COBOL.

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.