home *** CD-ROM | disk | FTP | other *** search
/ Solo Programadores 22 / SOLO_22.iso / docs / lovelace / lesson4.les < prev    next >
Encoding:
Text File  |  1995-11-21  |  9.7 KB  |  282 lines

  1. <COMMENT This is a lesson file for the Lovelace Ada tutorial>
  2. <COMMENT A program called genlesson is used to transform this file into a set>
  3. <COMMENT of useful HTML files for use by Mosaic & other WWW browsers.>
  4.  
  5. <TUTOR NAME="Lovelace">
  6. <LESSON NUMBER=4>
  7. <AUTHOR NAME="David A. Wheeler" EMAIL="wheeler@ida.org">
  8. <AUTHOR ADDRESS="<A HREF="dwheeler.htm">David A. Wheeler (wheeler@ida.org)</A>">
  9.  
  10. <COMMENT $Id: lesson4.les,v 1.5 1995/08/09 21:07:40 wheeler Exp $ >
  11.  
  12. <COMMENT A lesson is divided into 1 or more "sections".>
  13.  
  14. <SECTION NAME="Type Integer">
  15. The next few sections will describe how to create subprograms, but we really
  16. can't talk about creating subprograms until we know a little about
  17. the Ada type <EM>Integer</EM>.
  18. <P>
  19. The Ada type Integer is used to store integer values if you don't care
  20. what its minimum and maximum range is -- Ada will then select whatever range is
  21. ``most natural'' for your machine.
  22. Ada guarantees that an Integer can store numbers between
  23. -32767 and 32767 (inclusive); an Integer is likely to have a wider range.
  24. In other words, an Integer type must use at least 16 bits, but the actual
  25. number of bits used will depend on the compiler and machine.
  26. <P>
  27. If you <EM>do</EM> care what the range of a value is, Integer isn't the
  28. right type to use.
  29. In fact, as we'll discover later, Ada has a rich collection of types and typing
  30. mechanisms to specify what you <EM>do</EM> want.
  31. Many real Ada programs don't use Integer very much - but what they
  32. <EM>do</EM> use will have to wait for a later lesson.
  33. <P>
  34. Normal Integer operations are available: + means add, - means subtract,
  35. * means multiply, / means (integer) division, and ** means exponentiate.
  36. The normal mathematical rules of evaluation are used, so exponentiation is
  37. done first, then multiplications and division, then addition and subtraction.
  38. Parentheses can be used to change the order or to make it clearer.
  39. Thus ``2+3*5'' is 17, and ``(2+3)*5'' is 25.
  40. <P>
  41. A <EM>key</EM> difference between Ada and some other languages (such as
  42. C and C++) is what happens when an evaluation cannot be completed.
  43. If a division by zero is attempted, or an expression result is too large,
  44. Ada will normally <EM>raise</EM> an <EM>exception</EM>.
  45. Exceptions can be handled, but if they aren't, the
  46. program will halt (with some debugging output
  47. to help identify the kind and location of the problem).
  48. This means that instead of silently giving wrong answers, Ada programs
  49. normally will halt when a computation cannot be completed.
  50. This simplifies debugging.
  51. <P>
  52. Normal Integer comparisons (which return true or false) are also available:
  53. = means ``is equal to'', > means ``greater than'',
  54. >= means ``greater than or equal to'', and so on.
  55. The ``not equal to'' operation is written as ``/='' (which looks
  56. like the mathematical symbol for `not equal').
  57. Comparisons are considered after arithmetic operations,
  58. so ``3 + 4 > 6'' is evaluated as ``7 > 6'' (True).
  59. <P>
  60. Unlike C or C++, but like Pascal and many other languages, Integers are
  61. <EM>not</EM> considered the same as True or False.
  62. A zero and False aren't the same thing (in Ada terms they are different types).
  63. If you want to determine if a number is zero, compare it (using =) to zero.
  64. This helps in catching errors early.
  65.  
  66. <QUESTION Type=Multiple-Choice>
  67. Which of the following expressions is true?
  68. <CHOICES>
  69. <CHOICE ANS=1>(2+3)*4 = 2+(3*4)
  70. <CHOICE ANS=2>6/3 > 12-2
  71. <CHOICE ANS=3>2+8 /= 28
  72. </CHOICES>
  73. <ANSWER ANS=3>
  74. <RESPONSES>
  75. <WHEN ANS=1>
  76. No, sorry.
  77. <P>
  78. (2+3)*4 evaluates to (5)*4, which is 20.
  79. <P>
  80. However, 2+(3*4) evaluates to 2+(12), which is 14.
  81. <P>
  82. They aren't equal.
  83. <WHEN ANS=2>
  84. Text for response 2.
  85. No, sorry.
  86. <P>
  87. 6/3 evaluates to 2.
  88. <P>
  89. 12-2 evaluates to 10.
  90. <P>
  91. Is 2 > 10? I'm afraid not.
  92. <WHEN ANS=3>
  93. Very good!
  94. </RESPONSES>
  95.  
  96. <SECTION NAME="Subprogram Declarations and Parameters">
  97. Let's see how to declare a subprogram (procedure or function) declaration.
  98. The main difference between a procedure and function is that a function
  99. returns a value, while a procedure does not (though a procedure
  100. can change the values of parameters sent to it).
  101. Here's an example of a procedure declaration for a procedure named Average,
  102. which takes as input two values and changes a third variable (presumably
  103. to hold the average):
  104. <P>
  105. <PRE>
  106. procedure Average(A, B : in Integer; Result : out Integer);
  107. </PRE>
  108. <P>
  109. Actually, a subprogram that averages two numbers would probably be defined
  110. as a function. Here's a declaration of a function which takes as input
  111. two values and returns a result:
  112. <P>
  113. <PRE>
  114. function Average_Two(A, B : in Integer) return Integer;
  115. </PRE>
  116. <P>
  117. Note the keywords `in' and `out';
  118. this indicates the <EM>mode</EM> of the parameter.
  119. There are three possible modes:
  120. <OL>
  121. <LI>
  122. `in' - the parameter's value may be used but not changed.
  123. <LI>
  124. `out' - the parameter's value may be changed but not used.
  125. <LI>
  126. `in out' - the parameter's value may be used and/or changed.
  127. </OL>
  128. <P>
  129. The default mode is `in', but I recommend that you always state
  130. the desired mode.
  131. <P>
  132. Here's a <A HREF="bnf.htm">BNF</A> for subprogram declarations:
  133. <P>
  134. <PRE>
  135. subprogram_declaration ::= subprogram_specification ";"
  136.  
  137. subprogram_specification ::= "procedure" procedure_name parameter_profile | 
  138.                  "function" procedure_name parameter_profile "return" type
  139.  
  140. parameter_profile ::= [ "(" parameter_specification
  141.                             { ";" parameter_specification} ")" ]
  142.  
  143. parameter_specification ::= parameter_name_list ":" mode type
  144.                             [ ":=" default_expression ]
  145.  
  146. mode ::= [ "in" ] | "out" | "in" "out"
  147.  
  148. parameter_name_list ::= identifier { "," identifier }
  149.  
  150. procedure_name ::= identifier
  151. </PRE>
  152.  
  153. <QUESTION Type=Multiple-Choice>
  154. Which of the following is not a legal subprogram declaration?
  155. <CHOICES>
  156. <CHOICE ANS=1>procedure Delete_File( in Integer : A );
  157. <CHOICE ANS=2>procedure Initialize;
  158. <CHOICE ANS=3>function Middle_Value( A, B, C : in Integer ) return Integer;
  159. </CHOICES>
  160. <ANSWER ANS=1>
  161. <RESPONSES>
  162. <WHEN ANS=1>
  163. Right. The parameter name is given first, then a colon,
  164. then the mode ("in", "out", "in out"), and
  165. then the name of the type.
  166. <P>
  167. A note for the picky - "Integer" isn't a reserved keyword in Ada, it's
  168. just the name of a predefined type.
  169. Thus, if there were a type named "A", then the
  170. following slightly-different declaration would be okay:
  171. <P>
  172. <PRE>
  173. procedure Delete_File( Integer : in A );
  174. </PRE>
  175. <P>
  176. This declaration creates a procedure "Delete_File" that takes
  177. an input variable named "Integer"; variable "Integer" is of type "A".
  178. An Ada compiler can handle that kind of bizarre declaration, but
  179. I <EM>strongly</EM> recommend that you do <EM>not</EM>
  180. use the names of predefined types as variable names.
  181. Using predefined type names as variable names
  182. is really confusing to humans trying to read the program.
  183. <WHEN ANS=2>
  184. No, sorry, that's a perfectly legitimate procedure declaration.
  185. If a procedure has no parameters, the parentheses aren't used.
  186. In the BNF, note that the `parameter_profile' is optional (surrounded by []).
  187. <WHEN ANS=3>
  188. No, sorry.
  189. That's a perfectly acceptable declaration of a function.
  190. </RESPONSES>
  191.  
  192. <SECTION NAME="Subprogram Bodies and Local Variables">
  193. A subprogram body defines the actual algorithm used by the subprogram.
  194. A subprogram body starts out with a subprogram specification (which is the
  195. subprogram declaration without the final semicolon) followed by the keyword "is".
  196. This is followed by a declaration of local variables, the keyword "begin", the
  197. statements to be executed, and then the keyword "end".
  198. <P>
  199. Here's a simple subprogram body that implements the procedure Average we
  200. declared in the last section. Note that after the word `end' we can add
  201. a word indicating what we're ending (the Ada compiler will check to make
  202. sure this is correct). Also note that the assignment statement in Ada
  203. is written as `:=' (the same as Pascal):
  204. <P>
  205. <PRE>
  206. procedure Average(A, B : in Integer; Result : out Integer) is
  207. begin
  208.  Result := (A + B) / 2;
  209. end Average;
  210. </PRE>
  211. <P>
  212. Local variables and local subprograms can be declared between the "is" and the
  213. "begin".
  214. Local variables are written the same as parameters are: the variable name(s),
  215. a colon, and their type.
  216. They can be given initial values (the following example initializes its local
  217. variable `Total' to the value of A).
  218. Functions return a value using the `return' statement.
  219. Here's an example:
  220. <P>
  221. <PRE>
  222. function Sum(A, B : in Integer) return Integer is
  223.  Total : Integer := A;
  224. begin
  225.  Total := Total + B;
  226.  return Total;
  227. end Sum;
  228. </PRE>
  229. <P>
  230. Here's an example with a function that computes the sum of the squares
  231. of two Integers. It works by creating a local function called Square:
  232. <P>
  233. <PRE>
  234. function Sum_Squares(A, B : in Integer) return Integer is
  235.   function Square(X : in Integer) return Integer is
  236.   begin -- begin Square
  237.     return X*X;
  238.   end Square;
  239. begin -- begin Sum_Squares
  240.  return Square(A) + Square(B);
  241. end Sum_Squares;
  242. </PRE>
  243. <P>
  244. Here's a <A HREF="bnf.htm">BNF</A> for subprogram declarations:
  245. </P>
  246. <PRE>
  247. subprogram_body ::= subprogram_specification "is"
  248.                       declarative_part
  249.                     "begin"
  250.                       sequence_of_statements
  251.                     "end" [designator] ";"
  252.  
  253. declarative_part ::= { declarative_item }
  254.  
  255. declarative_item ::= object_declaration | subprogram_body
  256.  
  257. object_declaration ::= identifier_list : [constant] type [":=" expression] ";"
  258. </PRE>
  259. <P>
  260. A brief note about statement sequences: like C, Ada uses semicolons as
  261. a statement <EM>terminator</EM> - each Ada statement ends in a semicolon.
  262. This is different than Pascal,
  263. which uses the semicolon as a statement <EM>separator</EM>.
  264.  
  265. <QUESTION Type=Multiple-Choice>
  266. Which of the examples in this section has an empty declarative_part
  267. (i.e. no local variables or subprograms)?
  268. <CHOICES>
  269. <CHOICE ANS=1>procedure Average
  270. <CHOICE ANS=2>function Sum
  271. <CHOICE ANS=3>function Sum_Squares
  272. </CHOICES>
  273. <ANSWER ANS=1>
  274. <RESPONSES>
  275. <WHEN ANS=1>
  276. Right!
  277. <WHEN ANS=2>
  278. No, sorry.
  279. <WHEN ANS=3>
  280. No, sorry.
  281. </RESPONSES>
  282.