A1: Many of the text-
books in the bibliography section of this FAQ
discuss using units in Turbo Pascal. For example see Tom Swan
(1989), Mastering Turbo Pascal 5.5, Chapters 9 and 10 for a more
detailed discussion than the rudiments given in the current item.
Likewise see your Turbo Pascal (7.0) User's Guide Chapter 6,
"Turbo
Pascal units".
You can and need to write your own units if you need recurring or
common routines in your programs and/or your program becomes
so big
that it cannot be handled as a single entity.
A Turbo Pascal unit is a separate file which you compile. The
following trivial example to calculate the sum of two reals
illustrates the basic structure of a unit.
{ The name of this file must be faq73.pas to correspond. }
unit faq73;
{}
{ The interface section lists definitions and routines that are }
{ available to the other programs or units. }
interface
function SUMFN (a, b : real) : real;
{}
{ The implementation section contains the actual unit program }
implementation
function SUMFN (a, b : real) : real;
begin
sumfn := a + b;
end;
{}
end.
When you compile the file FAQ73.PAS a unit FAQ73.TPU results.
Next
an example utilizing the faq73 unit in the main program.
uses faq73;
{}
procedure TEST;
var x, y, z : real;
begin
x := 12.34;
y := 56.78;
z := SUMFN (x, y);
writeln (z);
end;
{}
begin
TEST;
end.
A2: Most often you
would be compiling a Turbo Pascal program
using the IDE (Integrated Development Environment). If you have
precompiled units you must see to it that you have informed the
IDE
of the path to them.
Press F10 and invoke the "Options" menu (or press
alt-O). Select
"Directories...". Press tab two times to get to "Unit
directories"
and edit the path accordingly. Here is what I have entered myself
EXE & TPU directory r:\
Include directories r:\
Unit directories f:\progs\turbo70\tpu70
Object directories f:\progs\turbo70\tpu70
As you see I keep all my precompiled Turbo Pascal 7.0 units in
the
f:\progs\turbo70\tpu70 directory.