Why learn Pascal?
Pascal is one of the few languages designed first of all for teaching. Its creator wanted a language in which the structure of a program would be visible on the page: what the data is, what each part does, and in what order. That design made it the language in which a generation of programmers learnt their craft, and many habits now taken for granted, such as declaring variables with types, structuring code into procedures and loops rather than jumps, and reading code top to bottom, were spread by Pascal.
It would be a museum piece if that were all. But Pascal grew. Object Pascal, through Borland's Turbo Pascal and Delphi and the open-source Free Pascal and Lazarus, added units, classes, exceptions, interfaces and generics, and it remains one of the most productive ways to build native desktop applications: you draw a window in a visual designer, double-click a button, write the code for its click, and compile to a single fast executable for Windows, Linux or macOS. Business software, medical and industrial tools, file managers and database tools are built this way today.
Learning Pascal also teaches you something about language design. It is small, strict and readable, and comparing it with C, its contemporary, shows two very different answers to the question of how much a language should protect you from yourself.
From ETH Zurich to Lazarus
Niklaus Wirth, a professor at ETH Zurich, designed Pascal in 1968 and 1969 and published it in 1970, naming it after the seventeenth-century mathematician Blaise Pascal. He had two aims: a language suitable for teaching programming as a systematic discipline, and one that could be implemented reliably and efficiently on the computers of the day. Pascal took the block structure of ALGOL 60, added user-defined types, records, sets and enumerations, and left out anything that would make programs hard to read or compilers hard to write. Wirth later went on to design Modula-2 and Oberon, and received the Turing Award in 1984.
Pascal spread through universities in the 1970s, helped by a portable implementation: UCSD Pascal, from the University of California San Diego, compiled to an intermediate p-code that a small interpreter could run on many microcomputers, an idea that Java's bytecode would echo twenty years later. Pascal was standardised as ISO 7185 in 1983, and it was the language of the American Advanced Placement computer science exam until the late 1990s.
The decisive step for working programmers came in 1983, when Borland released Turbo Pascal, written by Anders Hejlsberg. It combined an editor, a very fast compiler and a low price, and it made Pascal one of the most popular languages on personal computers. Meanwhile Apple, consulting Wirth, developed Object Pascal for programming the Lisa and Macintosh, and Borland brought objects into Turbo Pascal 5.5 in 1989. In 1995 Borland released Delphi: Object Pascal with classes, exceptions and properties, plus a visual designer and a component library for Windows. Hejlsberg later moved to Microsoft, where he led the design of C# and TypeScript, and Delphi's influence on C# is easy to see.
Delphi passed from Borland to CodeGear and, in 2008, to Embarcadero, which still develops it: its 13.x releases target Windows, macOS, Linux, iOS and Android. In parallel, Free Pascal began in 1993 as a student project of Florian Klämpfl and grew into a mature open-source compiler for dozens of platforms, compatible with Turbo Pascal and largely with Delphi. Lazarus, its IDE and visual designer, offers Delphi-style application building on Windows, Linux and macOS. As of late 2026 the stable compiler is Free Pascal 3.2.2 and the stable IDE is Lazarus 4.8, with Lazarus 5.0 in release candidates.
Applications written in Object Pascal that you may have met include the database tool HeidiSQL, the file managers Total Commander and Double Commander, and the comparison tool Beyond Compare.
Installing Free Pascal and running a first program
Installing
- Linux:
sudo apt install fpcon Debian and Ubuntu,sudo dnf install fpcon Fedora, or the packages from freepascal.org. - Windows and macOS: the installers from freepascal.org, or install Lazarus, which brings Free Pascal with it.
Check with fpc -iV, which prints the version (3.2.2 for this course). For an IDE, Lazarus is the natural choice; Visual Studio Code has community Pascal extensions too.
Hello, Pascal
Save this as hello.pas:
program Hello;
begin
WriteLn('Hello, Pascal!');
end.Compile and run it:
fpc hello.pas
./helloHello, Pascal!Line by line:
program Hello;is the program heading. The name is mostly documentation; the executable is named after the file.begin…end.is the main block, the statements that run. The finalendis followed by a full stop, which marks the end of the program; everything after it is ignored.WriteLnwrites its arguments followed by a line break;Writewrites without one. Strings are in single quotes; to put a quote inside a string, double it:'It''s'.- Semicolons separate statements rather than end them, which is why none is needed before
end(though one is allowed and most programmers write it).
Pascal is not case-sensitive: WriteLn, writeln and WRITELN are the same. The usual style is lower-case keywords and CamelCase names. Comments are written { like this }, (* like this *) or // to the end of the line.
fpc compiles the program straight to native machine code and links an executable, in well under a second for a small program: fast compilation has been a point of pride since Turbo Pascal.
Declarations before statements
A Pascal program declares everything before the main block: constants after const, variables after var, each with its type. This program prints a conversion table:
program Distances;
const
KmPerMile = 1.609344; { exact, by international agreement }
var
Miles: Integer;
Km: Real;
begin
WriteLn('Miles':6, 'Km':10);
for Miles := 1 to 5 do
begin
Km := Miles * KmPerMile;
WriteLn(Miles:6, Km:10:2);
end;
WriteLn('A marathon is ', 42.195 / KmPerMile:0:2, ' miles.');
end.It prints:
Miles Km
1 1.61
2 3.22
3 4.83
4 6.44
5 8.05
A marathon is 26.22 miles.KmPerMile = 1.609344;declares a constant (with=), whileKm: Real;declares a variable (with:). Assignment is:=, and=is only ever a comparison.for Miles := 1 to 5 dorepeats one statement. To repeat several, wrap them inbegin…end, a compound statement, as here.WriteLn(Miles:6, Km:10:2)uses Pascal's built-in formatting::6pads to a width of six characters, and for real numbers:10:2means width 10 with 2 digits after the point.:0:2means "as narrow as possible, two decimals". Without a format, a real prints in scientific notation./always gives a real result, even for two integers. Integer division is the operatordiv, withmodfor the remainder.
Compiler modes and the standard units
Free Pascal compiles several dialects of Pascal, selected by a compiler mode. The mode changes details such as the size of Integer, the meaning of string and which syntax is accepted:
| Mode | Directive | Meant for |
|---|---|---|
fpc (the default) | {$mode fpc} | Turbo Pascal–style code; Integer is 16 bits |
objfpc | {$mode objfpc}{$H+} | modern Free Pascal code with classes and exceptions; the Lazarus default |
delphi | {$mode delphi} | code shared with Embarcadero Delphi |
tp, macpas, iso | {$mode tp}, … | Turbo Pascal 7, Mac Pascal and ISO 7185 code |
The difference is not academic. Without a mode directive, this one-line program printed SizeOf(Integer) = 2 bytes: in the default mode Integer is 16 bits, as in Turbo Pascal on 1980s PCs, and holds only -32768 to 32767. In objfpc and delphi modes it is 32 bits. Modern code starts with a mode directive, and from now on so does every program in this course. {$H+} makes string mean a long, dynamically sized string rather than an old 255-character ShortString.
program Modes;
{$mode objfpc}{$H+}
uses
SysUtils;
var
Name: string;
Greeting: string;
begin
Name := 'Wanjiru';
Greeting := 'Habari, ' + Name + '!';
WriteLn(Greeting);
WriteLn('Length: ', Length(Greeting));
WriteLn('SizeOf(Integer) = ', SizeOf(Integer), ' bytes');
WriteLn(Format('%s has %d letters; pi is about %.3f', [Name, Length(Name), Pi]));
WriteLn(UpperCase(Name), ' ', IntToStr(2026 - 1970), ' years after Pascal');
end.It prints:
Habari, Wanjiru!
Length: 16
SizeOf(Integer) = 4 bytes
Wanjiru has 7 letters; pi is about 3.142
WANJIRU 56 years after Pascaluses SysUtils; brings in a unit, Pascal's word for a module. SysUtils is part of the run-time library and provides, among much else, Format, UpperCase and IntToStr. Format takes a format string with %s, %d and %.3f placeholders and an open array of values in square brackets; it is the professional way to build output, since it keeps the layout of a message in one readable string. + joins strings, and Pi is a built-in function.
The compiler as a checker
Pascal is strongly typed, and the compiler tells you when types do not fit. This program does not compile:
program Oops;
var
Count: Integer;
begin
Count := 7 / 2;
WriteLn(Count)
end.Free Pascal 3.2.2 reports:
main.pas(5,14) Error: Incompatible types: got "Double" expected "SmallInt"The message gives the line and column, what the compiler found and what it expected. / produced a real number, and a real cannot silently become an integer; you must decide whether you mean 7 div 2 (which is 3) or Round(7 / 2) or Trunc(7 / 2). And note SmallInt: without a mode directive, Integer is the 16-bit SmallInt.
How Pascal programmers work
Most professional Object Pascal is written in one of two IDEs. Lazarus (free, open source, Windows, Linux and macOS) and Delphi (commercial, from Embarcadero, with a free Community Edition under licence conditions) both combine an editor with code completion, a debugger, a visual form designer and a large component library. Command-line builds use fpc directly, lazbuild for Lazarus projects, or Delphi's MSBuild-based tools.
A few conventions are nearly universal, and following them will make your code look like everyone else's:
- Types start with
T(TCustomer), pointer types withP(PNode), interfaces withI(IComparer), exception classes withE(EParseError), and private fields withF(FName). Delphi's library set this style and Free Pascal's follows it. - Parameters are often prefixed with
A(AName) to distinguish them from fields, in code that follows the Delphi style. - Each file starts with
{$mode objfpc}{$H+}or{$mode delphi}, chosen once per project. - Range and overflow checking (
{$R+}and{$Q+}, or the-Crand-Cocompiler options) are switched on in debug builds; the next lesson shows why.
Compiler messages come with levels: errors stop the build, warnings point at code that is probably wrong (such as a variable used before it is given a value), and hints and notes at code that is merely suspicious. Treat warnings as bugs until you are sure they are not.
Common misunderstandings
Misconception
"Pascal is a dead teaching language." Standard Pascal is mostly of historical interest, but Object Pascal is actively developed: Free Pascal and Lazarus release regularly, Embarcadero ships new Delphi versions, and many companies maintain and extend large Delphi and Lazarus applications.
Misconception
"Free Pascal and Delphi are the same language." They share a core and Free Pascal's delphi mode accepts most Delphi code, but each has features the other lacks, and their libraries differ (Lazarus's LCL versus Delphi's VCL and FireMonkey). Code meant for both needs care and testing on both.
Misconception
"Every statement must end with a semicolon." Semicolons separate statements. One before end is harmless, but one before else ends the if statement early and is a syntax error, a classic beginner's trap covered in the next lesson.
Misconception
"Integer is 32 bits." It depends on the compiler mode: 16 bits in Free Pascal's default and Turbo Pascal modes, 32 bits in objfpc and delphi modes. When the size matters, use the explicitly sized types Int32, Int64, Byte and so on, or set the mode.
Exercises
All outputs below were checked by compiling and running the code with Free Pascal 3.2.2.
1. First steps in Pascal
Warm-upHumanWhat were Niklaus Wirth's main aims when he designed Pascal?
2. First steps in Pascal
Warm-upComputationalWhat does WriteLn(3.14159:0:2); print?
3. First steps in Pascal
CoreComputationalWhat does WriteLn('[', 42:5, ']'); print? Type the whole line, brackets included.
4. First steps in Pascal
Warm-upComputationalWhat does WriteLn(17 div 5, ' ', 17 mod 5); print?
5. First steps in Pascal
Warm-upComputationalWhat does WriteLn('It''s Pascal'); print?
6. First steps in Pascal
CoreComputationalA program has no {$mode} directive and is compiled with plain fpc. What does WriteLn(SizeOf(Integer)); print?
7. First steps in Pascal
CoreComputationalIn the distance table, the loop is changed to for Miles := 1 to 10 do. What kilometre value appears on the last row? Type just the number.
8. First steps in Pascal
CoreHumanWhy was Borland's Turbo Pascal (1983) so important for Pascal's history?
Further reading and resources
- Free Pascal downloadsInstallers and packages for Linux, Windows, macOS and other systems.
- Free Pascal User's GuideInstalling the compiler, its command-line options and its compiler modes.
- LazarusThe free IDE for Free Pascal; its installers include the compiler.
- Embarcadero DelphiThe commercial Object Pascal environment descended from Borland's Delphi.