home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 15 / CDACTUAL15.iso / cdactual / program / pascal / PAS_TUT.ZIP / CHAP13.TXT < prev    next >
Encoding:
Text File  |  1991-02-04  |  14.4 KB  |  354 lines

  1.  
  2.  
  3.  
  4.                                                        Chapter 13
  5.                                             UNITS IN TURBO PASCAL
  6.  
  7.  
  8. When Nicklaus Wirth originally defined Pascal, it was intended to
  9. be a very small language to be used primarily for teaching
  10. programming concepts to computer neophytes.  A program would be
  11. contained in a single file and compiled in its entirety each time
  12. it was compiled.  There was no provision for splitting a program
  13. up into smaller parts, compiling each part separately, and linking
  14. all of the parts together into a final completed package.
  15.  
  16. Since human beings make mistakes, and because the entire program
  17. must be recompiled each time any mistake is discovered, pure Pascal
  18. is unsuitable for very large programs.  Seeing this problem, many
  19. compiler writers have defined some method by which a large program
  20. could be broken down into smaller parts and separately compiled. 
  21.  
  22.  
  23. This chapter will define and illustrate the way Borland
  24. International has chosen to allow TURBO Pascal to be broken up into
  25. smaller pieces to permit compilation of smaller portions of a
  26. program.  This allows you to write a much larger program since it
  27. does not have to be compiled all at once.
  28.  
  29.  
  30. PART OF A PROGRAM
  31. _________________________________________________________________
  32.  
  33. Load the program named AREAS.PAS and display it   ===============
  34. on your monitor.  This is the first example of       AREAS.PAS
  35. a TURBO Pascal unit and although it is similar    ===============
  36. to a program in many ways, it has a few
  37. differences which must be pointed out.  We will
  38. start by pointing out the major sections, then get into the details
  39. of each section.
  40.  
  41. You will first notice that this program begins with the reserved
  42. word unit instead of our usual program, followed by the unit name,
  43. Areas.  In line 10, the reserved word interface is used and all of
  44. the statements following it down to the next reserved word
  45. implementation, are part of the interface with any program outside
  46. of this unit.  The reserved word, implementation, defines the
  47. beginning of the definitions and executable parts of the private
  48. portion of the unit.
  49.  
  50. Finally, in lines 48 through 50, we find what appears to be a
  51. program block just like we have been using all through this
  52. tutorial, but actually is not.  We will see in a few paragraphs
  53. that this is the initialization section and does a very specific
  54. job for us even though somewhat different than what we have become
  55. used to.
  56.  
  57.  
  58.                                                         Page 13-1
  59.  
  60.                                Chapter 13 - Units in Turbo Pascal
  61.  
  62. THE INTERFACE PART
  63. _________________________________________________________________
  64.  
  65. Following the unit name we have a section of code in lines 10
  66. through 15 that define the interface of this module to the outside
  67. world.  Anything defined here is available to the outside world and
  68. can be used by any other program provided it has a "uses Areas;"
  69. statement in it.  Constants, types, and variables could also be
  70. defined here, and if they were, they too would be available to any
  71. user program, but in this case, only the four functions are made
  72. available. It should be fairly obvious that the functions calculate
  73. the areas of four different geometric shapes.
  74.  
  75. These four functions are available for use in any program in much
  76. the same way that any of the standard Pascal functions are
  77. available for use.  The only difference is that a uses clause must
  78. be included in order to use these functions.
  79.  
  80.  
  81. THE IMPLEMENTATION PART
  82. _________________________________________________________________
  83.  
  84. From line 16 through line 47 we have the implementation part as
  85. delineated by the reserved word implementation and the beginning
  86. of the initialization block.  The implementation part is the actual
  87. workhorse of the unit since it contains all of the executable code
  88. for the four functions defined above.  
  89.  
  90. Lines 26 through 31 contain the code needed to generate the area
  91. of a circle, and this code is no different than the code that would
  92. be used if this function were placed in the declaration part of any
  93. Pascal program.  There is a difference in the function header since
  94. the formal parameters are not repeated here, but are defined only
  95. in the interface part in this example.  TURBO Pascal allows you to
  96. either drop the formal parameters here or include them if you think
  97. the code would be more readable.  If you include them, they must
  98. be exactly as shown in the interface part or you will get a compile
  99. error.
  100.  
  101.  
  102.  
  103. A LOCAL PROCEDURE
  104. _________________________________________________________________
  105.  
  106. In lines 20 through 24, we have a procedure that is used within one
  107. of the four functions, namely the first.  It is really a stupid
  108. procedure since it really wastes time setting up linkage for the
  109. procedure call and does nothing that couldn't be done just as easy
  110. with a simple multiply, but it does illustrate that you can use
  111. another procedure within the unit body.  The procedure
  112. Mult_Two_Numbers cannot be used outside of this unit because it is
  113. not included in the interface part of the unit.  It is, in effect,
  114. invisible to the outside world.
  115.  
  116.  
  117.                                                         Page 13-2
  118.  
  119.                                Chapter 13 - Units in Turbo Pascal
  120.  
  121. The variable My_Pi would be more correctly represented as a
  122. constant but it is defined as a variable to illustrate the use of
  123. the body of the unit later.  Since My_Pi is not defined in the
  124. interface part of the unit, it also is invisible to the outside
  125. world and in fact protected from accidental corruption by a
  126. misplaced statement in another program.  The procedure
  127. Mult_Two_Numbers and the variable My_Pi for all practical purposes
  128. have an impenetrable barrier around them protecting them from
  129. unauthorized use or modification by the outside world, but the
  130. functions internal to this unit have free access to them just as
  131. in any other program.
  132.  
  133.  
  134. WHAT IS THE BODY USED FOR?
  135. _________________________________________________________________
  136.  
  137. Lines 48 through 50 constitute the body of the unit and although
  138. they appear to consist of another executable program that can be
  139. called and used, they actually perform another very specific and
  140. useful purpose.  This is actually an initialization section and all
  141. of the statements in this part of the unit are executed once and
  142. only once, and they are executed when the main program is loaded. 
  143. This is done automatically for you by the system.  There is no way
  144. provided for you to call the statements in the body after the
  145. program has begun execution.  This is why the variable My_Pi was
  146. defined as a variable, so we could use this section to initialize
  147. it to a useful value as an illustration.
  148.  
  149. The body can actually have function and procedure calls that are
  150. executed when the program is loaded, as well as loops or
  151. conditional statements.  
  152.  
  153. If you would like to execute some statements during initialization
  154. and again during the execution of the program one or more times,
  155. you can write a procedure or function to accomplish your desires
  156. and call it at the appropriate times in the main program.
  157.  
  158.  
  159. SELECTIVE NAMING OF FUNCTIONS AND PROCEDURES
  160. _________________________________________________________________
  161.  
  162. If you will study the interface part of this unit you will find
  163. that everything you need to use this unit is contained within it,
  164. provided that you know enough about plane geometry to understand
  165. the functions.  You should strive for this understanding in all of
  166. your interfaces so that the implementation doesn't even require
  167. consultation.  Keep in mind, that if you need to, you can include
  168. comments to further define the functions in the interface part of
  169. the unit.
  170.  
  171. At this time, you should compile this unit.  You will have to
  172. compile it to disk rather than only to memory so it will be
  173. available for use later in this chapter.  You do this by using the
  174. menus to change the Compile/Destination to the Disk option.  Note
  175.  
  176.                                                         Page 13-3
  177.  
  178.                                Chapter 13 - Units in Turbo Pascal
  179.  
  180. that it will not generate an .EXE file but instead a .TPU file. 
  181. This is Borland's filename extension for a unit.
  182.  
  183.  
  184. ANOTHER UNIT
  185. _________________________________________________________________
  186.  
  187. Load the file named PERIMS.PAS for another       ================
  188. example of a unit.  This is similar to the last     PERIMS.PAS
  189. except that it does not contain an internal      ================
  190. procedure, and it is composed of three
  191. procedures that calculate the perimeters of
  192. geometric shapes, all of which are visible to the outside world
  193. because they are included in the interface part of the unit.  Once
  194. again, we have a private variable named My_Pi and a block of code
  195. (actually a single statement) to initialize the value of My_Pi when
  196. the unit is loaded.
  197.  
  198. Be sure you compile this unit to disk in the same manner as the
  199. last and they will be ready for use.  Note that it is not a
  200. requirement that a unit be composed of only functions or only
  201. procedures.  They can be freely mixed in a unit.  It was only done
  202. this way in this example because it was convenient.
  203.  
  204. Now that we have several functions and procedures that can be used
  205. to calculate the areas or perimeters of several different shapes,
  206. we need a program to illustrate their use, so if you load and
  207. display the program named GARDEN.PAS you will have an example of
  208. their use.
  209.  
  210.  
  211. HOW DO WE USE OUR DEFINED UNITS?
  212. _________________________________________________________________
  213.  
  214. GARDEN.PAS is a very simple program that uses    ================
  215. one of the functions and one of the procedures.     GARDEN.PAS
  216. The only thing you must do is add the names of   ================
  217. the units prior to using the external functions
  218. or procedures.  Lines 16 and 17 each use one of
  219. our newly defined routines.  As you can see, there is nothing magic
  220. about the new routines, and once you include the unit names in a
  221. uses statement, the new routines are in a sense, an extension to
  222. the Pascal language.  Compile and run this program and see that it
  223. really does what you expect it to do.
  224.  
  225.  
  226. ONE MORE EXAMPLE OF UNIT USE
  227. _________________________________________________________________
  228.  
  229. Load and display the program named SHAPES.PAS    ================
  230. for another example of using a predefined unit.     SHAPES.PAS
  231. In line 3, this program includes our new unit    ================
  232. named Areas so all four of the area functions
  233. are available, and in fact, all four are used
  234.  
  235.                                                         Page 13-4
  236.  
  237.                                Chapter 13 - Units in Turbo Pascal
  238.  
  239. within the body of the program.  This program should not be
  240. difficult for you to understand and you will be left to study it
  241. on your own.  The supplied unit named Crt is also included in the
  242. uses clause to allow the use of the keyboard subprograms in lines
  243. 15 and 16.
  244.  
  245.  
  246. MULTIPLE USES OF AN IDENTIFIER
  247. _________________________________________________________________
  248.  
  249. Suppose we wanted to move the variable named My_Pi to the interface
  250. section in both of the units we defined earlier.  Then in the
  251. program named GARDEN.PAS when we included both of the units in the
  252. uses statement, both variables named My_Pi would be available for
  253. use so we would have a bit of a problem defining which one we
  254. really meant to use.  TURBO Pascal has a way to tell the system
  255. which one you wish to use by using a qualifier in much the same way
  256. that you use a field of a record.  The variable name Areas.My_Pi
  257. would refer to that variable from the unit named Areas, and the
  258. name Perims.My_Pi would refer to the variable from the unit named
  259. Perims.
  260.  
  261. You could even define a new variable of the same name in your main
  262. program and refer to it by the qualified name Garden.My_Pi if you
  263. chose to.  This is not recommended as it would get very confusing
  264. to you.  The compiler would be very happy to compile and run such
  265. a program, because it would not get confused.
  266.  
  267. It is not illustrated in the example program, but this technique
  268. applies to procedure and function names as well.  If you used the
  269. same procedure name in two different units, you could specify which
  270. procedure you intend to use by using the dot notation with the unit
  271. name and the procedure name.  Unit_Name.Procedure_Name would
  272. therefore refer to the procedure named Procedure_Name that is a
  273. part of the unit named Unit_Name.
  274.  
  275.  
  276. WHY USE UNITS?
  277. _________________________________________________________________
  278.  
  279. There are basically three reasons to use units in your programming. 
  280. First, some programs are so large that they should be broken up
  281. into smaller chunks for ease of handling and reasonable compilation
  282. size.  In fact some are so large that they cannot be compiled all
  283. at one time since TURBO Pascal has an upper limit of 64K of code
  284. which can be compiled at once.  Most other Pascal compilers have
  285. a similar limit also.
  286.  
  287. Secondly, once you complete the code to perform a certain job, you
  288. may wish to use the same code in another program to do the same
  289. job.  If you put the code in a unit, it is ready to simply call and
  290. use again in the same manner that we reused Areas in SHAPES.PAS. 
  291. This is becoming a rather important topic in software engineering
  292. usually referred to as "Reusable Software".
  293.  
  294.                                                         Page 13-5
  295.  
  296.                                Chapter 13 - Units in Turbo Pascal
  297.  
  298. THIS IS INFORMATION HIDING
  299. _________________________________________________________________
  300.  
  301. Finally, it is sometimes important to hide a portion of code from
  302. the rest of the program to assure that it cannot be unduly modified
  303. by an error somewhere else in the program.  This too is becoming
  304. an important area of software engineering and is usually referred
  305. to as information hiding.
  306.  
  307.  
  308.  
  309. PROGRAMMING EXERCISE
  310. _________________________________________________________________
  311.  
  312. 1.   Move My_Pi to the interface in both units and change one of
  313.      the values slightly to see if you can read in the right one
  314.      at the right time.  Define another variable of the same name
  315.      in your main program and see if you can differentiate between
  316.      all three values.  Note that, due to the nature of this
  317.      exercise, no answer is given for it on the distribution disk.
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.                                                         Page 13-6
  354.