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

  1.  
  2.  
  3.  
  4.                                                         Chapter 9
  5.                                     BLOCKS AND SCOPE OF VARIABLES
  6.  
  7.  
  8. LARGE PROJECT DECOMPOSITION
  9. _________________________________________________________________
  10.  
  11. Since Ada is a highly structured language, it has the means to
  12. divide a large project into many smaller projects through use of
  13. procedures and functions.  Because procedures and functions can be
  14. nested within other procedures and functions, we have the problem
  15. of visibility and scope of types, variables, constants, and
  16. subprograms.
  17.  
  18.  
  19. WHAT IS THE SCOPE OF A VARIABLE?
  20. _________________________________________________________________
  21.  
  22. Examine the program named SCOPE.ADA for several   ===============
  23. examples of variables with different scopes.         SCOPE.ADA
  24. You should spend a few minutes familiarizing      ===============
  25. yourself with the structure of the program which
  26. contains the main program, or procedure, and
  27. four procedures embedded within it.  We will begin with the
  28. variable named Count declared in line 4, and state that it has a
  29. scope which extends from the semicolon at the end of its
  30. declaration to the end of the entire program in line 32.  Its scope
  31. extends to the end of the program because it is declared in the
  32. declaration part of the main program.  It is commonly referred to
  33. as a global variable.
  34.  
  35.  
  36.  
  37. WHERE IS A VARIABLE VISIBLE?
  38. _________________________________________________________________
  39.  
  40. The variable named Count, declared in line 4, is visible anyplace
  41. in the range of its scope, except for one small area of the
  42. program.  Since another variable with the same name is defined in
  43. line 10, the first one is effectively hidden from view within the
  44. range of the newer, local variable.  Note that it is the local
  45. variable that takes precedence and hides the global variable,
  46. rather than the other way.  The variable named Count from line 4,
  47. is not visible from the end of line 10 through the end of line 13.
  48. It should be clear that the scope of the local variable extends to
  49. the end of the executable portion of the subprogram in which it is
  50. declared.
  51.  
  52. In like manner, the variable named Index, defined in line 7, has
  53. a scope that extends from the end of line 7 to the end of its
  54. procedure which ends in line 23.  The variable named Index is
  55. visible throughout its range, because there are no other variables
  56. of the same name in a lower level subprogram.  The variable named
  57.  
  58.                                                          Page 9-1
  59.  
  60.                         Chapter 9 - Blocks and Scope of Variables
  61.  
  62. Data is visible throughout its range which extends from the end of
  63. line 16 through 19.
  64.  
  65.  
  66. THAT WAS ACTUALLY A LIE
  67. _________________________________________________________________
  68.  
  69. The global variable Count is not visible from lines 10 through 13,
  70. but there is a way to use it in spite of its hidden nature.  This
  71. will be the topic of the next example program, but you should
  72. compile and run the present program to see that it really will
  73. compile as given.  There is no output, so execution will be
  74. uninteresting.
  75.  
  76.  
  77. USING THE DOT NOTATION
  78. _________________________________________________________________
  79.  
  80. Examine the program named SCOPE2.ADA for some    ================
  81. examples of making an invisible variable            SCOPE2.ADA
  82. visible.  The careful observer will notice that  ================
  83. this is the structure of the last program with
  84. additional variables declared, and some added
  85. assignment statements.
  86.  
  87. We will consider three variables of the same name, Count, and see
  88. that we can use all three variables in a single statement if we so
  89. desire.  Assume we are at line 12 in the program where we wish to
  90. use the local variable named Count, the one that was declared in
  91. line 10.  By the definition of Ada, the innermost variable will
  92. take precedence and by simply using the name Count, we are using
  93. the desired one.  If however, we would like to use the one declared
  94. in line 4, we can do so by using the "dot" notation illustrated in
  95. line 13.  We are giving the compiler a complete map on where to
  96. find the variable.  The dot notation can be read as follows, "Go
  97. to the outer level, Scope2, dot, and the variable named Count."
  98. Line 13 is therefore referring to the variable declared in line 4.
  99. Using the notation Scope2.Level1.Count would refer to the variable
  100. declared in line 7.
  101.  
  102. Additional examples of the use of dot notation to use otherwise
  103. invisible variables are given in lines 21 through 30.  This is also
  104. called the expanded name of the variable or the expanded naming
  105. convention.
  106.  
  107.  
  108. RENAMING A VARIABLE
  109. _________________________________________________________________
  110.  
  111. In order to reduce the number of keystrokes used and to improve the
  112. clarity of some programs, Ada provides a renaming capability.  Line
  113. 18 illustrates this by renaming the triple component combination
  114. to the much simpler name, Outer_Index.  Anyplace in the program
  115. where it is permissible to use the longer name, it is also legal
  116.  
  117.                                                          Page 9-2
  118.  
  119.                         Chapter 9 - Blocks and Scope of Variables
  120.  
  121. to use the new shorter name, because they are simply synonyms for
  122. the same actual variable.  This is a construct that could easily
  123. be abused in a program and make a program unnecessarily
  124. complicated, so it should be used sparingly.
  125.  
  126. Compile and run this program, even though it has no output, to
  127. assure yourself that it actually will compile.  The dot notation
  128. will be used in many other places in Ada, so you should become
  129. familiar with it.
  130.  
  131.  
  132.  
  133. AN ADA BLOCK
  134. _________________________________________________________________
  135.  
  136. Examine the program named BLOCKS.ADA for an      ================
  137. example of the use of an Ada block.  Just as you    BLOCKS.ADA
  138. can define a procedure and jump to it, by        ================
  139. calling it of course, Ada allows you to define
  140. the equivalent of a procedure and execute it as
  141. inline code.  Such a section of code is called a block and has the
  142. form of three reserved words, declare, begin, and end, with
  143. declarations between the declare and begin, and executable
  144. statements between the begin and end.  Any new types, subtypes,
  145. variables, constants, and even subprograms can be declared in the
  146. declaration part of the block and used in the executable part.  The
  147. scope of the declarations begin where they are declared, and end
  148. at the end of the block.
  149.  
  150.  
  151.  
  152. A BLOCK IS A SINGLE STATEMENT
  153. _________________________________________________________________
  154.  
  155. A block is a single statement and because it is, it can be put
  156. anywhere that it is legal to put any other executable statement.
  157. It could be used within a loop, in one branch of an if statement,
  158. or even as one of the cases of a case statement.  The example
  159. program contains two such blocks, the first in lines 20 through 30,
  160. and the second in lines 37 through 50.  The only real difference
  161. is that the second block is a named block, with the name Who, the
  162. use of which will be defined shortly.
  163.  
  164. Study the program and you will see that even though there are
  165. several variables defined in the block, and at least one is a
  166. repeat of a global variable, all are actually available through use
  167. of the dot notation defined during our study of the last program.
  168. In the first block, the local variables are the default variables
  169. when there is a repeated name, but in the second block, the
  170. variables can be specifically named by using the dot notation.
  171. This is possible because the block is named, the name being Who in
  172. this particular case.  The name is mentioned just prior to the
  173. block followed by a colon, and the name is repeated following the
  174. end of the block.  In this case, the name does nothing for you, but
  175.  
  176.                                                          Page 9-3
  177.  
  178.                         Chapter 9 - Blocks and Scope of Variables
  179.  
  180. if there were two nested blocks, either or both could be named, and
  181. you would be able to select which variable you were interested in.
  182. There is no limit to the number of blocks that can be nested.
  183.  
  184. Note that the name used for a block is not a label to which you can
  185. jump to in order to execute a goto statement.  The name is only to
  186. name the block.
  187.  
  188. If no declarations are needed, you can declare a block without the
  189. reserved word declare, using only the execution block delimiters
  190. begin and end.  Without the declaration part, there is little
  191. reason to declare the block until we come to the topic of exception
  192. handling where it will be extremely useful to have this capability.
  193. Compile and execute this program and study the results.  Be sure
  194. you understand where each of the displayed values come from.
  195.  
  196.  
  197. WHAT ARE AUTOMATIC VARIABLES?
  198. _________________________________________________________________
  199.  
  200. This is a good time to discuss a very important  ================
  201. topic that can have a significant effect on how    AUTOMATC.ADA
  202. you write some of your programs in the future.   ================
  203. The topic is automatic variables, what they are
  204. and what they do for you.  The best way to
  205. define them is to examine another program, and the program named
  206. AUTOMATC.ADA is written just to illustrate this point.
  207.  
  208. The program is actually very simple since it is merely one big loop
  209. in which the variable Index covers the range from 1 through 10.
  210. Each time through the loop, the block in lines 19 through 29 is
  211. executed and contains another loop to output some integer type
  212. data.  Take careful notice of the constants and the way they are
  213. used, and you will see something that is a little strange.  Each
  214. time we enter the block, we enter with a larger value for the loop
  215. variable, in this case named Index, and therefore the constants are
  216. different each time through the block.  During each pass, however,
  217. they are constant, and will be treated as such.  This behavior is
  218. perfectly normal and will be clearly understood when we define an
  219. automatic variable.
  220.  
  221. Prior to entering the block, the two constants, and the variable
  222. named Count_Stuff do not exist.  When the block is entered, the
  223. constants are generated by the system, initialized to their
  224. constant values, and available for use within the block.  Since the
  225. constants are generated each time the block is entered, it is
  226. possible to assign them a different value each time, which is
  227. exactly what is being done here.  The process of assigning the
  228. constants their values is called elaboration.
  229.  
  230. The variable is also generated, and made available for use within
  231. the block where it is assigned a nonsense value for illustrative
  232. purposes, then never used.  When program control leaves the block,
  233. in this case dropping out the bottom, the two constants and the
  234.  
  235.                                                          Page 9-4
  236.  
  237.                         Chapter 9 - Blocks and Scope of Variables
  238.  
  239. variable disappear from existence entirely and are no longer
  240. available for use anywhere in the program.  They are said to have
  241. a limited lifetime, their lifetime being from the time they are
  242. elaborated until we leave the block in which they are declared.
  243. The scope and lifetime of a variable or constant is therefore very
  244. important for you to understand.
  245.  
  246. It should be clear to you that the outer loop variable, Index, is
  247. visible from lines 15 through 32 except for lines 26 through 28.
  248. Within the region of lines 26 through 28, the outer loop variable
  249. cannot be used, even by using the expanded naming convention (i.e.
  250. - the dot notation), because there is no name for the outer loop.
  251. Naming the outer loop would make the outer loop variable available.
  252.  
  253.  
  254.  
  255. WHERE ELSE IS THIS USED?
  256. _________________________________________________________________
  257.  
  258. This concept of the automatic variable is very important because
  259. it is used in so many places throughout an Ada program.  It is used
  260. in four different places in the present example program, once in
  261. the block, as we have just mentioned, once in the main program
  262. itself, where the four variables with animal names are
  263. automatically generated, and twice in the for loops.  The variables
  264. named Index in each of the for loops are actually automatic
  265. variables that are generated when the loop is entered, and
  266. discarded when the loop is completed.  As you can see, there is a
  267. very good reason why the loop control variable is not available
  268. after you leave the loop.
  269.  
  270. Each time you call a procedure or function, the formal parameters
  271. are generated, as are the defined variables and constants.  The
  272. process of variable generation and constant initialization is
  273. called elaboration.  They are then used within the subprograms, and
  274. discarded when the procedure or function is completed and control
  275. is returned to the calling program.
  276.  
  277. Since the main program is itself a procedure, its variables are
  278. handled the same way.
  279.  
  280.  
  281.  
  282. THE STACK STORES THE AUTOMATIC ENTITIES
  283. _________________________________________________________________
  284.  
  285. The generated constants and variables are stored on an internal
  286. stack, the definition of which is beyond the scope of this
  287. tutorial.  If you understand what a stack is, it should be clear
  288. to you how the system can generate items, place them on the stack,
  289. use them, and discard them.  Also, due to the nature of a stack,
  290. it should be clear to you how additional variables can be placed
  291. on the stack as calls are made to more deeply nested procedures and
  292. functions.  Finally, it is only because of this use of automatic
  293.  
  294.                                                          Page 9-5
  295.  
  296.                         Chapter 9 - Blocks and Scope of Variables
  297.  
  298. variables that recursive subprograms can be used.  Ada requires
  299. that all subprograms be re-entrant and use of the stack also makes
  300. this possible.
  301.  
  302. If the last paragraph is too technical for you, don't worry about
  303. it, because it is only mentioned for general information, and is
  304. not needed to understand Ada programming.
  305.  
  306. Compile and run AUTOMATC.ADA and study the output.  Be sure you
  307. understand the concept of the automatic variable because some of
  308. the advanced programming techniques in Ada will require this
  309. knowledge.
  310.  
  311.  
  312.  
  313. PROGRAMMING EXERCISES
  314. _________________________________________________________________
  315.  
  316. 1.   Modify BLOCKS.ADA to include a procedure in the declaration
  317.      part of the first block.  The procedure should output a line
  318.      of text to the monitor when called from the executable part
  319.      of the block.  Why can't this procedure be called from outside
  320.      of the block?
  321.  
  322. 2.   Name the block in AUTOMATC.ADA and output the value of the
  323.      outer Index in the loop using the dot notation.  It will
  324.      output the same number six times since the outer Index is
  325.      invariant in the inner loop.
  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.                                                          Page 9-6