[Pascal – TUT] Posts 8: Unit

The creation of the subroutine in a program made to programming objects and much more. However, This program is effective only child in our program contains only, in another program wants to use the program are required to write to them, so very laborious. To remedy, they collect the subroutine often used as a standalone module and precompiled on Disc. Later, any program can reuse this subroutine without rewriting them. Such modules are called UNIT.

There are two types of standard UNIT is Pascal Unit and Unit created by programmers to create their own service.

pascal

1. Some standard unit

the. Some standard unit

– Unit CRT: Including constant, type, variable, jaw, procedures related to the text display mode (TEXT mode).
– Unit PRINTER: Including constant, type, variable, jaw, procedures related to printing mode LPT1 port (Connector DB25).
– Unit GRAPH: Including constant, type, variable, jaw, procedures related to graphics mode.
– Unit DOS: Including constant, type, variable, jaw, procedure involves direct processing registers, interrupts and calls to the functions of MS-DOS operating system.
– Unit OVERLAY: Including constant, type, variable, jaw, procedures relating to the placement of executable code is accessible on disk (loaded / Elegant) instead of all at once put into memory when running program.

When you want to use a Unit occurs, we must declare that the name at the top of the program Unit (except the default unit as unit Pascal SYSTEM) with syntax”

USES <tên unit>;

b. Some functions and procedures commonly used in CRT Unit
– ClrScr: Procedures to delete screen.
– GotoXY(x, and: Byte): Move the cursor to the position column x, y line on screen. Common, screen in textmode(Co80) have 25 current (automatic 1 to flow 25) and 80 post (post 1 to column 80). So the top-left coordinates of the screen's (1, 1), Bottom right coordinates are (80, 25) .
– Delay(ms: Word): Procedures program delay in milliseconds ms.
– Sound(hz: Word): Procedures emit sound through the speakers inside (internal speaker) hz frequency.
– NoSound: Procedures to stop the sound.
– Keypressed: The function result is TRUE if a key is pressed.
– Readkey: read keys from the keyboard buffer.
– TextBackGround(color: Byte): The procedures selected background color. We can set the background color for the entire screen by placing this command just before the command clrscr.
– TextColor(color: Byte): The procedures selected colored text.
Below is a list of predefined constants Pascal color.
• Black = 0 Come.
• Blue = 1 Blue.
• Green = 2 Green.
• Cyan = 3 Green eggs flute.
• Red = 4 Dirty.
• Magenta = 5 Purple lotus.
• Brown = 6 Brown.
• LightGray = 7 Light gray.
• DarkGray = 8 Dark gray.
• LightBlue = 9 Light blue.
• LightGreen = 10 Green Light.
• LightCyan = 11 Bright green eggs flute.
• LightRed = 12 Bright red.
• LightMagenta = 13 Bright purple lotus.
• Yellow = 14 Gold.
• White = 15 Page.
(8 first reigns from Black to LightGray applies to both the text color and background color. The remaining reigns applies only text color).
Color + blink: blinking word.

2. Construction unit

the. Step 1
Pascal creates a file ending in .PAS and structured as shown below, Note the name of the unit must match the file name. The form of a Unit (filename is MyUnit.pas):

Unit MyUnit ;
Interface
Khai báo Uses
Khai báo Const, Type, Var
Khai báo Procedure, Function
Implementation
Khai báo Uses
Khai báo Const, Type, Var
Cài đặt các Procedure, Function
Begin
	Các lệnh khởi tạo – chỉ xuất hiện khi có từ khoá Begin
End;

UNIT <Tên Unit>; {Tên unit bắt buộc phải trùng với tên tập tin}
INTERFACE {Không có dấu ; ở đây}
{Đây là phần giao diện của Unit. Trong phần này chúng ta sẽ khai báo các unit
đã có mà các unit này sử dụng, khai báo các hằng, kiểu, biến mà các chương
trình khác sẽ sử dụng. Khai báo các hàm, thủ tục mà chương trình khác sẽ gọi
tới, chỉ khai báo tên chương trình con, các tham số, kiểu kết quả. Những hàm,
thủ tục thiết lập ở phần sau mà không khai báo trong phần này thì các chương
trình khác không gọi tới được.}
IMPLEMENTATION {Không có dấu ; ở đây}
{Đây là phần hiện thực các hàm, thủ tục đã khai báo trong phần Interface. Trong
phần này nếu có các chương trình con được dùng riêng bên trong Unit mà không
khai báo trong phần Interface, các chương trình con này sẽ không thể truy cập
được bởi người dùng Unit.}

BEGIN
{Phần chứa các câu lệnh sẽ được thực thi ngay trước khi câu lệnh đầu tiên của
chương trình gọi Unit này được thực hiện. Phần này không bắt buộc phải có, tuy
nhiên trong trường hợp đó vẫn phải giữ lại từ khóa "END." dưới đây.}
END.

b. Step 2
Unit is not designed to run that set up the disc to compile, so we can not press CTRL F9 to do according to the following order:
– Chọn menu Compile (Everything + C).
– Continue to turn into Disk Destination. Noted: Destination Disk is created on the disk unit, Memory is generated on the RAM unit.
– Choose the menu and select the function complie complie (Everything + F9). Now that the file appears on disk is the name of my unit was created with the extension TPU. From here, One can use this unit by calling it in the command uses as mentioned above.

Example:

Unit MyUnit; {Trùng tên với tập tin MyUnit.pas}
INTERFACE
Function HamMu(a: Real; n: Integer): Real;
Function GiaiThua(n: Integer): Longint;
Function USCLN(X,Y:Word):word;
IMPLEMENTATION
	Function HamMu(a: Real; n: Integer): Real;
	Var tam: Real;
	i: Integer;
	Begin
		tam := 1;
		For i:=1 to n do
			tam := tam * a;
		HamMu := tam;
	End;

	Function GiaiThua(n: Integer): Longint;
	Var tam: Longint;
	i: Integer;
	Begin
		tam := 1;
		For i:=1 to n do
			tam := tam * i;
		GiaiThua := tam;
	End;

	Procedure HoanChuyen(var x,y:word);
	Var Tam:word;
	begin
		Tam:=x;
		x:=y;
		y:=Tam;
	End;

	Function USCLN(x,y:Word):word;
	begin
		While (y<>0) DO
		Begin
			if (x<y) then HoanChuyen(x,y)
			else x:=x-y;
		End;
		USCLN:=x;
	End;
END.


Original article : vietsource.net