Pascal Tutorial: Getting Started - 01

Pascal is often an overlooked or even disregarded language. I know I can't change that, but I can help you to learn and understand Pascal. Pascal is a great language to learn, and rather easy too. It reads nicely, and most of the semantics make perfect sense. I want to embark on a short journey to teach you Pascal, starting with Hello World, and ending with much more complicated application, yet to be determined. This is mainly due to the lack of my creativity.

First you need to download and install the FreePascal compiler, and Lazarus IDE. I recommend visiting the Lazarus website, if you are on window or mac, and downloading the latest installer. If you are on linux, using your package manager, install lazarus from there. (Please See, Pascal Tutorial: Installing FreePascal & Lazarus - 00)

Hello World.

When you first start lazarus, it will create a blank LCL Forms application. This is great for making visual applications, which require buttons, grids, etc. We are going to be creating a CLI application to start with. The first thing we need to do is close the Project by clicking Project > Close Project

You should then be presented the "Project Wizard" Dialog Window. Select New Project.

This will bring up the "Create a new project" dialog window. Here we need to select Program under the Project types list on the left of the window. This is a standard CLI application without any additional "helper" libraries, which will just serve to confuse a newcomer to Pascal.

Congratulations. We have a skeleton of an application. It will compile and run. However, it will do nothing. Let's change that by taking the existing code and adding our own instructions into the "Main" and get it to print "Hello World"

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

begin
end.

Adding our code.

Now that we have the skeleton of our application, modify the source; adding a new line between 13 and 14 (begin & end.) with the following code.

  WriteLn('Hello Worlds!');

Making it look like the following

program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

begin
  WriteLn('Hello Worlds!');
end.

Compile and run your program.

It is finally time to compile and run your new program. You can quickly do this by pressing F9. At which time you will be prompted to save the file on your computer. I strongly recommend, you create a new folder somewhere on your computer for each project/program you create. Even though this program is only 1 file, it will help keep you sane.

Alternatively you can press the Play button on the toolbar. (This will compile and execute your program)

Expected output.

Once you have saved your project, and ran it. you should expect to see something flash on the computer screen. Followed by a message box saying the "execution stopped". Don't worry, be happy, this is completely expected. to get your application to stay visible, we can simply add the following line to the program, just before the line (end.).

  ReadLn();

This will cause the program to wait until we press Return or Enter to exit.

Your output should look something like the following:

Pretty much it.

That is just about all there is to creating a Hello World program with FreePascal and Lazarus.


If you like this tutorial and want to see more. Let me know on social media, links below. Follow me on twitter, facebook, youtube, and google+