The Pascal programming language has been around since the 1970s and although it isn’t as popular as C, or C’s cousins C++ and C#, it certainly has a longevity. It is still taught in many academic institutions, as it encourages structured programming. Plus it has often had the occasional renaissance, for example when Borland Delphi was at its peak of popularity.
Pascal is available for the Raspberry Pi via the Free Pascal Compiler (FPC). The FPC is a Pascal compiler that not only supports Raspbian on ARM, but it also supports a wide range of 32-bit and 64-bit systems including Intel/AMD system running Linux, FreeBSD, OS X and Windows.
To install it on your Pi, type the following command:
To test the compiler, we can use a “Hello World” type program. Create a file called “hello.pp” using nano:
Insert the following lines:
In Pascal, like most programming languages, it is important to include all correct symbols, specifically the semi-colons at the end of the lines and the dot after the word end. Exit and save nano with the shortcut key “Ctrl + x”.
To compile the program, call the compiler “fpc” with the name of the Pascal file as the first parameter, i.e.:
This will compile the program and generate a binary file called “hello.” If you see a warning about the linker, “link.res contains output sections; did you forget -T?” then don’t worry. This is a benign warning that appears due to a bug in the linker (ld). You can see more about the warning in the FPC FAQ.
To run the program type:
And you should see the message “Hello Make Tech Easier.”
Here is a more complex program which calculates prime numbers by using trial by division. This is a very inefficient way to calculate primes, however it is sufficient to demonstrate a more complex Pascal program.
Create a file called “primes.pp” using nano:
And insert the following lines:
Exit and save nano. Compile the program:
The out will look something like this:
Free Pascal also includes a text-based IDE. It is reminiscent of the other text-based Pascal IDEs like Turbo Pascal from the days of MS-DOS. To start it, just type
fp; if you want to load a program into the IDE at start up, include it as the first parameter. For example, to run the IDE and load "hello.pp", type:
To access the menus, press ALT followed by the first letter (marked in red) of the menu name. So "Alt + F" opens the File menu, "Alt + R" opens the Run menu and so on. There are also some "F" key shortcuts. F3 to open a file, F2 to save a file, F9 to build the project and "Ctrl + F9" to run it.
The IDE is great in that you get an editor with syntax highlighting and a quick way to compile your source code. Two disadvantages that I have discovered are:
- The IDE doesn't include debugger support. Although there is a debugger menu, any attempt to use it gives the error "No debugger support available."
- When you run your program from within the IDE, the output is written over any existing output on your terminal. This means it is very hard to read the output.
Using Free Pascal on the Raspberry is a great way to learn the Pascal programming language. It is also a good stepping stone to Lazarus, a Delphi-like IDE and visual programming environment developed by the same project team.
If you have any questions about the examples above, please use the comments section below, and we will see if we can help. Free Pascal also has a vibrant user community and a set of forums where you can get help from other Free Pascal users.