This article is part of the Getting Started series |
|
Here is a basic program that does absolutely nothing:
program first;
begin
(*comment*)
end.
program first;
is the "program header". Program headers are for dealing with multiple programs and "units". For most dialects this is optional.
begin
is the compiler's indication that the preparation is done and that the rest is the actual program.
(*
and *)
tells the compiler to ignore what's in between them. This is a "comment". In the original Pascal, these can't be embedded like this: (*(*example*)*)
. However, almost all other dialects use braces ( {
and }
) and use precedence so this works: {(*example*)}
. Other dialects (Free pascal's default mode) can be embedded.
end.
(notice the period) tells the compiler to stop compiling. In fact, anything after that is completely ignored.
As always, we shall begin from the simplest program that any programmer will ever write—the Hello World program.
program hello;
uses crt;
begin
clrscr;
writeln('Hello World!');
end.
Let us break down the program into its constituent parts.
On line one, we have the program header (program hello;
). In a normal, small program like the above, the program header just serves as a short name for the program. This line can be omitted if deemed unnecessary. However, this header line has its use, which we will see when we move on to units (libraries).
On the third line, we see the beginning of a block which is initiated with a syntax begin
, then we see in the fifth line the "body", where the instructions for the computer hardware is located in this case it tells the computer that it will use the monitor or the CRT (with the "uses crt;
")and display the string "Hello World".
The simple script is terminated by an end.
on the sixth line. This is the main program in Pascal. If you have used C before, you might be a little surprised—in Pascal, the main program is not declared as a function. Rather, it is just a special block marked out with a begin
and an end.
.
On the fifth line, there is a call to a library function, writeln
. This function is part of a library which is linked into your Pascal programs by default, thus, there is no need to "include" any libraries explicitly.
If you have experience with C, you might wonder why no libraries need to be included. The reason for this is simple—while Pascal allows prototyping like C, the standard libraries usually have no prototypes, but are still included by default just in C. Thus, you can use the functions in them without declaring them.
As you might have noticed, the function writeln
is written in small letters. However, in contrast to C, Pascal functions, variables and other names are not case-sensitive: Writeln
, WriteLn
, WrItElN
are all equivalent, though Writeln
may be preferred by some, as it may look more readable.
You might have also noticed the use of single quotes instead of double quotes. In Pascal, strings and characters are single-quoted all the time. Double quotes have no special meaning in Pascal.