• Keine Ergebnisse gefunden

ADVANCED PROGRAM STRUCTURE

A PROGRAMMER'S GUIDE TO TURBO PASCAL

8. PROGRAM STRUCTURE

8.4 ADVANCED PROGRAM STRUCTURE

Program structure in Standard Pascal follows a definite lormat.

Some sections can be omitted, but no rearranging is allowed.

However, rearranging is allowed by TURBO Pascal. Here is the complete format of a Standard Pascal program:

program Name(file parameters);

label

<

label declarations

>

ccmat

<

const declarations

>

type

<

type declarations >

var

<

variable declarations

>

<

subprograms

>

begin

<

main body of program

>

eD4.

TURBO Pascal is more flexible than Standard Pascal in its program structure. The label, const, type, and var sections can be placed in any order and can occur more than once. They also can be mixed with subprograms. This allows logical grouping of definitions, such as:

ccmst

A second advantage of this freedom is the ability to define typed constants, which will be discussed below.

As you've seen, a program starts with the program statement.

The program statement is followed by the declaration section.

Here you define any labels, constants, data types, and/or variables which will be used throughout the program. Each section begins with the appropriate reserved word (Iabel,and so forth), followed by one or more corresponding declarations.

Here's a brief description of each kind:

label one or more unsigned integers (0 .. 9999) or identifiers, separated by commas, with the whole list ended with a semicolon:

label

10,ProcExtt,4213,Error;

const an identifier and a constant val ue, separated by an equals sign (=) and ended with a semicolon;

also, a typed constant (see below):

const

type

var

an identifier and a type definition, separated by an equals sign (=) and ended with a semicolon:

type XRange

language = 1.XMax;

= (BASIC,FURTRAN,Ada,Pascal, FURTH,C,COBOL,ALGOL);

one or more identifiers separated by commas, followed by a colon and a type identifier or type definition, and ended with (you guessed it!) a semicolon:

var A,B,C Flag Choice X

: Integer;

: Boolean;

: language;

: XRange;

The const-type-var sequence allows definitions to cascade down, so that you can change an entire program by merely changing a few constants and recompiling. Combined with the ability to repeat that sequence, this gives you great power and flexibility in defining your data structures.

8.4. 1

Typed Constants

TURBO Pascal lets you declare what it calls typed constants.

These aren't really constants, though they are declared in the const section. Instead, they are pre-initialized variables. Suppose you had two flags, Flag1 and Flag2, that you were going to use throughout your program. Flag1 needed an initial value of True, while Flag2 needed one of False. You might do the following:

program Whatever;

var

Flag! ,Flag2 : Boolean;

begin

Flag! : = True;

Flag2 : = False;

end..

This is fine, but if you have many such variables, it can consume space and time to do all of the initialization. With TURBO Pascal, you could instead do this:

program Whatever;

const

Flag! : Boolean = True;

Flag2 : Boolean = False;

begin

end..

Flag1 and Flag2 can still be used as variables; that is, you can still assign values to them through the course of your program.

However, they will now start out with the desired values and will not have to be explicitly initialized.

Forthose of you who know something about Pascal, yes, you can have typed constants for data structures such as arrays, strings, sets, and records. All the details can be found in the TURBO Pascal Reference Manual, Chapter 13.

8.4.2 Subprograms

Between the definitions and the main body of the program you place any subprograms (procedures and functions) that you might define. I'll talk more about these in Chapter 11, but a brief explanation isn't out of place. A subprogram has exactly the same structure as a program, except that it starts with a

procedure or function statement and ends with a semicolon (instead of a period). A subprogram is executed just by men-tioning its name along with any parameters it might take. Pascal includes a set of predefined subprograms; that is, procedures and functions that already exist without you having to define them. You saw some in the example earlier in the chapter: Write, WriteLn, and ReadLn. Thetwo tables at the end ofthis chapter list the standard subprograms of TURBO Pascal. As a quick example, here's our sample program with the main body converted into a procedure named Ca/culateSum:

program Sample;

{

purpose to demonstrate a subprogram last update 28 August 1986

}

procedure CalculataSum;

{

purpose read in two values and print the sum last update 28 August 1986

var

A,B,O : Integer;

begin { main body of procedure CalculataSum } Write('Enter two numbers: ');

ReadLnCA); ReadLnCB);

O:=A+B;

WriteLn('The sum is ',0) end.; { of proc CalculataSum }

begin { main body of program Sample } CalculataSum

end. { of program Sample }

When the program runs, it only has one statement to execute:

CalculateSum. That tells it to execute the statements in the main body of the procedure CalculateSum. When it's all done, it goes back to the main body and, having no more statements, stops.

You can do an awful lot with subprograms, but I won't tell you what until Chapter 11.

8.4.3 Block Statements

Following all the subprograms is the main body of the program itself, consisti ng of the reserved words begin and end, with some number of statements betweenthem. This is just a special case of what is known as a block statement. A block statement has the sequence begin ... end, with zero or more statements (separated by semicolons!) in between. A block statement can be used anywhere a regular statement can be used, and several places where a regular statement can't. For example, the main body of a program is just a block statement followed by a period, while the main body of a subprogram is a block statement followed by a semicolon. The real advantage of block statements shows up in control structures (see Chapter 10). For example, the if ••• then statement is defined as

if <Boolean expression>

then <statement>

But you often wish to execute more than one statement when a given condition is true. Solution:use a block statement, like this:

if Max < Min then begbl { force niin <= max } temp :=max;

max min end.;

:= min;

:=temp

You also used a block statement back in the section. on comments. You were using a with statement, which al!o~s you to easily see the fields of a record data type. It has the format:

with <identifler(s» do <statement>

The statement here was the block statement:

begbl

Write('SYSTEM: ' ,Name);

Write(' Population: ' ,Pop);

if Indx = CurStat.Loc.System

then Write(' <current location> ');

WriteLn end.;

You can nest block statements; that is, have block statements within block statements. For example, you could have a block statement in the if ... then statement, yielding:

with Starmap'" [Indx] do begin Write(,SYSTEM: ' ,Name);

Write(' Population; ',Pop);

if Indx = CurStat.Loc.System then begin Write(' <current location> ');

LocationFound := True end;

WriteLn end;

Simple, huh? You'll use block statements a lot throughout the book, especially after Chapter 10.

That's all for advanced program structu res for now. Before continuing to the next chapter, here are the two tables I promised you back in the early parts of this chapter. First is a table of predefined procedures, followed by a table of predefined functions.

clear to end of current screen line clear entire screen

send terminal reset string send terminal init string delay M milliseconds delete section of string S delete current screen line recover memory used by P'"

delete file F