Pascal Program Structure
Before you study the building blocks of the Pascal language, let us look at a bare minimum Pascal program structure so you have it as a reference.
A Pascal program basically consists of the following parts:
- Program name
- Uses command
- Type declarations
- Constant declarations
- Variables declarations
- Functions declarations
- Procedures declarations
- Main program block
- Statements and Expressions within each block
- Comments
Every pascal program generally has a heading statement, a declaration and an execution part strictly in that order. Following format shows the basic syntax for a Pascal program:
Program {name of the program}
uses {comma delimited names of libraries you use}
const {global constant declaration block}
var {global variable declaration block}
function {function declarations, if any}
{ local variables }
begin
...
end;
procedure { procedure declarations, if any}
{ local variables }
begin
...
end;
begin { main program block starts}
...
end. { the end of main program block }
Pascal Hello World Example
The following is a simple pascal program that would print the words: Hello, World!
program HelloWorld; uses Display; (* Here the main program block starts *) begin Writeln('Hello, World!'); Readkey; End.
Looking at various parts of the above program:
- The first line of the source code is labeled as a program and named HelloWorld.
- The next line of code uses Display is a preprocessor command, which tells Modern Pascal to include the built-in unit display before going to the actual compilation phase.
- The next lines enclosed within begin and end statements are the main program block. Every block in Pascal is enclosed within a begin statement and an end statement. However, the end statement indicating the end of the main program is followed by a full stop (.) instead of semicolor (;).
- The begin statement of the main program block is where the program execution begins.
- The lines within (*...*) will be ignored by the compiler and it has been put to add a comment in the program.
- The statement writeln('Hello, World!') uses the writeln function available in Pascal which causes the message to be displayed on the screen.
- The statement readkey allows the display to pause until the user presses a key. It is part of the display unit. A unit is like a library in Pascal.
- The last statement end. ends your program.
Executing the Pascal Program
- Open a text editor and add the above-mentioned source code.
- Save the file as hello.p
- Open a command prompt and go to the directory, where you saved the file.
- type mp2 hello.p at the command prompt and press enter to execute your code.