home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / PROG / PASCAL / PASTUT24.ZIP / PTUTRTXT.ZIP / CHAP05.TXT < prev    next >
Encoding:
Text File  |  1989-12-01  |  28.6 KB  |  650 lines

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