Pascal - Programs

From SwinBrain

A pascal program is made up of several sections. This document helps outline the language's syntax, describing each of the parts of the syntax with reference to the syntax diagrams.

Contents

Programs


ref143x.png

ref144x.png

ref145x.png

ref146x.png

This indicates that a program starts with a program header and is then followed by a block. The following code is an example of the program header. The program header starts with the program keyword and is followed by an identifier which names the program. In the example below this identifier is HelloWorld. Notice that the ; indicates the end of the program header.

program HelloWorld;

Following the program header you can optionally have a uses clause. This clause allows you to gain access to functions and procedures from the library. In the following code we make use of the CRT unit from the library. This unit contains functions and procedures for working with the console.

program HelloWorld;
    uses CRT;

Following the uses clause there is a block. Blocks have their own definition which is explained below.

Blocks


ref154x.png

ref155x.png

ref157x.png

ref159x.png

ref160x.png

ref162x.png

ref163x.png


Lets have a look at these sections in detail.

A block contains a declaration part followed by a statement part.

Declaration Part

The declaration part may contain nothing, or it may contain a number of the following:

  • Label declaration part (excluded from this page)
  • Constant declaration part
  • Resource string declaration part (excluded from this page)
  • Type declaration part
  • Variable declaration part
  • Thread variable declaration part (excluded from this page)
  • Procedure/Function declaration part

The following code illustrates the declaration of a constant PI, a procedure Main, and a variable x.

program HelloWorld; //the program header
    uses CRT; //the uses clause
 
const PI: Double = 3.14159265; //a constant declaration
 
procedure Main(); ... //a Procedure/Function declaration
 
var x: Integer; //a variable declaration 

For more details on declaring functions and procedures see Pascal - Functions and Procedures.

Statement Part

The statement part within the block is implemented as a Compound Statement. The compound statement is explained below.

Compound Statement

A compound statement is used to group together a number of statements. The compound statement within the program's block contains the instructions that are run when the program starts.


ref109x.png

The compound statement starts with the keyword begin which is then followed by a number of statements which are separated by semicolons (;). The end of the compound statment is indicated by the end keyword.

program HelloWorld; //the program header
 
const PI: Double = 3.14159265; //a constant declaration
 
procedure Main(); ... //a Procedure/Function declaration
 
var x: Integer; //a variable declaration
 
begin //the start of the compound statement
    x := 10;  //This section contains the statements which are run when the program is executed
    Main();   //These statements are separated by ;
end. //the end of the compound statement with a . to end the program 

[edit]Pascal: Programs | Functions and Procedures | Statements | Data Types | Identifiers | ...
Links: Topic Brains
Return to Swin Brain