Thursday, January 5, 2012

A. Program Structure

Here is a sample source code of a Pascal program:  

sample program; 

uses  crt; 

var
  
a: string;


begin
  
clrscr;
  
write ('Enter your name:');
  
readln (a);
  
writeln ('Hello', a);
  
readln;
 
end. 

program 
Contains the name / title of the program, in this case the 'example'.

uses 
Contains a list of units to be used in the program, namely 'crt'. Unit is a separate program module and used to store related processes/function. As in the example, 'uses crt' means the program uses a unit called 'crt', which contains a collection of routines (procedures and functions) to control PC. If the program uses more than one unit, then each unit separated by commas (example: uses crt, dos ;). 

var (declaration)
Contains a list of variables to be used in the program (in this example is 'a') and its data type (string), or can be called with the variable declaration. In addition to the variables, other declarations needed a program include: 
  • Declaration of data types 
  • Declaration of constants 
  • Variable declaration 
  • Declaration procedure
  • Function declaration 
  • Label declaration 

Examples of constant and variable declarations: 

const 
n = 100; 
var 
a: integer; 

begin-end
Contains statements that will run the program. Examples of statements are clrscr, writeln, readln, etc.. These statements should be in the begin-end block.

No comments:

Post a Comment