Unit 1 Ready !





Hi ! Nice to meet you ! This time we will discuss about units. What is units anyway ? It's like having a library of commands, like crt unit, that we often use so long. Now, we're gonna make one.

What's so different with units ? Well, units have structures like this :


{This code must be saved with name WHAT.PAS}
unit what;

interface
uses ....
var
  ...    { Visible global variables }

{ Procedures & functions definitions }
procedure myproc;
function  myfunc: word;
:
:

implementation
var
  ...    { Invisible global variables }

procedure myproc;
begin
  :  { the routines }
  :
end;

function myfunc:word;
begin
  :  { the routines }
  :
end;

:
:  { other procedure's / function's routines }
:

begin
  :   { Initialization code }
  :
end.

That was the structure. The interface part contains definitions of procedures or functions and the variables. All definitions ( procedures, functions, and variables ) in this part are visible to the user of your unit. It means that the user who put your unit in their uses clause can invoke the declared procedure/function and can easily change the variables.

The implementation part contains the contents of all procedures and variables which are either visible or not. Variable declaration in this part is NOT visible. Means that user cannot modify them. Only the procedure or function inside that unit can.

Attention : Unit name must comply to the filename.
Look at this example :


{ This unit must be stored in MYUNIT.PAS }
unit myunit;
interface
uses crt;

var
  x   : byte;
  y,z : integer;

procedure a;
procedure b;
function  c:byte;

implementation
var
  p,q,r : shortint;

procedure a;
begin
  :
  :
end;

procedure d;
begin
  :
  :
end;

procedure b;
begin
  :
  :
end;

function c: byte;
begin
  :
  :
end;

procedure e;
begin
  :
  :
end;

begin
  :  { Initialization code }
  :
end.

Suppose I make a program using the unit above :


uses myunit;

var
  n : byte;
begin
  a;      { legal }
  b;      { legal }
  n:=c;   { legal }
  x:=1;   { legal }
  y:=-1;  { legal }
  z:=14;  { legal }
  
  d;      { illegal, because it is invisible }
  e;      { illegal, because it is invisible }
  p:=-1;  { illegal, because it is invisible }
end.

I can call procedure a, b, and function c, within my program, but not procedure d, and e. Access to variable x, y, and z is legal, but not to p, q, and r.

Procedure d and e can only be invoked INSIDE that unit, for example procedure b call procedure d. The invokability rules are just the same as normal program, i.e. d can call a, but a cannot call d.

Easy, right ? Now, what's all this for ? If you have made procedures or functions and you think that you will use them a lot in the future, you may want this placed in a unit so that you can re-use them.

Mind this : This is a VERY important feature and you shall heed it.

Start thinking of procedures or functions that you often use, then place it in a unit. Think seriously, because I will ask you that in the quiz. Look at this unit (MYUNIT.PAS). It is a simple unit, but works !


unit myunit;
interface
uses crt;

procedure clearbkgr(color : byte);
procedure makewindow(x1, y1, x2, y2, color : byte);
procedure writexy(x, y : byte ; mess : string);
function  ucase(st:string):string;

implementation

procedure clearbkgr(color : byte);
begin
  textbackground(color);
  clrscr;
end;

{ Invisible to unit user, this is internal function }
function fill(s : string; count : byte; what: char):string;
begin
  fillchar(s,count+1,what);
  s[0]:=chr(count);
  fill:=s;
end;

procedure makewindow(x1, y1, x2, y2, color : byte);
var
  s  : string;
  i  : byte;
begin
  textattr:=color;
  s:=fill(s,x2-x1-1,#205);
  gotoxy(x1,y1); write(#201+s+#187);
  gotoxy(x1,y2); write(#200+s+#188);
  s:=fill(s,x2-x1-1,' ');
  s:=#186+s+#186;
  for i:=1 to y2-y1-1 do
  begin
    gotoxy(x1,y1+i); write(s);
  end;
end;

procedure writexy(x, y : byte; mess : string);
begin
  gotoxy(x,y); write(mess);
end;

function ucase(st:string):string;
var
  i : byte;
begin
  for i:=1 to length(st) do st[i]:=upcase(st[i]);
  ucase:=st;
end;

begin
  { Leave this empty if you don't have any initialization code }
end.

What is the initialization codes ? If you think that you need to do routines before the unit is ready to use, you may do it in the main begin...end block. It's similar to preparation code, that is to prepare your unit. For example : You feel that some variables need to be set, do it in that section. If you need some setup code need to be established, do that too in the same way. Initialization codes is done before the begin...end block of the main program that use your unit. Suppose the program is like this :


:
:
uses unit_a, unit_b, unit_c;
:
:
:
begin
  :
  :
end.

Initialization code in unit_a is done first, then unit_b, then unit_c, then the main begin...end.

This is quite a short lesson, and this time EXTRA !

Converting numbers into bits, vice versa. Easy ! Divide the number by 2 :


                53
            2 ------ 1      (53 mod 2 = 1)
                26          (53 div 2 = 26)
            2 ------ 0      (26 mod 2 = 0)
                13          (26 div 2 = 13)
            2 ------ 1      (13 mod 2 = 1)
                 6          (13 div 2 = 6)
            2 ------ 0      ( 6 mod 2 = 0)
                 3          ( 6 div 2 = 1)
            2 ------ 1      ( 3 mod 2 = 1)
                 1          ( 3 div 2 = 1)

Stop here, since 1 is less than 2. So the binary form of 53 is 110101. Look at this example :

               100    /|\     41             37
            2 ------ 0 |   2 ---- 1       2 ---- 1
                50     |      20             18
            2 ------ 0 |   2 ---- 0       2 ---- 0
                25     |      10              9
            2 ------ 1 |   2 ---- 0       2 ---- 1
                12     |       5              4
            2 ------ 0 |   2 ---- 1       2 ---- 0
                 6     |       2              2
            2 ------ 0 |   2 ---- 0       2 ---- 0
                 3     |       1              1
            2 ------ 1 |
                 1     | Direction to read

So, 100 is 1100100 in binary, 41 is 101001, and 37 is 100101. Easy, no ? Now, how to convert binary digits (bits) into decimals ? Suppose we have :

Shorter one :

Easy, right ? If you want to convert numbers to hexadecimal, it is just similar, but change the divisor 2 into 16, like this :


      100            41            53           37           47
  16 ----- 4     16 ---- 9    16 ------ 5   16 ---- 5    16 ---- 15
        6             2             3            2            2

Means that 100 = 64 hex, 41 = 29 hex, 53 = 35 hex, 37 = 25 hex, 47 = 2F hex Any number exceed 9 is decoded like this :

   10   into   A
   11   into   B          61             44            58
   12   into   C     16 ------ 13   16 ------ 12  16 ------ 10
   13   into   D           3              2             3
   14   into   E     61 = 3D hex     44 = 2C hex    58 = 3A hex
   15   into   F

Converting back to decimal is the same :

And so on.
That's all folks for this time ! Good bye ! Don't hesitate to mail me !


Where to go ?

Back to main page
Back to Pascal Tutorial Lesson 1 contents
To the quiz
Back to Chapter 11 about sets
To Chapter 13 about text files
My page of programming link
Contact me


By : Roby Joehanes, © 1997, 2000