pjhayward's Pascal Tutorial


pjhayward.net Home

Got a question?

Tutorial Home
Preparation
Lesson One - Sample Program
Lesson Two - Program Structure
Lesson Three - Data Types
and Constants
Lesson Four - Variables
Lesson Five - Text I/O
Lesson Six - Subroutines
Lesson Seven - Conditional
Statements
Lesson Eight - Arrays
Lesson Nine - Loops
Lesson Ten - Units
Lesson One - "Hello, World!": A Sample Program

Now that you have installed Free Pascal, open up the IDE (Integrated Development Environment). You should have an icon either on your desktop, or in the start menu. If you can't seem to find the shortcut, find where you installed Free Pascal, then open the bin/win32/fp.exe file. (Linux users: run 'fp' in a terminal window)

Now that you have the IDE open, we're going to put in a sample program and compile it. The IDE runs in a command prompt, so your mouse won't work. Get used to not using your mouse much when you're coding. Once you move on to Delphi or some other visual development interface, you'll use it often enough. But for now, stick to your keyboard. Menus are opened by pressing the Alt key, along with the highlighted letter in the menu name. So open File, then select New. You can either use your arrow keys, or just press "n".

Now, enter the following source code into the IDE: (Everything BETWEEN the start code and end code notes - don't include the lines with the dashes)
--- Start Code ---

program hellowld;

begin
  writeln('Hello, World!');
  readln;
end.
--- End Code ---


Now that you have your program typed in, you should save it. Open the File menu again (Alt-F), then select Save. At the bottom of the save window, you will see the directory (the original name for "Folder" that you are currently working in. If you ran the IDE straight from the install, you will notice that you are in the bin\i386-win32 folder of the Free Pascal installation path. That's generally a bad place to store your source code files. You can use the .. directory (use the Tab key to move between fields) to move up one directory until you get to the base installation folder of Free Pascal. (C:\FPC for the default installation.) This still may not be the best place to put your code, but it's easy to remember. If you know where you want to put your source code, go ahead and store it wherever you want. Once you are in the directory you want, type the name "hellowld.pas" into the Name box (Alt-N, or use tab) and save the file.

Once you have saved your source file, open the Run menu (Alt-R), then select Run. At this point, the IDE takes a couple of steps. First, it checks to see if the file has been changed since it was last compiled. If it has, it runs the compiler on the source code file, which translates it into an executable file, or program. This is stored in the same directory as the source code file, unless you have the compiler configured to store it somewhere else. It then runs the compiled program. If no changes were made, it just runs the existing compiled program.

At this point, you should see a black console window, with the text "Hello, World!" at the top left. If by chance you get an error, make sure you typed the program exactly as it appears on the web page - make sure you have semicolons at the end of each line (except the "begin" line), and make sure you used single quotes (also known as an apostrophe) around the text "Hello, World!". Pascal is very strict on program structure. This is generally a good thing, because it helps protect you from common mistakes that can result in fatal program crashes. Of course, you can still find plenty of ways to crash your programs, but the strict syntax is helpful.

Once you are done admiring your handiwork, press the Enter key to return to the IDE. At this point, you should see the code for the program in the IDE window. I'll go through line by line and explain what each does.

program hellowld;
This line tells the compiler that the file defines a new program - in this case, one called hellowld; The name is important - some compilers require that the name of a program matches the filename - that's why we named the file hellowld.pas. It's important that you place the semicolon at the end - if you don't you'll get a syntax error. The reason the semicolon is required is that you don't actually HAVE to put everything on it's own line. You could write one long line of code, and have that be the entire program. The semicolons define statements within the program. New line characters, spaces, and tabs are all called whitespace - basically, just junk that the compiler completely ignores. So make sure you have a semicolon, or you'll get an error.

begin
The begin statement tells the compiler that we are beginning a new block of code. All blocks of code, including programs, subroutines (called procedures and functions in Pascal), and loops need a begin statement. You can also use a begin statement to start a new block of code anywhere inside an existing block. This can be very useful. For example, the if-then statement normally expects to be followed by a single statement, which it runs if the condition specified is true. But if you want to run more than one statement, you can use the begin statement to start a new block of code. The entire block is run by the if-then statement, as if it were a single statement. Something to note: The begin statement does NOT need a semicolon. That is because the entire block is considered one statement - so the statement doesn't actually finish until the corresponding end statement (see below).

writeln('Hello, World!');
This line is pretty self-explanatory. The writeln() statement writes the specified text to the console, followed by a new line character. If you don't want the new line character, you can use write() instead. The write() and writeln() procedures are actually very flexible. We'll cover them in more detail in a later lesson.

readln;
This line is also very simple. It just waits for the user to press the Enter key. The read and readln procedure are a lot like the writeln procedures, but in reverse. We'll cover them in the same lesson as write() and writeln().

end.
You may or may not have noticed that we have one exception here to the semicolon rule. The end of the main program block is denoted by an end statement with a PERIOD following it, instead of a semicolon. This is the only place you will put a period after the end statement. All other code blocks should have a semicolon.

Next