home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / m2t-1.zip / CHAP07.TXT < prev    next >
Text File  |  1989-01-18  |  7KB  |  176 lines

  1.  
  2.                                                      Chapter 7
  3.  
  4.                                   OVERALL PROGRAM CONSTRUCTION
  5.  
  6.  
  7.  
  8. HOW TO PUT IT ALL TOGETHER
  9. ______________________________________________________________
  10.  
  11. We have pretty well covered the topic of how to put all the
  12. parts together to build up a program.  In this chapter we will
  13. go over the whole process in order to clear up any loose ends
  14. and have the entire process in one place.  There is nothing
  15. magic about the way the various pieces fit together but the
  16. rules must be followed in order to build a usable program.
  17.  
  18. Examine the program named OVERPROG.MOD for    ================
  19. our first look at the overall structure of      OVERPROG.MOD
  20. the Modula-2 program.  It would be well       ================
  21. for you to keep in mind that there is a
  22. major category that we have not even hinted at yet, the issue
  23. of modules.  They will be covered in Part III of this
  24. tutorial, and although they are very important, you can begin
  25. writing meaningful programs before you even hear what modules
  26. are or how they are used.
  27.  
  28.  
  29. NESTED PROCEDURES
  30. ______________________________________________________________
  31.  
  32. The program on display contains several levels of nested
  33. procedures as an illustration for you.  The main program has
  34. lines 3 through 37 as its declaration part, and lines 38
  35. through 43 as its statement part.  Since the procedure
  36. definitions actually define the procedures called for by the
  37. main program, they correctly belong in the declaration part
  38. of the program.  Only two of the procedures can actually be
  39. called by the main program, Proc1, and Proc2, because they are
  40. the only procedures at the same level as the main program.
  41. The procedure Proc1 is a simple procedure, but Proc2 has
  42. additional procedures in its declaration part.
  43.  
  44. Procedure Proc2 contains a declaration part in lines 13
  45. through 30, and a statement part in lines 31 through 36.  Its
  46. declaration part contains two procedures, Proc3 and Proc4.
  47. The nesting is carried one step farther in Proc4 which
  48. contains the procedure Proc5 in its declaration part.
  49. Procedures can be nested to whatever level desired according
  50. to the definition of Modula-2.
  51.  
  52.  
  53. WHO CAN CALL WHO?
  54. ______________________________________________________________
  55.  
  56. It is important for you to clearly understand which procedure
  57. can call which other procedures.  A procedure can call any
  58.  
  59.                                                            7-1
  60.  
  61.                       Chapter 7 - Overall Program Construction
  62.  
  63. procedure on the same level as itself provided that both have
  64. the same parentage, or any procedure that is included in its
  65. own declaration part at the level of its own declaration part.
  66. For example, the main program can only call Proc1, and Proc2.
  67. The others are nested within Proc2 and are not available to
  68. the main program.  Likewise the statement part of Proc2 can
  69. call Proc1, because it is on the same level, and Proc3 and
  70. Proc4, because they are within its declaration part and at the
  71. same level as its declaration part.  The procedure Proc5 can
  72. only be called by Proc4, because no other procedure is at its
  73. level.  Note that if another triple nesting were included in
  74. Proc1, its third level procedure could not be called by Proc5
  75. because they would not have the same parentage.
  76.  
  77. There is one additional rule about procedure calling.  A
  78. procedure can call any procedure that is a peer of any of its
  79. ancestors.  This means that the executable part of Proc5 can
  80. call Proc3 or Proc1 because they are peers of Proc4 and Proc2
  81. respectively, which are both ancestors of Proc5.
  82.  
  83. Nested procedures can be very useful when you wish to use a
  84. procedure that you don't want any other part of the program
  85. to be able to access or even see.  A private procedure can
  86. therefore be written with no concern that the name may clash
  87. with some other part of the program and cause undesirable side
  88. effects.
  89.  
  90. The important thing to gain from this program is that nesting
  91. is possible and can be very useful, and the definition of a
  92. procedure is the same as that of the main program.  This means
  93. that procedures can be nested within procedures in any way
  94. that helps in designing a clear, understandable program.  Note
  95. that function procedures can also be nested in this manner if
  96. desired.  Compile and run this program and study the output
  97. from it until you understand it completely.
  98.  
  99.  
  100. WHERE DO CONSTANTS, TYPES, AND VARIABLES GO?
  101. ______________________________________________________________
  102.  
  103. Examine MOREPROG.MOD, for examples of         ================
  104. where you can put the other definitions in      MOREPROG.MOD
  105. the declaration part of the program and       ================
  106. the procedures.  This is a repeat of the
  107. last program with CONST, TYPE, and VAR declarations added in
  108. every place where it is legal to put them.  This is done as
  109. an example to you of where they can be put, so no explanation
  110. of details will be given.  Notice lines 45 through 48 where
  111. the use of a global type and other types defined in ancestor
  112. procedures are illustrated.  Study these lines until you
  113. understand where the types are coming from.  Some time spent
  114. studying this program should aid you in understanding even
  115. better the overall program construction problem.
  116.  
  117.                                                            7-2
  118.  
  119.                       Chapter 7 - Overall Program Construction
  120.  
  121. WHAT ABOUT ORDER OF DECLARATIONS?
  122. ______________________________________________________________
  123.  
  124. Load the program LASTPROG.MOD for an          ================
  125. example of how the various fields can be        LASTPROG.MOD
  126. ordered in the declaration part of the        ================
  127. program.  Notice that there are 2
  128. procedures, two constants, two types, and two variables
  129. defined, but they are defined in a seemingly random order.
  130. The order is random and was done only to illustrate to you
  131. that the order doesn't matter in Modula-2, as long as
  132. everything is defined before it is used.
  133.  
  134. In only one case does the order matter.  The compiler is very
  135. picky about where the import list goes because the Modula-2
  136. language definition requires it to be first.  In addition, the
  137. export list, if it exists, must immediately follow the import
  138. list.  We will cover both of these in detail later, for now
  139. simply remember that the order of all declarations can come
  140. in random order as long as they follow the import/export lists
  141. and come before the statement part of the program.
  142.  
  143.  
  144. PROGRAMMING EXERCISES
  145. ______________________________________________________________
  146.  
  147. 1.   Using the program OVERPROG, add some calls to illegal
  148.      places to see what messages the compiler displays.
  149.  
  150. 2.   Using the program MOREPROG, add some illegal variable
  151.      references to see what messages the compiler displays.
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.                                                            7-3
  175.  
  176.