home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog1 / chap02.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  13.0 KB  |  294 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                         Chapter 2
  6.                                                 PROGRAM STRUCTURE
  7.  
  8.  
  9. OUR FIRST ADA PROGRAM
  10. _________________________________________________________________
  11.  
  12. The best way to begin our study of Ada is to      ===============
  13. look at a real Ada program, so by whatever means    TRIVIAL.ADA
  14. you have at your disposal, display the Ada        ===============
  15. program named TRIVIAL.ADA on your monitor.  (You
  16. may wish to study the program from a printed
  17. copy.  The READ.ME file included on the distribution diskette
  18. describes a way to print out all of the example files in this
  19. tutorial with line numbers for reference.)
  20.  
  21. You are looking at the simplest Ada program that it is possible to
  22. write.  Even though it is the simplest, it is a complete executable
  23. program that you will be able to compile and execute after we
  24. discuss a few of the terms and requirements that appear in it.
  25.  
  26. The word procedure in line 1 is the first of the reserved words we
  27. will look at.  Until we reach a much later point in this tutorial,
  28. we will simply say that a procedure is a program.  In this case,
  29. therefore, the program is defined by the reserved word procedure
  30. followed by the program name and another reserved word is.
  31. Following the reserved word is, we have the actual program
  32. extending to line 5.  The reserved word is therefore, is saying
  33. that the program (or procedure) which is named Trivial is defined
  34. as everything that follows.
  35.  
  36. The program name, in this case Trivial, must be selected by
  37. following all of rules given in chapter 1 for naming an identifier.
  38. In addition to those rules, it cannot be a reserved word.
  39.  
  40.  
  41. WHERE IS THE ACTUAL PROGRAM?
  42. _________________________________________________________________
  43.  
  44. There are two portions of any Ada program, a declarative part,
  45. which is contained between the reserved words is and begin, and the
  46. executable part which is contained between the reserved words begin
  47. and end.  In this particular case, there is no declarative part,
  48. and the executable part consists of line 4.  (We will return to
  49. line 4 shortly.)  Following the reserved word end is a repeat of
  50. the program name and a semicolon.  Repeating the program name is
  51. optional but you should get into the habit of including it at the
  52. end of every program.  The semicolon is required to end the
  53. program.
  54.  
  55. The actual program, the executable part, is line 4 since that is
  56. the only statement between the begin and end reserved words.  In
  57.  
  58.                                                          Page 2-1
  59.  
  60.                                     Chapter 2 - Program Structure
  61.  
  62. this case we wanted to study the simplest Ada program possible so
  63. we wanted the program to do nothing.  We explicitly tell the Ada
  64. compiler to do nothing which is the definition of our fifth
  65. reserved word null in line 4.  Ada demands that you explicitly tell
  66. it that you really mean to do nothing rather than simply leaving
  67. the executable part of the program blank which would be acceptable
  68. in most other languages.
  69.  
  70.  
  71. WHAT IS A STATEMENT TERMINATOR?
  72. _________________________________________________________________
  73.  
  74. Line 4 ends with a semicolon which is a statement terminator in
  75. Ada.  It tells the compiler that this particular statement is
  76. complete at this point.  Later in this chapter you will see why the
  77. statement terminator is required.  The semicolon at the end of the
  78. procedure is also a statement terminator since a procedure, and
  79. hence the entire program, is defined as a complete statement in
  80. Ada.  Lines 10 and 12 are Ada comments and will be ignored by the
  81. compiler.  A complete definition of Ada comments will be given at
  82. the end of this chapter.  All example programs in this tutorial
  83. will give you the results of execution at the end of the program
  84. in this manner.
  85.  
  86. It should be clear that a complete Ada program uses at least the
  87. four reserved words to define the beginning and end of each of the
  88. fields and they must come in the given order.  Of course many other
  89. things can be included in the declarative part and the executable
  90. part which we will see during the remainder of this tutorial.
  91.  
  92. The program outline can be given as follows;
  93.  
  94.      procedure <program name> is
  95.         <declarative part>
  96.      begin
  97.         <executable part>
  98.      end <optional repeat of program name>;
  99.  
  100. At this point it would be wise for you to compile and run this
  101. program to see that it truly does obey all the rules of the Ada
  102. compiler.  Unfortunately, it does absolutely nothing, so running
  103. it will give you no results.  Even though this program does
  104. nothing, any good Ada compiler will allow you to compile, link,
  105. load, execute, and properly terminate execution of this program.
  106.  
  107.  
  108. NOW FOR A PROGRAM THAT DOES SOMETHING
  109. _________________________________________________________________
  110.  
  111. Observe the program named SOMEOUT.ADA for an      ===============
  112. example program that really does something.         SOMEOUT.ADA
  113. Ignore the first two lines for the time being,    ===============
  114. they are needed to tell the system how to output
  115. data to the monitor, and we will study them in
  116.  
  117.                                                          Page 2-2
  118.  
  119.                                     Chapter 2 - Program Structure
  120.  
  121. a later lesson.  Considering only lines 4 through 10, you will see
  122. exactly the same structure used in the last program with a
  123. different program name and something new in line 8.
  124.  
  125. Line 8 is a call to a procedure named Put which is supplied with
  126. your Ada compiler as a convenience for you.  It is very precisely
  127. defined so that you can use it to display text on your monitor.
  128. The string of characters contained within the parentheses and
  129. quotation marks will be displayed on your monitor when you compile
  130. and execute this program.  The procedure named Put is actually a
  131. part of an external library named Text_IO which is why the first
  132. two lines are included in this program.  They tell the system where
  133. to find the procedure named Put, and how to use it.  (We will
  134. discuss these two lines in great detail later in this tutorial.)
  135.  
  136. Once again, the executable statement is followed by a semicolon as
  137. the statement terminator in the same manner that the reserved word
  138. null was followed by a semicolon in the last program.
  139.  
  140. Compile and execute this program and observe that the phrase in
  141. line 8 is displayed on the monitor each time you execute the
  142. program.
  143.  
  144.  
  145.  
  146. A LITTLE MORE OUTPUT
  147. _________________________________________________________________
  148.  
  149. Examine the program named MOREOUT.ADA, and you    ===============
  150. will see a few differences from the last two        MOREOUT.ADA
  151. example programs.  Observe that the program name  ===============
  152. is not repeated in the last line of the program.
  153. This is optional as we stated earlier, but it is
  154. a good practice to include the name.
  155.  
  156. The second, and most obvious difference, is the fact that there are
  157. nine executable statements in this program.  The nine statements,
  158. as with most other procedural programming languages, are executed
  159. in sequential order from top to bottom.  The lines with calls to
  160. the procedure Put are executed just like the last program except
  161. a new operation becomes apparent here because we have multiple Put
  162. calls.  The Put call does not cause the cursor to return to the
  163. beginning of a line following output of the line of text.  The
  164. cursor simply stays where it ends up at, resulting in lines 8 and
  165. 9 being output on the same line of the monitor.  Another new
  166. procedure, at least new to us, is named New_Line and this procedure
  167. causes the cursor to be returned to the beginning of the next line
  168. of the monitor.  In fact, you can even include an optional number
  169. within parentheses following the procedure name, and the cursor
  170. will be moved down that number of lines.  This is illustrated in
  171. lines 12 and 14, and if the optional number is omitted a value of
  172. 1 is assumed.
  173.  
  174.  
  175.  
  176.                                                          Page 2-3
  177.  
  178.                                     Chapter 2 - Program Structure
  179.  
  180. Note carefully that the procedure names used here, Put and
  181. New_Line, meet all of the requirements for naming an identifier
  182. which we studied in chapter 1.  These names were selected by the
  183. Ada language definition committee.
  184.  
  185. Lines 16 and 17 introduce another useful procedure, named Put_Line,
  186. which causes an automatic "carriage return" to be output after the
  187. string within the parentheses is output.  You will find this to be
  188. very useful, and should begin using it immediately.  The blank line
  189. in line 15 is ignored by the Ada compiler.  More will be said about
  190. this in the next two example programs.
  191.  
  192.  
  193.  
  194. HOW DID WE NAME THE IDENTIFIERS?
  195. _________________________________________________________________
  196.  
  197. We have the option of naming our program any name we wish as long
  198. as we obey all of the rules of naming an identifier listed in
  199. Chapter 1 of this tutorial.  We also have a restriction on the
  200. program name because of the way Ada compiles and links a program.
  201. In order to meet all of the requirements as specified in the
  202. Language Reference Manual (LRM), a compiler must generate some form
  203. of intermediate files containing object and type information.  Any
  204. particular Ada compiler may use a naming convention for the
  205. intermediate files based on the program name we supply, or the file
  206. name we supply, so in order to avoid confusion, the same name will
  207. be used in both places throughout most of this tutorial.  We
  208. therefore have an arbitrary limit of eight characters which can be
  209. used for the program name if MS-DOS is being used for this
  210. tutorial.  (Note that your particular Ada compiler may not have
  211. this limitation.)
  212.  
  213. With the above information, you should be able to figure out what
  214. this program will do.  Compile and run it to see if you are correct
  215. in your analysis.
  216.  
  217.  
  218.  
  219. CONSIDER THE STYLE OF ADA PROGRAMMING
  220. _________________________________________________________________
  221.  
  222. Observe the program named GOODFORM.ADA and you   ================
  223. will see a familiar form, and a very clear and     GOODFORM.ADA
  224. easy to understand Ada program.  You should have ================
  225. no problem at all understanding what this
  226. program does.  You should observe that Ada
  227. programming is free form, allowing you to add spaces and blank
  228. lines anywhere you wish to make the program clear and easy to
  229. understand, provided of course that you do not split up an
  230. identifier.
  231.  
  232. Once again, don't worry about the first two lines, we will discuss
  233. them later.
  234.  
  235.                                                          Page 2-4
  236.  
  237.                                     Chapter 2 - Program Structure
  238.  
  239.  
  240.  
  241. After compiling and executing GOODFORM.ADA,      ================
  242. observe the next example program named             UGLYFORM.ADA
  243. UGLYFORM.ADA for an example of terrible          ================
  244. formatting style.  See if you can figure out
  245. what this program does then decide if you would
  246. like to debug it if a problem should surface.  Finally, compile and
  247. run this program and you will find that it actually does compile
  248. and execute, doing exactly what the last program did.
  249.  
  250. These two programs were intended to give you an idea of the amount
  251. of freedom you have in formatting style when writing an Ada program
  252. and the amount of information the style can add to a program.
  253.  
  254.  
  255.  
  256. COMMENTS IN AN ADA PROGRAM
  257. _________________________________________________________________
  258.  
  259. Examine the program named COMMENTS.ADA for an    ================
  260. example of how comments are added to an Ada        COMMENTS.ADA
  261. program.  Comments convey no information to the  ================
  262. computer, they only aid the writer and reader of
  263. the program to understand what the original
  264. writer was trying to do within the program.  All comments in Ada
  265. begin with a double minus sign, or double dash if you prefer, and
  266. continue to the end of that line.  No spaces are allowed between
  267. the dashes, they must be adjacent.  There is no provision for
  268. middle-of-line comments in the Ada language, only end-of-line,
  269. although they can be an entire line as in the first two lines in
  270. the example program.
  271.  
  272. Comments can be placed nearly anywhere in an Ada program, including
  273. prior to the program, and following the end of it.  The example
  274. program gives many illustrations of where comments can go and it
  275. will be left to your study.  Note that line 16 is not an executable
  276. statement since it is commented out.  As with all programs in this
  277. tutorial, this one is executable, so you should compile and execute
  278. it at this time.
  279.  
  280. One other point must be made.  This example program is not meant
  281. to be an illustration of good commenting style, only an indication
  282. of where comments can go.  It is actually a very choppy looking
  283. program, and is not at all clear.
  284.  
  285.  
  286. PROGRAMMING EXERCISES
  287. _________________________________________________________________
  288.  
  289. 1.   Write a program to display your name on the monitor.
  290.  
  291. 2.   Write a program to display your name, address, and phone
  292.      number on different lines of the monitor.
  293.  
  294.                                                          Page 2-5