Pascal - Program Structure


Before we study basic building blocks of the Pascal programming language, let us look a bare minimum Pascal program structure so that we can take it as a reference in upcoming chapters.


1. Pascal Program Structure

A Pascal program basically consists of the following parts:


Every pascal program generally have 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 
procedure  // procedure declarations,if any
//local variables 
begin     // main program block starts
end.     // the end of main program block 


2. Pascal Hello World Example

Following is a simple pascal code that would print the words "Hello, World!":

program HelloWorld;
uses crt;  //Here the main program block starts 
   writeln('Hello, World!');

Let us look various parts of the above program:


3. Compile and Execute Pascal Program:


$ fpc hello.pas
Free Pascal Compiler version 2.7.1 [2013/12/23] for x86_64
Copyright (c) 1993-2013 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling hello.pas
Linking hello
8 lines compiled, 0.1 sec
$ ./hello
Hello, World!

Make sure that free pascal compiler fpc is in your path and that you are running it in the directory containing source file hello.pas.