Pascal Programming/Beginning

In this chapter you will learn:
 * How a Pascal source code file is structured
 * Basic terminology

Programs
All your programming tasks require one source code file that is called in Pascal a. A  source code file is translated by the compiler into an executable application which you can run. Let’s look at a minimal  source code file:
 * 1) The first line   indicates that this file is a Pascal source code file for a program.
 * 2) The   and   mark a frame. We will explain this in detail as we move on.
 * 3)    is a comment. Comments will be ignored by the compiler, thus do not contribute in any way how the executable program looks or behaves.
 * 4) And the final dot   after the final   informs the compiler about the program source code file’s end.

Compilation
In order to start your program you need to compile it.

First, copy the program shown above. We advise you to actually type out the examples and not to copy and paste code. Name the file. is the program’s name, and the filename extension  helps you to identify the source code file.

Once you are finished, tell the compiler you have chosen to compile the program: If you are using the FPC, type into a console followed by a (relative or absolute) file name path to the source code file:

If you are using the GPC, type into a console followed by a (relative or absolute) file name path to the source code file:

Finally, you can then execute the program by one of the methods your OS provides. For example on a console you simply type out the file name of the executable file: (where  refers to the current working directory in Unix-like environments) As this program does (intentionally) nothing, you will not notice any (notable) changes. After all, the program’s name  is short for no operation.

The computer speaks
Congratulations to your first Pascal program! To be fair, though, the program is not of much use, right? As a small step forward, let’s make the computer speak (metaphorically) and introduce itself to the world:

Program header
The first difference you will notice is in the first line. Not only the program name changed, but there is. This is a program parameter. In fact, it is a list. Here, it only contains one item, but the general form is  and so on. A program parameter designates an external entity the OS needs to supply the program with, so it can run as expected. We will go into detail later on, but for now we need to know there are two special program parameters: and. These parameters symbolize the default means of interacting with the OS. Usually, if you run a program on a console,  is the console’s display.

Writing to the console
The next difference is. This is a statement. The statement is a routine invocation. The routine is called. has (optional) parameters. The parameters are, again, a comma-separated list surrounded by parentheses.

Routines
Routines are reusable pieces of code that can be used over and over again. The routine, short for write line, writes all supplied parameters to the destination followed by a “newline character” (some magic that will move the cursor to the next line). Here, however, the destination is invisible. That is, because it is optional it can be left out. If it is left out, the destination becomes, so our console output. If we want to name the destination explicitly, we have to write. and  are identical. The missing optional parameter will be inserted automatically, but it relieves the programmer from typing it out.

In order to use a routine, we write its name, as a statement, followed by the list of parameters. We did that in line 2 above.

String literals
The parameter  is a so-called string literal. Literal means, your program will take this sequence of characters as it is, not interpret it in any way, and pass it to the routine. A  literal is delimited by typewriter (straight) apostrophes.

Reserved words
In contrast to that, the words,   and   (and many more you see in a bold face in the code examples) are so-called reserved words. They convey special meaning as regards to how to interpret and construct the executable program. You are only allowed to write them at particular places.

Behavior
Now, that we know what the source code contains, create a new file, copy the source code (by typing it manually), compile and run it: This type of program, by the way, is an example of a class of “Hello world” programs. They serve the purpose for demonstrating minimal requirements a source code file in any programming language needs to fulfill. For more examples see Hello world in the WikiBook “Computer Programming” (and appreciate Pascal’s simplicity compared to other programming languages).

Comments
We already saw the option to write comments. The purpose of comments is to serve the programmer as a reminder.

Comment syntax
Pascal defines curly braces as comment delimiting characters: (spaces are for visual guidance and have no significance). The left brace opens or starts a comment, and the right brace closes a comment.

However, when Pascal was developed not all computer systems had curly braces on their keyboards. Therefore the bigramms (a pair of letters) using parentheses and asterisks was made legal, too: .

Such comments are called block comments. They can span multiple lines. Delphi introduced yet another style of comment, line comments. They start with two slashes  and comprise everything until the end of the current line.

Delphi, the FPC as well as GPC support all three styles of comments.

Helpful comments
There is an “art” of writing good comments.

When writing a comment, stick to one natural language. In the chapters to come you will read many “good” comments (unless they clearly demonstrate something like below).

Terminology
Familiarize with the following terminology (that means the terms on the right printed as comments):

Note, how every constant and type definition, as well as every variable declaration all go into dedicated sections. The reserved words,  , and   serve as headings.

A sequence is also called a compound statement. The combination of definitions, declarations and a sequence is called a block. Definitions and declarations are optional, but a sequence is required. The sequence may be empty, as we already demonstrated above, but this is usually not the case.

Do not worry, the difference between definition and declaration will be explained later. For now you should know and recognize sections and blocks.