Last Updated:

Program structure in Pascal

Program structure in Pascal

Any program in pascal has three sections:

  1. Heading
  2. Variable and function description block,
  3. The main body of the program.

Example:

Program name; The name of the program
// This is the section of descriptions of variable functions, modules, etc
. begin
// Function body - the main end statements are
written here.

Heading

The very first line of any program is the title. It begins with the word Program, after which you can write any arbitrary name of the program. For example: Program My first program. There are several conditions for the program title:

  1. The name should not begin with a number.
  2. Must not match the reserved word languages of the language.
  3. Must not be the same as the variable and function names in the descriptions section.
  4. The use of characters (", /, %, &... and others) other than underlining.
  5. You don't have to write a title.
  6. The title should reveal the meaning of the program (desirable, but not mandatory).

This is the area between the title and the word begin. Here, the programmer declares variables, structures, arrays, functions, modules, constants, and other language constructs.

Example:

Program name;
var // Declaring variables
s, p, f: integer;
t, r: string;
y, c: char;

const // Declaration of constants
O:=5;
I:=6;

procedure add() // Declaration of the
procedure begin
p:=O+I;
end;

function multy() // Declaration of the function
begin
s:=p*I;
end;

begin
// Function body — this is where the main operators are written

end.

In the descriptions section, you can also connect various files, libraries, classes, modules, etc. For this, the uses keyword is provided, followed by the name of the plug-in.

Example:

Program Hello;
uses crt // plug-in command line module to use additional functions

description of variables

begin
// Main program
end.

 

Program Body

 

The main operators are recorded here. This is where the program itself runs. The compiler, before executing statements from the body of the function, first allocates memory. He does it as follows: in the declarations section, he finds variables, constants and more, and allocates memory for them.

When variables are assigned values, the compiler stores the values in memory. The same thing happens with procedures or functions. The compiler reserves memory for them. The body of the function is executed only after the memory is allocated in the description section.

Example:

Program name;
var // Declaration of variables of different types
i, j, z: integer;
a, b: string;
d, e: char;

const // Declaration of constants
U:=5;
T:=6;

procedure sum() // Declare the
procedure begin
j:=U+T;
end;

function multyply() // Declaration of the function
begin
z:=j*I;
end;

begin
// Function body — here write the basic operators
i:=80;
sum(); Call the procedure that assigns a value
to the
multiply() variable; Call a function that returns z
writeLn(z);
end.

For code blocks, begin end statements are used, only a semicolon is placed at the end. In the main block of the program, a period is placed after the end. A block of code is a subroutine, so it is allocated to a separate begin end. Blocks use loops, functions, procedures, condition operators, objects (object Pascal), and other constructs.

Label description section

Labels are specified in the description section. They serve as a designation of the places in which part of the program the compiler will go when it hits the goto operator. Labels are declared by the label statement.

Example:

Program Tags;
label
place1; labels for the goto
place2 operator;
place3;

var
a, b: string;

begin
place1; Transition of the program here by the name of the write label
("a");
write(«b»);
write(«c»);
goto place1; Indicated which label to go to the
end.

In the example above, we created an infinite loop. The program will output "abc..." until the computer freezes.

Limitations of the program body

 

You cannot declare variables, functions, procedures, constants, or other types in the program body. This is the sole responsibility of the descriptions section. Declared variables can be assigned values in the body of the program, but not declared. If you do this, the compiler will throw an error.

Also, you cannot use uses in the body of the program. Functions and procedures are not declared here, but only called for execution. The same applies to tags. They should be declared in the description section.

Modules

 

When we connect a module through the uses operator, we can notice that its description is missing from the program. Modules are separate files that have their own description structure. Modules are a set of variables, constants, procedures, and functions that can be used in a host program. If you use the ABCPascal development environment, the muduls there are: crt, graphABC and others.

Modules are not written to the structure of the program, but are connected through the uses operator. Modules are usually stored in the project directory. If they are stored in another directory, then after uses you need to write the path to the module and its name.