Home Up Contents Search

Program Structure

What is a Pascal program?

It’s difficult to gain an understanding of the whole by examining only the parts. The following information presents an overview of a Pascal program but omits specific details. It gives you a brief description of each of the elements of a program and then shows you how they all fit together. You can refer to the Pascal Language Guide to find more specific details of the language.

A Pascal program

In its simplest form, a Pascal program is made up of a program heading, which names the program, and the main program block, which accomplishes the purpose of the program. Within the main program block is a section of code that occurs between two key words: begin and end. Here is a very simple program that illustrates these concepts:

program Welcome;
begin

Writeln (¢ Welcome to Pascal¢ );

end.

The first line is the program heading, which names the program. The remainder of the program is the code that starts with begin and stops with end. Although this particular code section contains only one line, it could contain many. In any Pascal program, all the action occurs between begin and end.

Procedures and functions

The code between the last begin and end in a program drives the logic of the program. In a very simple program, this section of code might be all you need. In larger, more complex programs, putting all your code here can make your program harder to read and understand - and more difficult to develop.

Procedures and functions let you divide the logic of a program into smaller, more manageable chunks, and are similar to subroutines in some other languages. All the action in a procedure or function occurs in the code between its begin and end just like in the main program block. Each of these segments of code performs a small, discrete task.

Figure 1 - Procedure or function diagram

If you find your program does the same thing many times, you might want to put the logic into a procedure or function. You write the code in a procedure or function once and your program can use it as often as necessary.

Here is an example of a function. This GetNumber function gets a number from the user:

function GetNumber: Real;
var

Response: Real;

begin

Write(¢ Enter a number: ¢ );
Readln(Response);
GetNumber := Response;

end;

A procedure or function must appear before the main code section in the main program block. The main code section can then use the procedure or function.

 

Figure 2 - Simple Pascal program diagram

The following example is an outline of a program that uses the GetNumber function. The programmer has divided the logic of this program into three tasks:

    1. Get a number from the user.
    2. Perform the necessary calculations with the user-supplied number.
    3. Print a report.

The main logic of the program is found between the last begin and end.

program Report;
var

A: Real;

{more declarations}

...

function GetNumber: Real;
var

Response: Real;

begin

Write(¢ Enter a number: ¢ );
Readln(Response);
GetNumber := Response;

end;

procedure Calculate(X: Real);

...

procedure PrintReport;

...

begin

A := GetNumber;
Calculate(A);
PrintReport;

end.

The primary logic in this program is very simple to understand. All the details are hidden within the bodies of the procedures and functions. Using procedures and functions encourages you to think about your program in a logical, modular way.

 

Statements

The code section between begin and end contains statements that describe the actions the program can take and is called the statement part. These are examples of statements:

A := B + C; {Assign a value}
Calculate(Length, Height);
{Activate a procedure}
if X < 2 then Answer := X * Y; {Conditional statement}
begin {Compound statement}

X := 3;
Y := 4;
Z := 5;

end;
while not EOF(InFile) do {Repetitive statement}
begin

Readln(InFile, Line);
Process(Line);

end;

Simple statements can either assign a value, activate a procedure or function, or transfer the running of the program to another statement in the code. The first two examples shown in the examples are simple statements.

Structured statements can be compound statements that contain multiple statements, conditional and repetitive statements that control the flow of logic within a program, and with statements that simplify access to data in a record.

You might compare a Pascal statement to a sentence in a human language such as English, Danish, or Greek. Simple Pascal statements and simple human sentences hold one complete thought. Structured Pascal statements and complex sentences contain more complicated logic.

 
Expressions

Just as a sentence is made up of phrases, so is a Pascal statement made up of expressions. The phrases of a sentence are made up of words, and the expressions of a statement are composed of elements called factors and operators. Expressions usually compare things or perform arithmetic, logical, or Boolean operations.

Just as phrases in a human language can be made up of smaller phrases, so can expressions in Pascal be made up of simpler expressions. You can read about all the combinations of factors and operators that make up expressions in the Pascal Language Guide. They can be quite complex. For now, it might help to see some examples of expressions:

X + Y
Done <> Error
I <= Length
-X


Tokens

Tokens are the smallest meaningful elements in a Pascal program. They make up the factors and operators of expressions. Tokens are special symbols, reserved words, identifiers, labels, numbers, and string constants; they are akin to the words and punctuation of a written human language. These are examples of Pascal tokens:

 
function {reserved word}
( {special symbol}
:= {special symbol}
Calculate {identifier for a procedure}
9 {number}

Here is an illustration of a statement. You can see that statements are made up of expressions, which are made up of tokens.

 

Figure 3 - Statement diagram

 
Types, variables, constants, and typed constants

A variable can hold a value that can change. Every variable must have a type. A variable’s type specifies the set of values the variable can have.

For example, this next program declares that variables X and Y are of type Integer; therefore, the only values X and Y can contain are integers, which are whole numbers. Pascal displays an error message if your program tries to assign any other type of value to these variables.

program Example;
const

A = 12; {Constant A never changes in value}
B: Integer = 23; {Typed constant B gets an initial value}

var

X, Y: Integer; {Variables X and Y are type Integer}
J: Real;
{Variable J is type Real}

begin

X := 7; {Variable X is assigned a value}
Y := 8;
{Variable Y is assigned a value}
X := Y + Y;
{The value of variable X changes}
B := 57;
{Typed constant B gets a new value}
J := 0.075;
{Variable J gets a floating-point value}

end.

In this simple and not very useful program, X is assigned the value 7 originally; two statements later it is assigned a new value, Y + Y. As you can see, the value of a variable can vary.

A is a constant. The program gives it a value of 12 and this value can’t change - its value remains constant throughout the program.

B is a typed constant. It’s given a value when it’s declared, but it’s also given a type of Integer. You can think of a typed constant as a variable with an initial value. The program can later change the initial value of B to some other value.

The part of this program that declares the constants and variables is called the declaration part.

If you look back at the example code GetNumber, you’ll see that the function GetNumber has a declaration part that declares a variable. Procedures and functions can contain a declaration part just as a program or unit can.

 

Putting it all together

Now that you’ve been introduced to the primary components of a Pascal program, you need to see how they all fit together. Here’s a diagram of a Pascal program:

Figure 4 - An expanded Pascal program diagram


The program heading, the optional uses clause (we’ll talk about this in the next section), and the main program block make up a Pascal program. Within the main program block can exist the smaller blocks of procedures and functions. Although the diagram doesn’t show this, procedures and functions can be nested within other procedures and functions. In other words, blocks can contain other blocks.

Combined with other tokens and blank spaces, tokens make up expressions which make up statements.

In turn, statements combined with declaration parts make up blocks, either the main program block or a block in a procedure or function.


Units

A Pascal program can use blocks of code in separate modules called units. You can think of a unit as a mini-program your application can use. Like a program, it has a heading, called a unit heading, and a main block that contains a code section delineated by begin and end.

Any Pascal main program block can include a line that enables the program to use one or more units. For example, if you are writing a text-console program called Colours and you want to change the colour of the text as it appears on your screen, you can specify that your program use the Crt32 unit:

program Colours;
uses Crt32;
begin
...
end.

The uses Crt32 line tells Pascal to include the Crt32 unit in the executable program. The Crt32 unit contains all the necessary code to change the colour of the text in your program, among other things. Simply by including uses Crt32, your program can use the code that is in the Crt32 unit. If you put all the code required to create the functionality of the Crt32 unit within your program, it would be a lot more work, and it would sidetrack you from the main purpose of your program.

You can also write your own units. Use them to divide large programs into logically-related modules. Code you place in a unit can be used by any program. You only have to write the code once, then you can use it many times.



Reference:
Borland Pascal With Objects Language Guide, 1992. Borland. USA.
 
 
Next
Send mail to ljschwerin@hotmail.com with questions or comments about this web site.

Copyright © 1999-2002 Leon Schwerin
Last modified: 26 March 2000
1