TL12.TXT 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 55
  2. TURBO-LESSON 12: FUNCTION APPLICATION - ERROR DETECTION
  3. OBJECTIVES - In this lesson, you will learn about:
  4. 1. Error detection
  5. 2. Using a predefined function
  6. 3. Writing your own function
  7. 1. Error detection.
  8. In the previous lesson, you found that some input values caused
  9. a problem. The function used in PROG11 calculated the cube of a
  10. number entered. If the result was outside the range of valid
  11. integers (for this version of Pascal), a run-time error
  12. terminated the execution of the program - NOT A NICE EVENT!
  13. If you write programs for others to use, you will have to deal
  14. with the problem of ERRORS.
  15. There are several approaches to error handling:
  16. (1) Error detection before it happens - prevent the occurrence of
  17. the error.
  18. (2) Error detection when it happens - take corrective action.
  19. (3) Ignore the error - let the program bomb!
  20. The 3rd is not usually acceptable - but may be o.k. in the early
  21. stages of program development since you, the programmer, can fix
  22. the problem. You may also find alternate ways to program for the
  23. same result while avoiding the possibility of the error.
  24. The 2nd, error detection when it happens, will be explored later.
  25. Input/Output errors are typical examples of this class of errors.
  26. In this lesson, you will find ways to detect a problem and
  27. prevent its occurrence.
  28. ##### DO:
  29. In PROG12, examine FUNCTION Cube.
  30. The function has been expanded to detect integers which are too
  31. small or too large to produce a valid integer cube. If a number
  32. is entered which would cause an error, the result is set to 0
  33. instead of the erroneous result.
  34. Œ
  35. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 56
  36. ##### DO:
  37. Run PROG12 several times using the following values for input:
  38. 3, -3, 31, 32, -32, -33, 0
  39. Were all the results as expected? The inputs,32 and -33, would
  40. produce cubes out of the range of valid integers, so these two
  41. should have given results of 0.
  42. What about 0 as an input? Did you get the correct result?
  43. Can you determine whether a result of 0 is valid (0 input) or
  44. invalid (input of < -32 or > 31)?
  45. Later in this lesson you will write your own function to deal
  46. with this problem!
  47. 2. Using a predefined function.
  48. Pascal provides many functions and procedures which are already
  49. defined.
  50. Some advantages of using predefined subprograms:
  51. (1) The subprogram is already debugged.
  52. (2) The subprogram doesn't take up room in your program.
  53. (3) You can spend your time on more interesting programming,
  54. no need to "reinvent the wheel".
  55. To use a predefined function, you have to know:
  56. (1) The name of the function
  57. (2) What goes in (what values do you provide as input?)
  58. (3) What comes out (what result is associated with the function
  59. name?)
  60. The absolute value function, ABS, can be used in PROG12 to
  61. illustrate the use of a predefined function.
  62. What goes in What comes out
  63. 3 --------> [ABS] --------> 3
  64. -5 --------> [ABS] --------> 5
  65. The absolute value function provides a positive number of the
  66. same magnitude as the positive or negative number input to the
  67. function.
  68. Œ
  69. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 57
  70. ##### DO:
  71. Add the following statement after the ReadLn(No) statement in
  72. PROG12:
  73. WriteLn('Absolute value: ', ABS(No) );
  74. Run the program several times with both positive and negative
  75. numbers.
  76. NOTE: THERE ARE OFTEN SEVERAL WAYS TO DO THE SAME THING IN
  77. PROGRAMMING. YOU SHOULD BE LOOKING FOR WAYS TO DECIDE
  78. WHICH OF SEVERAL PROGRAMMING SOLUTIONS IS BETTER IN A
  79. GIVEN CASE.
  80. The next two exercises demonstrate two ways to use ABS in the
  81. error detection problem. You should decide which of the two is
  82. better. (Maybe neither is better than the present form of
  83. PROG12).
  84. ##### DO:
  85. Change the WriteLn with the reference to Cube to:
  86. WriteLn('The cube is; ', Cube(ABS(No)) );
  87. Test the program with various inputs to make sure the results are
  88. the same as before.
  89. Is the sign correct on all cubes (using both positive and
  90. negative inputs)?
  91. Try -32 as an input. What was the result? Why?
  92. Œ
  93. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 58
  94. Now, try another way to use the ABS function.
  95. ##### DO:
  96. Restore the WriteLn to its original form:
  97. WriteLn('The cube is: ', Cube(No) );
  98. Also change the first line of the IF statement in the FUNCTION
  99. Cube to:
  100. IF ABS(Number) > 31
  101. Test the program with several values.
  102. Did you get the same results as in the previous exercise?
  103. Which way do YOU think is best? (Maybe neither because of the
  104. problem with -32?)
  105. How would you decide whether to use the function, ABS, in the
  106. main program or in the function, Cube?
  107. Is the action accomplished by ABS of interest to you in getting
  108. the cube of a number? If not, it should probably be pushed out
  109. of the main program and into the subprogram.
  110. 3. Writing your own function.
  111. Now, it's your turn. Another approach to the error detection
  112. problem uses a second function, which you are about to write!
  113. Give the function the name: Has_Valid_Cube
  114. The function will have one input: Number of type Integer
  115. The type of the function will be: Boolean
  116. What the function does:
  117. If Number would produce a valid cube, the function,
  118. Has_Valid_Cube, will have the value, TRUE.
  119. If Number would produce an error, Has_Valid_Cube will have the
  120. value, FALSE.
  121. Œ
  122. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 59
  123. ##### DO:
  124. Write the function, Has_Valid_Cube. Place it before the main
  125. program. It can be either before or after the function, Cube.
  126. Look at FUNCTION Cube if you need help with the form of the
  127. function declaration or the IF statement needed.
  128. ##### DO:
  129. In the main program, replace the WriteLn which references Cube
  130. with the following:
  131. IF Has_Valid_Cube (No)
  132. THEN
  133. WriteLn('The cube is: ', Cube(No) )
  134. ELSE
  135. BEGIN
  136. WriteLn('The cube of ',No,' is outside the integer range');
  137. WriteLn('in this version of Pascal.');
  138. END;
  139. Test the program with several positive and negative values and
  140. values which would cause erroneous cubes.
  141. (If you have trouble writing the function, PROG12A is available
  142. as a sample. Don't check PROG12A until you have given it a try
  143. on your own!)
  144. Are there any other improvements you want to make to PROG12?
  145. FUNCTION Cube still checks for invalid inputs. Is this still
  146. necessary?
  147. ##### DO:
  148. Change FUNCTION Cube so that it does no error checking, just
  149. calculates the cube of the number input.
  150. Test the program with several values including 0.
  151. Note that there is no longer any ambiguity when the result is a
  152. cube of 0.
  153. Œ
  154.