Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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

  1. Download the Free Pascal installer from the official website.
  2. Run the installer and follow the on-screen instructions.
  3. When the installer finishes, open the command prompt and type fpc to check that the installation worked.

macOS

  1. Download the Free Pascal installer from the official website.
  2. Open the downloaded .dmg file and drag the Free Pascal application to your Applications folder.
  3. Open Terminal and type fpc to verify the installation.

Linux

  1. Open your terminal.
  2. Install Free Pascal with your package manager. On Debian-based systems, for example:
    sudo apt-get install fpc
    
  3. Type fpc in 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.
  • begin and end. 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

  1. Open your command line interface (CLI).
  2. Go to the directory where you saved hello.pas.
  3. Compile the program with the Free Pascal Compiler:
    fpc hello.pas
    
  4. If there are no errors, the compiler creates an executable file named hello.
  5. 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.