Chapter 1: Introduction to Pascal
History and background
Pascal is a procedural programming language that Niklaus Wirth designed in the late 1960s and named after the French mathematician and philosopher Blaise Pascal. His aim was to encourage good programming practice through structured programming and data structuring.
Pascal started out as a teaching language, but its clarity and ease of use soon made it popular in industry too. Many later languages show its influence, including Ada, Modula-2, and early versions of Delphi, itself an influential language.
Getting started with Pascal
To write Pascal programs, you need a Pascal compiler. These are some popular choices:
- Free Pascal (FPC): an open-source compiler that supports several Pascal dialects and runs on many platforms.
- Turbo Pascal: an older compiler from Borland, important in Pascal’s history.
- Lazarus: an open-source integrated development environment (IDE) for Free Pascal.
Installing Free Pascal
The steps depend on your operating system.
Windows
- Download the Free Pascal installer from the official website.
- Run the installer and follow the on-screen instructions.
- When the installer finishes, open the command prompt and type
fpcto check that the installation worked.
macOS
- Download the Free Pascal installer from the official website.
- Open the downloaded
.dmgfile and drag the Free Pascal application to your Applications folder. - Open Terminal and type
fpcto verify the installation.
Linux
- Open your terminal.
- Install Free Pascal with your package manager. On Debian-based systems, for example:
sudo apt-get install fpc - Type
fpcin the terminal to check that it installed correctly.
Writing your first program
Your first program prints “Hello, World!” to the console. Create a file called hello.pas and add this code:
program HelloWorld;
begin
writeln('Hello, World!');
end.
Explanation
program HelloWorld;declares the name of the program.beginandend.mark the beginning and end of the program’s main block.writeln('Hello, World!');calls a built-in procedure that writes the given string to the console.
Compiling and running the program
Using the command line
- Open your command line interface (CLI).
- Go to the directory where you saved
hello.pas. - Compile the program with the Free Pascal Compiler:
fpc hello.pas - If there are no errors, the compiler creates an executable file named
hello. - Run the executable:
./hello # On Windows, use hello.exe
You should see Hello, World! printed on the console.
You’ve now written and run your first Pascal program. The next chapters cover Pascal’s syntax, data types, and control structures in more detail.