Working with standard modules in pascal examples. Custom modules in pascal. General structure of a graphics program

Module is not an executable program, but only contains a set of facilities for use in an executable program: data types, variables, procedures, and functions. The source text of the module has the .pas extension. The module is compiled separately from the main program, the compiled module has the extension .tpu (Turbo Pascal Unit). The finished compiled module can be used in the main program using the uses clause, which is written immediately after the program name.

Turbo Pascal has standard modules: DOS, CRT (Cathode Ray Tube, Cathode Ray Tube), Printer, Graph, System and others. They do not need to be described, but can be immediately included in the program with the uses clause. The System module is used in all programs, so an exception is made for it, you can not include it in the uses clause, it will be included automatically. Other modules need to be included in the uses clause if the program uses the module's resources, such as procedures and functions.

Module Structure

The module has the following structure:
unit< имя >;
Interface
< интерфейсная часть >
Implementation
< исполняемая часть >
[Begin< инициирующая часть > ]
end.

Here:
unit- code word (English module); module header starter;
<имя> - module name (correct identifier);
Interface- code word that starts the interface part of the module;
Implementation- code word (English implementation); starts the executable part;
Begin- code word that starts the initiating part;
(part of the Begin module< инициирующая часть >optional);
end.- sign of the end of the module.

Thus, the module consists of a header and three constituent parts, any of which can be empty.

The interface opens code word Interface. This part contains declarations of all global objects of the module (types, constants, variables and subroutines) that should be made available to the main program and/or other modules.

It should be noted that all constants and variables declared in the interface part of the module, as well as global objects of the main program, are placed by the Turbo-Pascal compiler in an abundance of data segment ( maximum length segment 65521 bytes).

Order of appearance various sections ads and their number can be arbitrary.

In the interface part of modules, forward description cannot be used.

Executable part

The executable part begins with a code word Implementation and contains the bodies of procedures and functions declared in the interface part. This part can also declare objects local to the module: auxiliary types, constants, variables and blocks, as well as labels, if they are used in the initiating part. Global procedures and functions previously declared in the interface part must be declared in the same order as their headers appear in the interface part. The description of the global block in the executable part must be preceded by a heading in which it is allowed to omit the list of formal variables (and the result type for the function), since they are already described in the interface part.

But if the block header is given in full, i.e. with a list of formal parameters and a declaration of the result, it must match the header declared in the interface part.

Local variables and constants, and all programming codes, generated when the module is compiled, are placed in a shared memory segment.

Compiling modules

The Turbo Pascal environment has tools that control how modules are compiled and facilitate development. major programs projects. In particular, three compilation modes are defined: Compile, Make, and Build. The modes differ only in the way in which the module being compiled or the main program being compiled is associated with other modules declared in the Uses clause.

When compiling a module or main program in Compile mode, all modules mentioned in the Uses clause must be pre-compiled and the results of their compilation must be placed in files of the same name with the .TPU extension. For example, if a program (in a module) contains the Uses Global clause, then the directory declared by the Unit directories option must already contain the GLOBAL.TPU file on the disk. A file with the extension .TPU (from the English Turbo-Pascal Unit) is created as a result of module compilation.

In MAKE mode, the compiler checks for TPU files for each declared module. If any of the files is not found, the system tries to find the file with the same name with the .PAS extension, i.e. file with the source code of the module, and if the PAS file is found, it starts compiling it. In addition, in this case, the system monitors for possible changes to the source code of any module used. If any changes are made to the PAS file (module source code), then regardless of whether the corresponding TPU file already exists in the directory or not, the system compiles it before compiling the main program.

Moreover, if changes are made to the interface part of a module, then all other modules that access it will be recompiled as well. Make mode, therefore, greatly simplifies the process of developing large programs with many modules: the programmer gets rid of the need to monitor the correspondence between existing TPU files and their source code, since the system does this automatically.

In Build mode, existing TPU files are ignored and the system tries to find (and compile) the appropriate PAS file for each module declared in Uses. After compiling in Build mode, the programmer can be sure that all changes made by him in any of the modules are taken into account.

Connection of modules to the main program and their possible compilation is carried out in the order of their declaration in the Uses clause. When moving to the next module, the system first searches for all the modules to which it refers. Links of modules to each other can form a tree structure of any complexity, however, explicit or indirect reference of the module to itself is prohibited.

6 question

The CRT module is a library of procedures and descriptions that enhances the user's ability to work with text, the text screen, and the keyboard. Let's consider some of them.

1). TextMode(mode: integer) - selects the specified text mode CRT modes:

2).TextBackground (color: byte) - Selects background color.

The "color" parameter is an expression of an integer type, for example:

  • 0- black
  • 1-blue
  • 2-green
  • 3-blue

3). ClrScr - Screen cleaning. All character positions are filled with spaces. This uses the current background color specified in the TextBackGround procedure.

4). TextColor (color:byte) - Sets the color of the characters. ( constants to set color)

5). Window(x1,y1,x2,y2) - Specifies a text box on the screen. The x1,y1 parameters are the coordinates of the upper left corner of the window, the x2,y2 parameters are the coordinates of the lower right corner. The minimum size is one column per line. Maximum - X=80, Y=25. If the coordinates are invalid, then the call to the Window procedure is ignored. By default, the window is set to full screen.

6).GoToXY(x,y: byte) - Positions the cursor. The cursor moves to the position within the current window, which is specified by x and y coordinates (x - specifies a column, y - specifies a row). The upper left corner is specified by coordinates (1,1) example: Window(1,10,60,20); GoToXY(1,1); This will cause the cursor to move to a point with absolute coordinates (1,10).

7).WhereX and WhereY return the X and Y coordinates for the current cursor position relative to the current window, respectively. The result type is Byte.

8).Delay(ms: word) - Delays the given number of milliseconds. The ms parameter specifies the number of milliseconds of the timeout interval. But this procedure is approximate, so the delay period will not be exactly equal to the specified number of milliseconds.

9).READKey - Reads a character from the keyboard.

10).Sound - is a procedure that includes an internal speaker; Description: Sound(hertz: word); where the "hertz" parameter specifies the frequency of the generated signal in hertz. The sound will sound until it is turned off by calling the NoSound procedure;

11).NoSound - Turns off the internal speaker.

Sound,Delay,NoSound example

Sound(220); (turn on sound)

delay(300); (wait 300ms)

no sound; ( turn off the sound )

7 question.

Graph module

TP has quite a variety of tools for working with a standard VGA screen.

The VGA adapter has a resolution of 640x480 pixels (dot (0,0) in the upper left corner of the screen), 16 colors.

Before you start working with graphics, you need to initialize it, and when finished, close it. All graphical procedures and functions are located in the Graph module, so its inclusion is also necessary.

General structure graphics program:

Use crt, graph;
var Gd, Gm: Integer;
begin
Gd:= Detect;
InitGraph(Gd, Gm, "c:\bp\bgi");
...
(Here the construction of the image)
...
ReadKey;
Close Graph;
end.

The path c:\bp\bgi specifies the location of the egavga.bgi file (driver graphics adapter). On different computers this path may be different. If the egavga.bgi file is placed in the directory with the program, then the path can be omitted.

Basic graphics procedures and functions:

Building figures

PutPixel(x,y,c)
- displays a point on the screen with coordinates (x,y) and color c

Line(x1,y1,x2,y2)
- draws a line starting at (x1,y1) and ending at - (x2,y2)

Rectangle(x1,y1,x2,y2)
- draws a rectangle outline with diagonal (x1,y1) - (x2,y2)

Bar(x1,y1,x2,y2)
- draws a filled rectangle with diagonal (x1,y1) - (x2,y2)

Circle(x,y,r)
- draws a circle with center (x,y) and radius r

Ellipse(x,y,ba,ea,xr,yr)
- draws an ellipse arc centered at (x,y), horizontal and vertical radii xr and yr, and start and end angles ba and ea

FillEllipse(x,y,xr,yr)
- draws a filled ellipse centered at (x,y), horizontal and vertical radius xr and yr

Module (UNIT-module, unit) is an autonomously (separately) compiled program unit that contains the components of the description section (labels, constants, types, variables, procedures, functions), and may also contain initializing part operators.
The module itself is not an executable program, but is intended to be used by other programs and modules.

Module structure

The module has the following structure:
UNIT< module name >
INTERFACE
< interface section >
IMPLEMENTATION
< implementation section >
BEGIN
< initialization section >
END.

Module header consists of a reserved word unit(module) and module name.

The name of the module is chosen by general rules and must match the name disk file containing the source code of the module.
Module name extension (. pas ) is not specified, it is set by default.

The name of the module is used to link it to the main program with a clause uses .
Offer uses m.b. placed after the module header or behind the words

Interface And Implementation .

Interface part Interface(interface, articulation, connection) and contains references to other modules and declarations (descriptions) of global objects, i.e. labels, constants, types, variables, and procedure and function headers that are available to the main program and other modules (i.e. visible from outside).

Implementation section - begin with keywordImplementation(execution) and contains a description of objects local to the module, i.e. labels, constants, types, variables that are not available to the main program and other modules (i.e., not visible from outside) and Full description procedures and functions. In this case, in the header of subroutines, the list of formal parameters can be omitted, but if it is given, it must exactly match the description in the interface part.

Initialization section - enclosed in parentheses BEGIN END.
and contains statements that will be executed before control is transferred to the main program. This might be data (variables) initialization operators For example, assignment and input operators, as well as procedures for linking and opening files. Operators section can be. empty BEGIN END or just absent END .
A period is placed at the end of the module.

Compiling and Using Modules

The RAM system has a segment structure (one segment is equal to 64K = 65535 bytes). The program code can be no more than one segment, the amount of data cannot exceed one segment (unless using dynamic memory) and one segment for the stack. The stack size is set by the directive ($M<>). The minimum stack size is 1K and the maximum single segment is 16K by default. The values ​​of local variables are pushed onto the stack when the subroutine is called, and popped from the stack when exiting.
The module code is placed in a separate segment, because it is broadcast autonomously from the main program, and the number of modules used by the program depends only on the available OP. This allows you to create large programs.
The compiler generates module code with the same name but extension tpu (turbo pascal unit).
To use the module by the main program or other modules, its name (without extension) is placed in the suggestion list uses
If the module is compact and often used application programs, then it can be placed in the standard module library TURBO.TPL (Turbo-Pasacal-library ) using the utility TPUMOVER.
But this should be done only in case of emergency. the library is loaded into the OP and reduces the space for the program.
When compiling the file with the source code of the module, a file of the same name appears with the extension tpu and placed in the directory specified by the option

OPTIONS/DIRECTORIES/UNIT DIRECTORIES

or in the current directory if this option is not present.
When compiling the main program, the modules used must be in the directory specified by the option
OPTIONS/DIRECTORIES/EXE & TPU DIRECTORIES

or in the current directory in the absence of this option
For getting EXE task file in option

COMPILE/DESTINATION/DISK(MEMORI)
install DISK .
There are three modes for compiling modules:
- COMPILE
-BUILD
- MAKE

Modes set by menu COMPILE

1.Mode COMPILE(called Alt-F9 ) . In this case, the program is compiled, and the modules used should be. pre-compiled and stored in the appropriate directories.
2.Mode BUILD(called - F9) . In this case, previously compiled modules are ignored, and modules with the extension pas and recompiled.
3.Mode MAKE(called F9) . In this case, only modules that have had changes in the text are recompiled.

Example 16.1.

In file inp.txt there are three arrays of real numbers

2.1 3.1 -2.4 5.6 5.4 -6.7 3.5 -3.6

Compute function

where Max_a, Max_b, Max_c, Sa, Sb, Sc, ka, kb, kc are the maximum element, the sum and the number of positive elements of the corresponding arrays a, b, and c.
Output the result to a file out. txt and on the screen.

Module text

UNMAS;
Interface
Const n=10;
Type vec=array of real;
Varz:vec;
i:integer;
f1,f2:text;

Procedure SK1(z:vec; num:byte; Var s:real; Var k:byte);
Function MAX(z:vec; num:byte):real;

Implementation
ProcedureV(s:char; num:byte;Var z:vec);
Begin
Writeln("Array ",s);
For i:=1 to num do
Begin
Read(f1,z[i]); Write(z[i]:4:1," ":3);
end;
readln(f1); Writeln;
end;

Procedure SK1(z:vec;num:byte; Var s:real; Var k:byte);
Begin
s:=0; k:=0;
for i:=1 to num do if z[i]>0 then
Begin
s:=s+z[i];
k:=k+1
end;
end;
Function MAX(z:vec;num:byte):real;
varm:real;
Begin
m:=z;
for i:=1 to num do if z[i]>m then m:=z[i];
MAX:=m
end;

Begin
Assign(f1,"inp.txt"); reset(f1);
Assign(f2,"out.txt"); Rewrite(f2)
end.

Program text

Program lr7_16;
Uses CRT,UNMAS;
Var
a,b,c:vec;
y,sa,sb,sc:real;
ka,kb,kc:byte;
Begin
clrscr;
Vv("a",8,a);
vv("b",9,b);
vv("c",n,c);
SK1(a,8,sa,ka);
SK1(b,9,sb,kb);
SK1(c,n,sc,kc);
y:=(MAX(a,8)+MAX(b,9)+MAX(c,n))+(sa+sb+sc+ka+kb+kc);
Writeln("Result:":20);
Write ("Array a: ");
Writeln("sa=",sa:5:1," ka=",ka);
Write("Array b: ");
Writeln("sb=",sb:5:1," kb=",kb);
Write("Array c: ");
Writeln("sc=",sc:5:1," kc=",kc);
Writeln(" ":10,"y=",y:10);
readln;
Writeln(f2,"ђҐ§g"mv to:");
Writeln(f2," ":10,"y=",y:10);
close(f1);
Close(f2)
end.

Program results

Array a
-2.1 3.1 -2.4 5.6 5.4 -6.7 3.5 -3.6
Array b
2.3 -4.3 2.1 2.5 -3.7 -5.6 4.6 3.5 -7.5
Array c
-2.1 4.3 -2.3 7.6 4.5 -8.9 5.7 -4.5 6.8 -5.8
Result:
Array a: sa= 17.6 ka=4
Array b: sb= 15.0 kb=5
Array c: sc= 28.9 kc=5
y=9.330E+01

Modules in Pascal in relation to the main part of the program, they resemble subroutines (procedures and functions). But by definition they are independent programs, whose resources can be used in other programs. In addition, the description of modules occurs outside the calling application, and in separate file, so a module is a separately compiled program. The compiled module file (which is the one you need to use) will have the extension provided by the programming environment (for example, .tpu, .ppu, .pcu).

Modules are created, as a rule, to ensure the compactness of the code, which large projects have to take care of. It is also worth noting that the use of modules in a sense removes the restriction on memory segmentation, since the code of each module is located in a separate segment.

The module structure looks like this:

unit<имя модуля>;
Interface
<интерфейсная часть>
Implementation
<исполняемая часть>
Begin
<инициализация>
end.

Module name (Unit)

The name of the module following the keyword unit, must match the name of the file (without .pas) that contains its code. Also, using the name, the module is connected to another module, or to the main program. To do this, you must specify the service word uses, and list the list of plug-ins separated by commas:

uses<список имен модулей>;

Interface part (Interface)

The interface part describes the headers of objects that other modules and programs will have access to. These are constants, types, variables and subroutines. For example, this is how the interface part of the Search module may look like, which contains algorithms for searching for elements in an array.

1
2
3
4
5
6

unit Search;
Interface

vars: string

For announcement this module, in the program you need to specify its name:

After which it will become possible use all objects described in the interface part.

Executable part (Implementation)

This section begins with the word Implementation(implementation). It is here that you need to describe the subroutines declared in the interface part. At the same time, it is allowed not to specify formal parameters in their headers, otherwise they must completely match those in the interface part. In addition, the interface part may contain objects that are local (inaccessible to the calling program) for the module.

Initiating part

The initiating part begins its work before the start of the execution of the main program. It (between Begin and End), as a rule, describes operators intended for various kinds of support work. This part may be missing or have no code in it. In the first case, you need to specify End with a dot, in the second, leave an empty space inside Begin and End.

Compiling modules

Only compiled modules that have an extension provided by your application development environment can be used in the program. Consider the three most popular of them:

TurboPascal

The result of compiling a module in Turbo Pascal will be a file with the extension .tpu (Turbo Pascal Unit) that stores its code.

Free Pascal

After compiling a module in the Free Pascal environment, two files are created with different permissions: .ppu And .o. The first contains the interface part of the module, and the second (required for linking the program) is the implementation part.

Pascal ABC.NET

Pascal ABC.Net does not generate code during module compilation machine language. If the compilation is successful, the code is saved in a file with permission .pcu.

There are three compilation modes for the Turbo Pascal and Free Pascal programming environments: Compile, Make and Build. In Compile mode, all modules used in the program must be compiled beforehand. An application in Make compilation mode checks all connected modules for the presence of files with the appropriate permission for the programming environment (.tpu or .o). If any of them is not found, then a file with the name of the not found module and the .pas extension is searched. The most reliable of the modes is Build. Finding and compiling files (with .pas extension) in this mode occurs even when module files are already present.

Example

Let's create a small module containing the procedures for binary and linear search for elements in an array. Module code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

unit Search;
Interface
type arr = array [ 1 ..5 ] of integer ;
vars: string
procedure binary_search(x: integer ; Ar: arr; var s: string ) ;
procedure line_search(x: integer ; Ar: arr; var s: string ) ;
Implementation
var a, b, c, i: integer ;
procedure binary_search(x: integer ; Ar: arr; var s: string ) ;
begin
a:= 1 ; b:= 5 ; s:= 'NO' ;
while a<= b do
begin
c:= a+ (b- a) div 2 ;
if (x b:= c- 1
else if (x>Ar[ c] ) then
a:= c+ 1
else
begin s:= 'YES' ; break ; end ;
end ;
end ;
procedure line_search(x: integer ; Ar: arr; var s: string ) ;
begin
s:= 'NO' ;
for i:= 1 to 5 do
begin
if (Ar[i] = x) then
begin
s:= 'YES' ; break ;
end ;
end ;
end ;
end.

All this code should be in a separate file. Now let's write the main program, in which we will connect our Search module.

Module (unit) is a set of constants, data types, variables, procedures and functions. Each module is similar to a separate Pascal program: it can have a main body that is called before your program starts and performs the necessary initialization. In short, a module is a library of declarations that you can insert into your program and that will allow you to break the program into parts that are compiled separately.

Turbo Pascal gives you access to a large number of built-in constants, data types, variables, procedures, and functions. Some of them are specific to Turbo Pascal; others are specific to the IBM PC (and compatible computers) or to the MS-DOS operating system. Their number is large, however, in your program you rarely use them all at once. Therefore, they are divided into related groups called modules. In this case, you can use only those modules that are necessary in the program.

Module structure

A module provides a set of features through procedures and functions supported by constants, data types, and variables, but the actual implementation of these features is hidden because the module is divided into two sections: interface and implementation. If a program uses a module, then all module declarations become available to that program as if they were defined in itself.

The structure of a module is similar to that of a program, but there are a few significant differences. For example, consider the module:

unit<идентификатор>; interface uses<список модулей>; (Optional) (open descriptions) implementation (closed procedure and function descriptions) begin (initialization code) end.

The module header begins with the reserved word unit followed by the module name (identifier), just as in the case of a program name. The next element in a module is the interface keyword. It marks the start of a module's interface section - the section that is visible to all other modules or programs that use it.

A module can use other modules, for this they are defined in the uses clause. The uses clause, if present, follows immediately after the interface keyword. Note that the general rule for using the uses clause is satisfied here: if the module whose name is specified in the uses clause uses other modules, then the names of these modules must also be specified in the uses clause, and before they are used.

Standard modules

The TURBO.TPL file contains all standard packages except Graph and the compatibility packages (Graph3 and Turbo3): System, Overlay, Crt, Dos, and Printer. These packages are loaded into memory with Turbo Pascal and are always available to you. Typically, the TURBO.TPL file is stored in the same directory as TURBO.EXE (or TPC.EXE). You can store it in another directory as long as it is described as a Turbo Pascal directory. To do this, use TINST.EXE to set this directory in the TURBO.EXE file.

Packages used: no

System contains all the standard and built-in procedures and functions of Turbo Pascal. Any Turbo Pascal subroutine that is not part of standard Pascal and not found in any other module is contained in System. This module is attached to all programs.

Packages used: no

DOS defines numerous Pascalian routines and functions that are equivalent to the most commonly used DOS calls, such as GetTime, SetTime, DiskSize, and so on. In addition, it defines two low-level programs, MsDos and Intr, which allow you to activate any MS-DOS call or system interrupt. Registers is the data type for the parameter in MsDos and Intr. In addition, some other constants and data types are defined.

Packages used: no

Overlay - contains tools for creating overlay programs. The OVERKAY program is a program that does not load all, but in parts.

Packages used: no

Crt provides a set of PC-specific descriptions of constants, variables, and programs for I/O operations. The latter can be used to work with the screen (setting windows, direct cursor control, text and background colors). In addition, you can make "raw" keyboard input and control the sound generation board of a personal computer. This module provides many routines that were standard in version 3.0.

Packages used: Crt

The Printer module defines a text file variable, Lst, that is associated with a device driver that allows standard Pascal output to be sent to a printer using Write and Writeln. For example, by including Printer in your program, you can do the following:

Write (Lst,"Sum of " ,A:4 ," and " ,B:4 ," equals " ) ; C:=A+B; WriteIn(Lst,C:8) ;

Packages used: Crt

Graph3 supports the full set of graphics routines for version 3.0 - for regular graphics, advanced graphics, and graphics using relative commands. They are identical in name, function parameters, and version 3.0 routines.

Packages used: Crt

This module contains two variables and several procedures that are no longer supported by Turbo Pascal. These include the built-in file variable Kbd, the boolean variable CBreak, and the original integer versions of MemAvail and MaxAvail (which return free memory in paragraphs, not bytes as the original versions do).

Packages used: Crt

Graph provides a set of fast, efficient graphics routines that allow you to take full advantage of your personal computer's graphics capabilities.

This module implements a Borland device-independent graphics driver that allows support for CGA, EGA, Hercules, ATT 400, MCGA, 3270 PC, and VGA graphics adapters.

Writing your own modules

Let's say you wrote an IntLib module, wrote it to the INTLIB.PAS file, and translated it to disk; the resulting code is in the INTLIB.TRU file. To use this module in your program, you must include a uses statement in your program to tell the compiler which module to use. Your program might look like this:

program MyProg; uses IntLib;

Note that Turbo Pascal assumes that the file containing the module has the same name as the module itself. If your module name is MyUtilities, then Turbo Pascal will look for a file named MYUTILIT.PAS.

Module compilation

A module is compiled in exactly the same way as a program is compiled: it is created using the editor, and then the Compile / Сompile command is called (Compile / Compile) (or the Alt-C keys are pressed). However, instead of a file with the extension .EXE, Turbo Pascal creates a file with the extension .TRU (Turbo Pascal Unit - Turbo Pascal Unit). After that, you can leave this file as it is, or insert it into TURBO.TPL using TPUMOVER.EXE.

In any case, it makes sense to send the *.TRU files (along with the source files) to the unit directory, which is defined with the O/D/Unit directories command. There can only be one module per source file, because compilation stops as soon as the terminating end statement is encountered.

Example:

Let's write a small module. Let's call it IntLib and insert two simple subroutines for integers into it - a procedure and a function:

unit IntLib; interface procedure ISwap(var I,J: integer ) ; function IMax(I,J: integer ) : integer ; implementation procedure ISwap; var Temp: integer ; beginTemp:=I; I:=J; J:=temp end ; (end of ISwap procedure) function IMax; begin if I > J then IMax:=I else IMax:=J end ; (end of IMax function) end. (end of IntLib module)

Let's introduce this subroutine, write it to the INTLIB.PAS file, and then translate it to disk. As a result, we get the module code in the INTLIB.TRU file. Let's send it to the modules directory. The following program uses the IntLib module:

program IntTest; uses IntLib; varA,B: integer ; begin Write( "Enter two integer values: ") ; Readln(A,B) ; ISwap(A,B) ; WriteIn("A=" ,A," B=" ,B) ; writeln( "The maximum value is ",IMax(A,B) ) ; end. (end of IntTest program)

All declarations within a module are related to each other. For example, the Crt module contains all the descriptions needed for the screen routines on your personal computer.

In the Pascal language, libraries of subroutines are used to implement structured programming technology using modules. Textually, libraries are combined into independent program units called modules of the Pascal language. Modules are created to implement libraries of routines. Typically, modules combine subprograms that perform tasks of the same class.

Module is an independent program unit that is autonomously compiling and has a certain structure.

The module has the following structure:

The title of a Pascal module consists of the service word Unit followed by the name of the module. There are strict requirements for the module name, and this name must match the name of the disk file with the .pas extension, which contains the module text.

UnitMyType; //file MyType.pas

The module name is used to link the module to programs and other modules. Programs and modules link to the required module in the Uses section of the program or module. Modules can call other modules for their work, if a module calls another module, then the Uses section must follow the INTERFACE keyword.

The interface part of the Pascal language begins with the reserved word INTERFACE. It serves to link the module with the host program or other modules. The interface part contains the declaration of all global objects of the module. First of all, subroutines, and in addition, global types and variables are declared. If an object is declared globally, then it can be used in the calling program and module without a special declaration.

Unit Watermass;

INTERFACE

USES MyType;

[type_name]:[type];

[list_of_global_variables]:[type];

In the interface part, subroutines are declared by their headers, which include the name of the subroutine and a list of formal parameters.

The executable part of the Pascal language begins with the service word IMPLEMENTATION and contains a description of the subprograms declared in the interface part, in addition, objects local to the module (variables, constants) can be declared in the executable part. The description of a subroutine in the executable part must begin with the title of the subroutine, while the list of formal parameters is omitted.

The initiating part of the Pascal language terminates the module, and this part may be absent or may be empty. Doing the initiating part is not recommended as hang situations can occur. It contains executable statements, sets the initial values ​​of global variables, opens necessary files, links to other files are established.

Compiling modules in Pascal

When working with modules, you must compile the modules themselves and compile programs that use the modules. As you know, there are three compilation modes in the Borland Pascal environment (Compile, Build, Make). When compiling the module itself, the Compile mode is usually used, although other modes are possible if the module is used by other modules. Compiling a module does not create an executable program, but creates a special compiled module file with the .tpw extension. Compiled unit files are placed in directories, usually named Units (specified in the environment settings). When compiling programs containing modules, all three compilation modes can be used. Compilation different regimes differ in duration and reliability.

Mode Compile- the simplest and fastest. In this mode, the compiler checks for the presence of plug-ins in the Uses section and compiles programs if they are present. If the module is missing, compilation stops and a message about the missing module is displayed.

In mode Make the compiler checks for the presence of plug-ins, and if a module is not found, the compiler looks for the text of the module with the .pas extension, compiles the module, and then compiles the program. In addition, the compiler checks the creation dates of the .tpw and .pas modules. If it finds that the creation date of the .pas file is later than the .tpw file, it deduces that changes have been made to the module text and recompiles the modules. Thus, it is more reliable.

In mode Build all .tpw files are ignored, the compiler will recompile all modules used in the main program and compile the program. This is the most reliable mode, but at the same time the longest.

Liked the article? Share with friends: