The Structure of Turbo Pascal Programs. ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß Turbo Pascal programs have an ordered structure, starting with the program name, followed by a declaration part and then by the statement part. The declaration part includes any procedures and functions, which must usually be declared before they are called. The complete structure of a program is illustrated below and is available as STRUCT.PAS and as the executable file STRUCT.EXE. Inspection of the other example programs of this tutorial will demonstrate actual applications. For clarity, all reserved words are shown in capitals in the full structure layout below, although this is not so in many of the other programs: PROGRAM DataCreation; { Choose any suitable name. } {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} { Appropriate comment and explanation, contained within braces. } { At any point in the program comments can be inserted to help } { the user understand the program, provided they are in braces. } {_______________________________________________________________} USES Crt; { A list of all units employed by the } { program, in this case just the Crt unit. } LABEL Continue, Finish; { Only used occasionally, as GOTO statements } { are not needed in a structured language. } { Not used in this demonstration program. } CONST { Global constants. } Max = 10; { A numeric constant. } Str = 'Press any key to continue: '; { A string constant. } TYPE { Global types. } LinePointer = ^LineOfNumbers; { A pointer type, not } { used in this program} LineOfNumbers = String[79]; { A string of fixed length.} VAR { Global variables. } Line : LineOfNumbers; { Specific instances of} Ptr : Array[1..Max] of LinePointer; { user-defined types, } { but Ptr not actually } { used in this program.} j : Integer; { Instance of Integer } { type. } PROCEDURE CreateData; { Choose a meaningful name. } { Local Labels, Constants and Types may be declared here. } VAR { Local variables, as necessary. } i : Integer; { Used as counter in a FOR loop. } n : Array[0..79] of char; { An array of 80 character types.} BEGIN Line := ''; { Initialize Line to be empty. } FOR i := 0 TO 79 DO { A FOR loop repeated 80 times. } BEGIN n[i] := Chr(Random(9) + 48); { Use of Chr & Random functions, } { to generate ASCII characters } { from 48 (=0) to 57 (=9). } Line := Line + n[i]; { Add all the characters to form } { line of 80 characters. } END; END; FUNCTION ReturnZero : Integer; { A trivial example of a function.} BEGIN ReturnZero := 0; END; { Main part of program. } BEGIN ClrScr; { Procedure from Crt unit to clear the screen. } FOR j := 0 TO Max DO BEGIN CreateData; { Call to procedure declared above.} Writeln(Line); { Call to system procedure Writeln.} END; Writeln; Writeln('Function return value = ',ReturnZero); Writeln; Write(Str); { Call to procedure Write for constant Str } REPEAT UNTIL KeyPressed; { Function KeyPressed returns True if a key } { has been pressed, ensuring that nothing } { happens until a key is pressed. } END. The above program is functional, but does contain a few redundant declarations and statements for illustrative purposes only. Most of program has been extracted from a larger program, in which for example the pointer type and variable are used. In the above example, some relevant structures have been used. The syntax for all the common structures used in Turbo Pascal are now listed: FOR i := 1 TO n DO BEGIN { If there is only one statement following } statement 1; { DO then the reserved words BEGIN and END } statement 2; { need not be used. } ... statement r; END; WHILE i < n DO { The WHILE loop has the condition checked } BEGIN { before the statements are executed and } statement 1; { may result in no action at all. } ... statement s; END; x := 1; y := 2; REPEAT { The REPEAT UNTIL loop has the condition } GoTOXY(x,y); { checked after execution of the statements} Write(x); { which must be executed at least once. } inc(x); { In this case the word REPEAT obviates the} UNTIL x > 8; { need for BEGIN, and UNTIL replaces END. } PROCEDURE YourChoiceOfName; { Choose any appropriate name. No spaces } { are allowed, but underscores can be used. } { Any parameters are passed within } { parentheses. See notes on Procedures. } BEGIN statement 1; statement 2; ... statement t; END; FUNCTION AnotherName : Return type; { Choose a suitable name and after } { a colon indicate the return type } BEGIN statement 1; ... statement m; AnotherName := some value or expression; { of the correct type. } END; WITH Something DO { Something is an instance of a } { RECORD or an OBJECT type. } BEGIN statement 1; ... statement n; END; CASE AVariable OF { AVariable is any appropriate variable.} 1: BEGIN statement 1; ... statement j; END; 2: BEGIN statement k; ... statement v; END; END; STRUCT.TXT 15.1.93