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

  1.  
  2.  
  3.  
  4.                                                         Chapter 5
  5.                                          PROCEDURES AND FUNCTIONS
  6.  
  7. A PASCAL PROGRAM OUTLINE
  8. _________________________________________________________________
  9.  
  10. In order to properly define procedures and functions we need to lay
  11. some groundwork in the form of a few definitions.  These are
  12. important concepts, so pay close attention.  Every Pascal program
  13. is composed of three fundamental parts which can be defined as
  14. follows.
  15.  
  16. Program Heading - This is the easiest part since it is only one
  17.      line, at least it has been in all of our programs up to this
  18.      point.  It is simply the program line, and it never needs to
  19.      be any more involved or complicated than it has been up to
  20.      this point in TURBO Pascal.  You may remember that we said it
  21.      is not even necessary in TURBO Pascal.
  22.  
  23. Declaration Part - This is the part of the Pascal source code in
  24.      which all constants, variables, and user defined auxiliary
  25.      operations are defined.  Some of the programs we have examined
  26.      have had one or more var declarations.  These are the only
  27.      components of the declaration part we have used to this point. 
  28.      There are actually five components in the declaration part,
  29.      and procedures and functions which are the topics of this
  30.      chapter, are the fifth part.  We will cover the other
  31.      components in the next chapter.
  32.  
  33. Statement Part - This is the last part of any Pascal program, and
  34.      it is what we have been calling the main program.  It is one
  35.      compound statement bracketed with the reserved words begin and
  36.      end.  Note that neither of these two words are optional since
  37.      both are required to have a Pascal program.
  38.  
  39. It is very important that you grasp the above definitions because
  40. we will be referring to them constantly during this chapter and
  41. throughout the remainder of this tutorial.  With that introduction,
  42. let's go on to our first Pascal program with a procedure in it, in
  43. fact it will have three procedures.
  44.  
  45.  
  46. THE FIRST PROCEDURES
  47. _________________________________________________________________
  48.  
  49. Examine PROCED1.PAS as your first example         ===============
  50. program with a procedure and display it on your     PROCED1.PAS
  51. monitor.  You will notice that it doesn't look    ===============
  52. like anything you have seen up to this point
  53. because it has procedures in it.  Let's go back
  54. to our definitions from above.  The first line is the Program
  55. Heading which should pose no difficulty.  The Declaration Part
  56. begins with the var statement in line 4 and continues down through
  57.  
  58.                                                          Page 5-1
  59.  
  60.                              Chapter 5 - Procedures and Functions
  61.  
  62. and including all three procedures ending in line 19.  Lines 21
  63. through 26 constitute the Statement Part.  It may seem strange that
  64. what appears to be executable Pascal statements, and indeed they
  65. are executable statements, are contained in the Declaration Part
  66. rather than the Statement Part.  This is because of the Pascal
  67. definition and it will make sense when we have completed our study
  68. of procedures and functions.
  69.  
  70. Continuing to examine PROCED1.PAS, we will make note of the program
  71. itself, which is the Statement Part.  The program, due to the
  72. nature of Pascal and the carefully chosen procedure names, clearly
  73. tells us what it will do.  It will write a header, eight messages,
  74. and an ending.  The only problem we are faced with is, how will it
  75. write these messages?  This is where the Declaration Part is called
  76. upon to define these operations in detail.  The Declaration Part
  77. contains the three procedures which will completely define what is
  78. to be done by the procedure calls in the main program.
  79.  
  80.  
  81. DEFINITIONS GO IN THE DEFINITION PART
  82. _________________________________________________________________
  83.  
  84. It should be clear to you that the definitions of the procedures
  85. should be in the Definition Part of the program because that is
  86. exactly what they do.  In the case of a var, a variable is defined
  87. for later use by the main program, and in the case of a procedure,
  88. the procedure itself is defined for later use by the main program. 
  89.  
  90. Let's arbitrarily pick one of the procedures, the first, and
  91. examine it in detail.  The first executable statement we come to
  92. in the main program is line 22 which says simply, Write_A_Header,
  93. followed by the usual statement separator, the semicolon.  This is
  94. a simple procedure call.  When the compiler finds this statement,
  95. it goes looking for a predefined procedure of that name which it
  96. can execute.  If it finds one in the Declaration Part of the
  97. program, it will execute that procedure.  If it doesn't find a user
  98. defined procedure, it will search the Pascal library for a system
  99. defined procedure and execute it.  The Write and Writeln statements
  100. are system procedures, and you have already been using them quite
  101. a bit, so procedures are not completely new to you.  If the
  102. compiler doesn't find the procedure defined in either place, it
  103. will generate an error message.  Depending on which version of
  104. TURBO Pascal you are using, the system may search several libraries
  105. to find the procedures called by the program.  Much more will be
  106. said about this later in this tutorial.
  107.  
  108.  
  109. HOW TO DEFINE & CALL A PROCEDURE
  110. _________________________________________________________________
  111.  
  112. To call a procedure, we simply need to state its name.  To define
  113. a simple procedure, we use the reserved word procedure followed by
  114. its calling name, with a semicolon as a terminator.  Following the
  115. Procedure Heading, there is the Declaration Part of the procedure
  116.  
  117.                                                          Page 5-2
  118.  
  119.                              Chapter 5 - Procedures and Functions
  120.  
  121. followed by a body which is nothing more than a compound statement
  122. bracketed by the reserved words begin and end.  This is identical
  123. to the Statement Part of the main program except that the procedure
  124. ends with a semicolon instead of a period.  Any valid Pascal
  125. statements can be put between the begin and end, and in fact, there
  126. is no difference in what can be put in a procedure and what can be
  127. put in the main program.
  128.  
  129. The program we are examining would be no different if we would
  130. eliminate the first procedure completely and move the Writeln
  131. contained in it down to the Statement Part in place of
  132. Write_A_Header.  If that statement is not clear, go back and reread
  133. the last two paragraphs until it is.
  134.  
  135. Lines 23 and 24 will cause the procedure named Write_A_Message to
  136. be called 8 times, each time writing a line of output to the
  137. monitor.  Suffice it to say at this time, that the value of the
  138. variable Count, as defined here, is available globally, meaning
  139. anywhere in the entire Pascal program.  We will define the scope
  140. of variables shortly.  Finally, the last procedure call is made,
  141. causing the ending message to be displayed, and the program
  142. execution is complete.  
  143.  
  144.  
  145. THE UNBROKEN RULE OF PASCAL
  146. _________________________________________________________________
  147.  
  148. Having examined your first Pascal procedures, there is a fine point
  149. that is obvious but could be easily overlooked.  We mentioned the
  150. unbroken rule of Pascal in an earlier chapter and it must be
  151. followed here too.  "Nothing can be used in Pascal until it has
  152. been defined".  The procedures must all be defined ahead of any
  153. calls to them, once again emphasizing the fact that they are part
  154. of the Declaration Part of the program, not the Statement Part.
  155.  
  156. Compile and run PROCED1.PAS to verify that it does what you expect
  157. it to do.
  158.  
  159.  
  160. MORE PROCEDURE CALLS
  161. _________________________________________________________________
  162.  
  163. Assuming you have run PROCED1.PAS successfully    ===============
  164. and understand its output, let's go on to           PROCED2.PAS
  165. PROCED2.PAS and examine it.  In this program we   ===============
  166. will see how to call a procedure and take along
  167. some data for use within the procedure.  To
  168. begin with, notice that there are three procedure calls in the
  169. Statement Part of the program and each has an additional term not
  170. contained in the calls in the last program, namely the variable
  171. name Index within brackets.  This is Pascal's way of taking along
  172. a variable parameter to the procedure.  
  173.  
  174.  
  175.  
  176.                                                          Page 5-3
  177.  
  178.                              Chapter 5 - Procedures and Functions
  179.  
  180. You will notice that the variable Index is defined as an integer
  181. type variable in the very top of the Declaration Part.  Since we
  182. are taking an integer type variable along when we visit the
  183. procedure Print_Data_Out, it had better be expecting an integer
  184. variable as input or we will have a type mismatch.  In fact,
  185. observing the procedure heading itself in line 7, indicates that
  186. it is indeed expecting an integer variable but it prefers to call
  187. the variable Puppy inside of the procedure.  Calling it something
  188. different poses no problem as long as the main program doesn't try
  189. to call its variable Puppy, and the procedure doesn't try to use
  190. the name Index.  Both are actually referring to the same piece of
  191. data but they simply wish to refer to it by different names.  
  192.  
  193.  
  194.  
  195. FORMAL AND ACTUAL PARAMETERS
  196. _________________________________________________________________
  197.  
  198. The parameters listed within the parentheses of the procedure
  199. header are called the formal parameters, whereas the parameters
  200. listed within the parentheses of the procedure call in the main
  201. program are referred to as the actual parameters.  Observe that the
  202. next procedure is called with Index as the actual parameter and the
  203. procedure prefers to use the name Cat as the formal parameter name. 
  204. In both cases, the procedures simply print out the parameter passed
  205. to it, and each then try to modify the value passed to it before
  206. passing it back.  We will see that one will be successful and the
  207. other will not.
  208.  
  209. We are in a loop in which Count is incremented from 1 to 3 and
  210. Pascal does not allow us to modify the loop variable so we make a
  211. copy of the value in line 21 and call it Index.  We can then modify
  212. Index in the main program if we desire.  
  213.  
  214.  
  215.  
  216. CALL BY VALUE
  217. _________________________________________________________________
  218.  
  219. In line 7, the procedure heading does not contain the reserved word
  220. var in front of the passed parameter and therefore the parameter
  221. passing is only one way because of the way Pascal is defined. 
  222. Without the reserved word var in front of the variable Puppy, the
  223. system makes a copy of Index, and passes the copy to the procedure
  224. which can do anything with it, using its new name, Puppy, but when
  225. control returns to the main program, the original value of Index
  226. is still there.  The copy of Index named Puppy is modified in the
  227. procedure, but the original variable named Index remains unchanged. 
  228. You can think of the passed parameter without the var as one way
  229. parameter passing.  This is a "call by value" because only the
  230. value of the actual variable is passed to the procedure.
  231.  
  232.  
  233.  
  234.  
  235.                                                          Page 5-4
  236.  
  237.                              Chapter 5 - Procedures and Functions
  238.  
  239. CALL BY REFERENCE
  240. _________________________________________________________________
  241.  
  242. In line 13, the second procedure has the reserved word var in front
  243. of its desired name for the variable, namely Cat, so it can not
  244. only receive the variable, it can modify it, and return the
  245. modified value to the main program.  A copy is not made, but the
  246. original variable named Index is actually passed to this procedure
  247. and the procedure can modify it, therefore communicating with the
  248. main program.  The formal parameter name, Cat in the procedure, is
  249. actually another name for the actual variable named Index in the
  250. main program.  A passed parameter with a var in front of it is
  251. therefore a two way situation.  This is a "call by reference" since
  252. a reference to the original variable is passed to the procedure.
  253.  
  254.  
  255.  
  256. SOME NEW TERMINOLOGY
  257. _________________________________________________________________
  258.  
  259. To restate some of the new terminology in the last few paragraphs,
  260. the parameter name in the calling program is referred to as the
  261. actual parameter, and the parameter name in the procedure is
  262. referred to as the formal parameter.  In the last example then, the
  263. actual parameter is named Index and the formal parameter in the
  264. procedure is named Cat.  It should be pointed out that it is called
  265. a formal parameter whether it is a "call by reference" or a "call
  266. by value".  This terminology is used in many other programming
  267. languages, not only in Pascal.
  268.  
  269. When you run this program, you will find that the first procedure
  270. is unable to return the value of 12 back to the main program, but
  271. the second procedure does in fact succeed in returning its value
  272. of 35 to the main program.  Spend as much time as you like studying
  273. this program until you fully understand it.  It should be noted
  274. that as many parameters as desired can be passed to and from a
  275. procedure by simply making a list separated by commas in the calls,
  276. and separated by semicolons in the procedure.  This will be
  277. illustrated in the next example program.
  278.  
  279. Compile and run PROCED2.PAS and study the output.  You should be
  280. able to comprehend all of the output.  If it is not clear, reread
  281. the last few paragraphs.
  282.  
  283. To add to your knowledge of Pascal, examine the   ===============
  284. program PROCED3.PAS for an example of a             PROCED3.PAS
  285. procedure call with more than one variable in     ===============
  286. the call.  Normally, you would group the three
  287. input values together to make the program more
  288. readable, but for purposes of illustration, they are separated. 
  289. Observe that the variable Fruit is a two way variable because it
  290. is the 3rd variable in the actual parameter list and corresponds
  291. to the 3rd formal parameter in the procedure header.  Since the
  292. third variable is a "call by reference", it has the ability to
  293.  
  294.                                                          Page 5-5
  295.  
  296.                              Chapter 5 - Procedures and Functions
  297.  
  298. return the sum of the other 3 variables to the main program.  This
  299. is the reason it was defined this way.
  300.  
  301. Compile and run PROCED3.PAS to see that it does what you expect it
  302. to do based on the above explanation.
  303.  
  304.  
  305.  
  306. "CALL BY REFERENCE" OR "CALL BY VALUE"?
  307. _________________________________________________________________
  308.  
  309. It may seem to you that it would be a good idea to simply put the
  310. word var in front of every formal parameter in every procedure
  311. header to gain maximum flexibility, but using all "call by
  312. references" could actually limit your flexibility.  There are two
  313. reasons to use "call by value" variables when you can.  The first
  314. is simply to shield some data from being corrupted by the
  315. procedure.  This is becoming a very important topic in Software
  316. Engineering known as "information hiding" and is the primary basis
  317. behind Object Oriented Programming which will be discussed in
  318. chapters 14 and 15 of this tutorial.
  319.  
  320. Secondly is the ability to use a constant in the procedure call. 
  321. Modify line 17 of PROCED3.PAS as follows;
  322.  
  323.      Add_The_Fruit(12,Orange,Fruit,Pear);
  324.  
  325. and compile and run the program.  Since Value1 is a "call by
  326. value", the constant 12 can be used and the program will compile
  327. and run.  However, if you change line 17 to;
  328.  
  329.      Add_The_Fruit(Apple,Orange,32,Pear);
  330.  
  331. you will find that it will not compile because Total is a "call by
  332. reference" and the system must be able to return a value for the
  333. formal parameter Total.  It cannot do this because 32 is a
  334. constant, not a variable.  A call by reference must have a variable
  335. for the actual parameter.  As a programming exercise, make Value1
  336. a call by reference by adding the word var in front of it in line
  337. 6, and you will find that the compiler will not allow you to
  338. replace the variable Apple with the constant 12 as was suggested
  339. earlier in this section.
  340.  
  341. The prior discussion should indicate to you that both "call by
  342. value" and "call by reference" have a useful place in Pascal
  343. programming and it is up to you to decide which you should use.
  344.      
  345. When you are satisfied with the present illustration and you have
  346. compiled and executed PROCED3.PAS several times to study the
  347. results of the suggested changes, we will go on to study the scope
  348. of variables using PROCED4.PAS.
  349.  
  350.  
  351.  
  352.  
  353.                                                          Page 5-6
  354.  
  355.                              Chapter 5 - Procedures and Functions
  356.  
  357. A MULTIPLY DEFINED VARIABLE
  358. _________________________________________________________________
  359.  
  360. If you will examine PROCED4.PAS, you will notice  ===============
  361. that the variable Count is defined twice, once      PROCED4.PAS
  362. in the main program var block and once in the     ===============
  363. var block contained within the procedure named
  364. Print_Some_Data.  This is perfectly legal and is
  365. within the Pascal definition.
  366.  
  367. The variable Index is defined only in the main program var block
  368. and is valid anywhere within the entire Pascal program, including
  369. the procedures and is said to be a global variable.  The variable
  370. Count is also defined in the main program var block and is valid
  371. anywhere within the entire Pascal program, except within the
  372. procedure where another variable is defined with the same name
  373. Count.  The two variables with the same name are in fact, two
  374. completely different variables, one being available only outside
  375. of the procedure and the other being available only within the
  376. procedure.  The variable More_Stuff is defined within the
  377. procedure, so it is invisible to the main program, since it is
  378. defined at a lower level than that of the main program.  It is only
  379. available for use within the procedure in which it is defined.
  380.  
  381. Any variable is available at any point in the program following its
  382. definition but only at the level of definition or below.  This
  383. means that any procedure in the Declaration Part of a program can
  384. use any variable defined in the Declaration Part of the program
  385. provided that the definition occurs prior to the procedure.  Any
  386. variable defined within a procedure cannot be used by the main
  387. program since the definition is at a lower level than the main
  388. program.
  389.  
  390. Be sure to compile and run PROCED4.PAS before continuing on to the
  391. next example program.
  392.  
  393.  
  394. PROCEDURES CALLING OTHER PROCEDURES
  395. _________________________________________________________________
  396.  
  397. Load and examine PROCED5.PAS to see an example    ===============
  398. of procedures that call other procedures.  Keep     PROCED5.PAS
  399. in mind that, "Nothing can be used in Pascal      ===============
  400. until it has been previously defined", and the
  401. order of procedures will be clear in this
  402. example.  Note that procedure Three calls procedure Two which in
  403. turn calls procedure One.
  404.  
  405. Compile and run PROCED5.PAS and study the output until you
  406. understand why it outputs each line in the order that it does.
  407.  
  408. Now that you have a good working knowledge of procedures, we need
  409. to make another important point.  Remember that any Pascal program
  410. is made up of three parts, the Program Heading, the Declaration
  411.  
  412.                                                          Page 5-7
  413.  
  414.                              Chapter 5 - Procedures and Functions
  415.  
  416. Part, and the Statement Part.  The Declaration Part is composed of
  417. five unique components, four of which we will discuss in detail in
  418. the next chapter, and the last component, which is composed of some
  419. number of procedures and functions.  We will cover functions in the
  420. next example, so for now simply accept the fact that it is like a
  421. procedure.  A procedure is also composed of three parts, a
  422. Procedure Heading, a Declaration Part, and a Statement Part.  A
  423. procedure, by definition, is therefore nothing more or less than
  424. another complete Pascal program embedded within the main program,
  425. and any number of procedures can be located in the Declaration Part
  426. of the main program.  These procedures are all in a line, one right
  427. after another.
  428.  
  429. Since a procedure is defined like the main program, it would seem
  430. to be possible to embed another procedure within the Declaration
  431. Part of any procedure.  This is perfectly valid and is often done,
  432. but remember that the embedded procedure can only be called by the
  433. procedure in which it is embedded, not by the main program.  This
  434. is a form of information hiding which is becoming popular in modern
  435. software engineering.
  436.  
  437. The previous paragraph is probably a bit difficult to grasp.  Don't
  438. worry about it too much now, as you become proficient as a Pascal
  439. programmer, you will very clearly see how embedded procedures are
  440. used.
  441.  
  442.  
  443. NOW LET'S LOOK AT A FUNCTION
  444. _________________________________________________________________
  445.  
  446. Now to keep a promise, let's examine the program ================
  447. named FUNCTION.PAS to see what a function is and   FUNCTION.PAS
  448. how to use it.  In this very simple program, we  ================
  449. have a function that simply multiplies the sum
  450. of two variables by 4 and returns the result. 
  451. The major difference between a function and a procedure is that the
  452. function returns a single value and is called from within a
  453. mathematical expression, a Writeln command, or anywhere that it is
  454. valid to use a variable, since it is really a variable itself. 
  455. Observing the Function Heading of the function, in line 6, you will
  456. notice that a function begins with the reserved word function. 
  457. Further observation reveals the two input variables inside the
  458. parenthesis pair being defined as integer variables, and following
  459. the parenthesis is a colon and another integer.  The last integer
  460. is used to define the type of the variable being returned to the
  461. main program. 
  462.  
  463. Any call to this function is actually replaced by an integer value
  464. upon completion of the call.  Therefore in line 14, the function
  465. is evaluated and the value returned is used in place of the
  466. function call.  The value is returned by assigning the return value
  467. to the name of the function as illustrated in line 8.  The result
  468. of the function is therefore assigned to the variable named Feet.
  469.  
  470.  
  471.                                                          Page 5-8
  472.  
  473.                              Chapter 5 - Procedures and Functions
  474.  
  475. Note that a function always returns a value and it may return
  476. additional values if some of its formal parameters are defined as
  477. "call by reference".  Be sure to compile and run this program. 
  478.  
  479.  
  480. NOW FOR THE MYSTERY OF RECURSION
  481. _________________________________________________________________
  482.  
  483. One of the great mysteries of Pascal and several ================
  484. other popular programming languages, is the        RECURSON.PAS
  485. recursion of procedure calls.  Simply defined,   ================
  486. recursion is the ability of a procedure to call
  487. itself.  Examine the Pascal example file
  488. RECURSON.PAS for an example of recursion.  The main program is very
  489. simple, it sets the variable named Count to the value 7 and calls
  490. the procedure Print_And_Decrement.  The procedure prefers to refer
  491. to the variable by the name Index but that poses no problem for us
  492. because we understand that the name of the formal parameter can be
  493. any legal identifier.  The procedure writes a line to the video
  494. display with the value of Index written within the line, and
  495. decrements the variable.
  496.  
  497. The if statement introduces the interesting part of this program. 
  498. If the variable is greater than zero, and it is now 6, then the
  499. procedure Print_And_Decrement is called once again.  This might
  500. seem to create a problem except for the fact that this is perfectly
  501. legal in Pascal.  Upon entering the procedure the second time, the
  502. value of Index is printed as 6, and it is once again decremented. 
  503. Since it is now 5, the same procedure will be called again, and it
  504. will continue until the value of Index is reduced to zero when each
  505. procedure call will be completed one at a time and control will
  506. return to the main program.
  507.  
  508.  
  509. ABOUT RECURSIVE PROCEDURES
  510. _________________________________________________________________
  511.  
  512. This is really a stupid way to implement this particular program,
  513. but it is the simplest recursive program that can be written and
  514. therefore the easiest to understand.  You will have occasional use
  515. for recursive procedures, so don't be afraid to try them.  Remember
  516. that the recursive procedure must have some variable converging to
  517. something, or you will have an infinite loop.  Compile and run this
  518. program and observe the value decrementing as the recursion takes
  519. place.
  520.  
  521. THE FORWARD REFERENCE
  522. _________________________________________________________________
  523.  
  524. Occasionally you will have a need to refer to a   ===============
  525. procedure before you can define it.  In that        FORWARD.PAS
  526. case you will need a forward reference.  The      ===============
  527. example program named FORWARD.PAS has an example
  528. of a forward reference in it.  In this program,
  529.  
  530.                                                          Page 5-9
  531.  
  532.                              Chapter 5 - Procedures and Functions
  533.  
  534. each one of the procedures calls the other, a form of recursion. 
  535. This program, like the last, is a very stupid way to count from 7
  536. to 0, but it is the simplest program possible with the forward
  537. reference.
  538.  
  539. The first procedure, Write_A_Line, has its header defined in
  540. exactly the same manner as any other procedure but instead of the
  541. normal procedure body, only the reserved word forward is given in
  542. line 6.  This tells the compiler that the procedure will be defined
  543. later.  The next procedure is defined as usual, then the body of
  544. Write_A_Line is given with only the reserved word procedure and the
  545. procedure name.  The variable reference has been defined earlier. 
  546. In this way, each of the procedure names are defined before they
  547. are called.
  548.  
  549. It would be possible, by using the forward reference in great
  550. numbers, to move the main program ahead of all procedure
  551. definitions and have the program structured like some other
  552. languages.  This style of programming would be perfectly legal as
  553. far as the compiler is concerned, but the resulting program would
  554. be very nonstandard and confusing.  You would do well to stick with
  555. conventional Pascal formatting techniques and use the forward
  556. reference sparingly.  Be sure you compile and run this program.
  557.  
  558.  
  559.  
  560. THE PROCEDURE TYPE
  561. _________________________________________________________________
  562.  
  563. Examine the program named PROCTYPE.PAS.  This is ================
  564. a new extension to the Pascal language by          PROCTYPE.PAS
  565. Borland beginning with version 5.0.  In this     ================
  566. program, the procedure Do_Math is defined as a
  567. procedure type in line 12, and three regular
  568. procedures are defined each of which have the same structure of
  569. formal parameters.  In the program, since Do_Math is a procedure
  570. type that is compatible with each of the defined procedures, it can
  571. be assigned one of the other procedures, and a call to Do_Math is
  572. identical to a call to that procedure to which it is currently
  573. assigned.  The program should be self explanatory with those few
  574. comments so you will be left to study the details on your own.  It
  575. is important that you recognize that the procedure type variable
  576. is used to call several different procedures.
  577.  
  578. Note the comments in lines 4 and 5 of the program.  When using a
  579. procedure type or a function type, which is the topic of the next
  580. example program, TURBO Pascal requires that you use the compiler
  581. directive F+, which forces the system to use far calls for all
  582. procedure calls.  Study the documentation for your version of TURBO
  583. Pascal to obtain more information on compiler directives.
  584.  
  585.  
  586.  
  587.  
  588.  
  589.                                                         Page 5-10
  590.  
  591.                              Chapter 5 - Procedures and Functions
  592.  
  593. Examine the program named FUNCTYPE.PAS for an    ================
  594. example of a program using some of the same        FUNCTYPE.PAS
  595. techniques as the last program but instead uses  ================
  596. a function type for the subprogram variable. 
  597. This program should be simple for you to study
  598. on your own concerning the details of operation.
  599.  
  600. The only rule concerning the procedure and function types which
  601. must be stated, is that a subprogram type variable can only be
  602. assigned subprogram names if the list of actual parameters are
  603. identical for the type and the subprogram.  This includes the type
  604. of the return value for a function.  Since this is a new extension
  605. to the TURBO Pascal languages, it has not been used much, so don't
  606. worry about it too much.  You should know that it can be done,
  607. because someday you will find a piece of Pascal code with this
  608. construct used.  Of course, you will someday find a good use for
  609. it yourself.
  610.  
  611.  
  612. PROGRAMMING EXERCISES
  613. _________________________________________________________________
  614.  
  615. 1.   Write a program to write your name, address, and phone number
  616.      with each Writeln in a different procedure.
  617.  
  618. 2.   Add a statement to the procedure in RECURSON.PAS to display
  619.      the value of Index after the call to itself so you can see the
  620.      value increasing as the recurring calls are returned to the
  621.      next higher level.
  622.  
  623. 3.   Rewrite TEMPCONV.PAS putting the centigrade to fahrenheit
  624.      formulas in a function call.
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.                                                         Page 5-11
  649.