Register for EarthWeb's Million Dollar Sweepstakes!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 7
Program Architecture

7-1 Programs and Subprograms

When you deal with real applications the problems get more complex than those you have met so far, so you usually have to break the main problem down into simpler subproblems and program each individually in a subprogram. The subprograms are then combined together to build up the complete program. If you can break your application down into the smallest possible modules, you will find that many of them are common problems such as sorting names or numbers. This means that you can write some generic subprograms and use them later in different applications. Another advantage of using subprograms is that you can thus avoid the repetition of several statements to print a header or display a menu; you can program such tasks as subprograms and call them whenever needed. In Pascal you can divide your program into smaller subprograms called procedure s and functions. Actually, the Pascal language itself is made up from predefined procedures and functions. When the compiler encounters a WRITELN statement in a program, for example, the predefined procedure WRITELN is invoked to perform the required task.

7-2 Procedures

If divided into procedures, the main body of the “Scores” program from Chapter 5 might look something like this:

    BEGIN
     ReadScores;
     GetAverage;
     DisplayResults
    END.

The main program contains only three calls, each of them the name of a procedure which performs a specific task. The procedure “ReadScores” reads the array of the scores, “GetAverage” calculates the average score, and “DisplayResults” displays the results. As you can see, a user-defined procedure is called by its name just like any standard procedure.

Procedure Definition

Before calling a procedure it must be defined in the subprogram section, which is the last section of the declaration part. The following is a complete list of all the sections of the declaration part:

LABEL section
CONST section
TYPE section
VAR section
PROCEDUREs and FUNCTIONs section

A procedure definition is very similar to a program definition in that it consists of a header, a declaration part, and statements. Let us begin with a simple procedure to draw a line 20 characters long.

{ ----------------------- figure 7-1 --------------------------- }
PROGRAM Procedures1(OUTPUT);
{ ------------ Beginning of Procedure ------------ }
PROCEDURE DrawLine;
CONST
 Dash = '-';
 LineLength = 20;
VAR
 Counter:INTEGER;
BEGIN
 FOR Counter:= 1 TO LineLength DO
  WRITE(Dash);
 WRITELN
END;
{ -------------- End of Procedure --------------- }
{ ---------------- Main program ----------------- }
BEGIN
 WRITELN;
 DrawLine;
 WRITELN('** THIS IS A TEST **');
 Drawline
END.

The output is:

-----------------
** THIS IS A TEST **
-----------------

There are no variables or constants in the main program here, so the declaration part contains only the procedure definition. The definition starts with the procedure header:

    PROCEDURE Drawline;

The header includes the name of the procedure (DrawLine), which must be a valid identifier. Then comes the declaration part:

    CONST
     Dash = '-';
     LineLength = 20;
    VAR
     Counter:INTEGER;

The declaration part of the procedure includes the same sections as that of the main program. In our example, two named constants and a variable were declared. Then come the statements of the procedure which represent the task to be done (drawing a line), enclosed in a block.

    BEGIN
     FOR Counter:= 1 TO LineLength DO
          WRITE(Dash);
     WRITELN
    END;

Notice that the END statement in a subprogram is terminated by a semicolon rather than a period.

In the main program, the procedure is called twice to draw a line both before and after the displayed text.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.