TL14.TXT 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 65
  2. TURBO-LESSON 14: INTRODUCTION TO PROCEDURES
  3. OBJECTIVES - In this lesson, you will learn about:
  4. 1. PROCEDURE declaration
  5. 2. Using a Procedure
  6. 3. Using Parameters
  7. 4. A counter with error checking
  8. 1. PROCEDURE declaration.
  9. The PROCEDURE subprogram is similar to the FUNCTION subprogram
  10. introduced in an earlier lesson. The form of the declaration
  11. is:
  12. PROCEDURE Add(No_1, No_2 : Integer; VAR Sum : Integer);
  13. BEGIN
  14. Sum := No_1 + No_2;
  15. END;
  16. Add is the name of the Procedure.
  17. No_1, No_2, and Sum are integer variables called "parameters".
  18. VAR in front of Sum indicates that this parameter is a "two-way
  19. street". It can receive data from the calling program, and
  20. return data to the calling program. No_1 and No_2 can only
  21. receive data from the calling program.
  22. The BEGIN END block defines the processing performed by the
  23. procedure:
  24. Add the value in the memory location, No_1, to the value in
  25. memory location, No_2, and place the result in the memory
  26. location, Sum.
  27. Œ
  28. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 66
  29. 2. Using a Procedure.
  30. A reference to this procedure in the main program (or another
  31. procedure or function) would have the following form:
  32. Add(2, Count, Adjusted_Count);
  33. The procedure is "called" or utilized by simply using its name as
  34. a statement. (When you define a Procedure or Function, you are
  35. adding additional "statements" to your programming language.)
  36. Notice that there are three "parameters" here, and three in the
  37. Procedure declaration. The three here are associated with the
  38. ones in the declaration by position.
  39. The first parameter here, the integer, 2, provides input to the
  40. first parameter of the procedure, No_1.
  41. The second, the variable, Count, provides input to the second
  42. parameter of the procedure, No_2.
  43. The third, Adjusted_Count, provides input to the third
  44. parameter of the procedure, Sum.
  45. Since Sum is declared VAR, variable, it also provides output back
  46. to Adjusted_Count.
  47. In the Main Program In Procedure Add
  48. 2 ----------> No_1 { When Procedure }
  49. Count ----------> No_2 { is called }
  50. Adjusted_Count ----------> Sum { }
  51. ---------------------------------------------------------------
  52. Adjusted_Count <---------- Sum { When Procedure ends }
  53. ##### DO:
  54. Inspect PROG14.
  55. Run the program.
  56. ##### DO:
  57. Add the following statement as the first statement in the main
  58. program:
  59. Sum := 10;
  60. Run the program.
  61. Does it make any difference what value is stored in Sum before
  62. Procedure Add is referenced? Look at the procedure - is Sum used
  63. as an input for the calculation, or only as a result?
  64. Œ
  65. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 67
  66. 3. Using Parameters.
  67. You have already been using parameters, but in this section, you
  68. will see a little more of the power and flexibility of
  69. parameters.
  70. ##### DO:
  71. Change the Add statement in the main program to:
  72. Add(2, 2, Adjusted_Count);
  73. Run the program. Is the result as expected?
  74. ##### DO:
  75. Change the Add to:
  76. Add(Count, Count, Adjusted_Count);
  77. Run the program. Any surprizes?
  78. ##### DO:
  79. Change the Add to:
  80. Add(2, 3, 4);
  81. Run the program. What happened?
  82. The compiler refused to accept 4 as a variable identifier.
  83. The VAR preceding Sum in the procedure declaration puts a limit
  84. on the corresponding parameter in the calling program: it has to
  85. be a variable location to receive the value of Sum when the
  86. procedure finishes its calculation.
  87. The first two parameters in the procedure, No_1 and No_2, are
  88. used only for input to the procedure - they do not provide any
  89. output back to the corresponding parameters in the calling
  90. program.
  91. For this reason, the first two parameters in the calling program
  92. are less restricted. They can be constants, variables, or
  93. expressions, as long as they are of the same type, integer, as
  94. the corresponding parameters in the procedure.
  95. Œ
  96. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 68
  97. ##### DO:
  98. Change the Add statement:
  99. Add(4, 2 * Count + 5, Adjusted_Count);
  100. Run the program. Does the expression work o.k. as a parameter?
  101. ##### DO:
  102. Change the Add to:
  103. Add(Count, Count, Count);
  104. Also change the WriteLn to print the value of Count instead of
  105. Adjusted_Count.
  106. Run the program. Any problems?
  107. 4. A counter with error checking.
  108. You could use the Procedure Add as a counter by using the
  109. following call:
  110. Add(Count, 1, Count);
  111. The procedure would add 1 to Count and put the result in Count.
  112. You could accomplish the same thing with the statement
  113. Count := Count + 1;
  114. So why bother to use the procedure?
  115. What if Count reaches the upper limit of the integer range,
  116. 32767?
  117. In the main program you could expand the counting statement to:
  118. IF Count < 32767
  119. THEN
  120. Count := Count + 1
  121. ELSE
  122. WriteLn('Counter reached upper limit');
  123. Œ
  124. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 69
  125. PROGRAMMING NOTE: HOW DO YOU DECIDE WHAT GOES INTO THE MAIN
  126. PROGRAM AND WHAT TO PUT IN SUBPROGRAMS?
  127. FROM THE FOLLOWING DISCUSSION, TRY TO FORMULATE
  128. AT LEAST ONE RULE FOR MAKING THIS DECISION.
  129. This looks a little messy, maybe you should use a procedure to
  130. get this out of the main program.
  131. Not a bad reason, but there's a more important reason:
  132. Ask yourself, "How much of the processing in the IF statement
  133. above is of interest in the main program?"
  134. Probably only the fact that a count is being incremented. The
  135. error checking and how it is done is probably of little interest
  136. and just clutters up the main program.
  137. ##### DO:
  138. Write your own procedure to increment the counter, Count.
  139. Call the procedure, Increment.
  140. Check for the upper limit of the integer range. (The IF
  141. statement above would be one way.)
  142. Note that only one parameter is needed, preceded by VAR.
  143. ##### DO:
  144. In the main program, add the following to check out your
  145. procedure:
  146. FOR I := 1 to 10 DO
  147. BEGIN
  148. Increment(Count);
  149. WriteLn('Count = ', Count);
  150. END;
  151. Run the program using the following values as input for count:
  152. 0, 3, -34, 32760
  153. (PROG14A is provided in case you need help.)
  154. You could also write a procedure, Decrement, to decrease a
  155. counter. Note that the error checking would be checking the
  156. lower limit, -32768, or perhaps 0 depending on how you intended
  157. to use the counter.
  158. Œ
  159.