Writing Pascal Programs on a Raspberry Pi Using FPC

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:

sudo apt-get -y install fpc

To test the compiler, we can use a “Hello World” type program. Create a file called “hello.pp” using nano:

nano hello.pp

Insert the following lines:

program hello;
 
begin
    writeln('Hello Make Tech Easier.');
end.

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.:

fpc hello.pp

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:

./hello

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:

nano primes.pp

And insert the following lines:

program primes;
 
{ Function to test if a number if prime }
function prime(n: integer): boolean;
var
  i: integer; max: real;
begin
  { 2 is a prime }
  if n = 2 then
    prime := true
  { All even numbers aren't primes }
  else if (n <= 1) or (n mod 2 = 0) then
    prime := false
  else begin
    { Test by division }
    prime := true; i := 3; max := sqrt(n);
    while i <= max do begin
      if n mod i = 0 then begin
        prime := false; exit
      end;
      i := i + 2
    end
  end
end;
 
{ Test and display primes 0 .. 100 }
var
  n: integer;
begin
  for n := 0 to 100 do
    if (prime(n)) then
      write(n, ' ');
  writeln();
end.

Exit and save nano. Compile the program:

fpc primes.pp

And run:

./primes

The out will look something like this:

fpc-primes

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:

fp hello.pp

fpc-fp

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:

  1. 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.
  2. 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.