TL11.TXT 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. TURBO-LESSONS
  2. A TURBO Pascal Tutorial
  3. Version 1.01
  4. Set # 2 - Lessons 11 - 17
  5. by Lyle M. Faurot
  6. June 27, 1985
  7. Copyright (C), 1985 by Lyle Faurot. All rights reserved.
  8. TURBO is a trademark of Borland International.
  9. Œ
  10. INTRODUCTION TO TURBO-LESSONS
  11. When Borland International ran their first ads for TURBO
  12. PASCAL my reaction was "You gotta be kidding - they can't do
  13. that". But, it seems they did! I've been enjoying this
  14. programming system ever since. Apparently a great many other
  15. programmers are either programming in TURBO or would like to
  16. learn how. TURBO-LESSONS were designed to meet that need.
  17. Each TURBO-LESSON is a bite-size tutorial which introduces
  18. one or more new concepts. Each of the lessons are presented in
  19. the same format which includes the OBJECTIVES of the lesson, and
  20. the TUTORIAL portion of the lesson. Most lessons direct you to
  21. work with a sample program which is provided with these lessons.
  22. To begin, you should print the TURBO-LESSONS before loading
  23. TURBO.
  24. ****************************************
  25. D I S T R I B U T I O N N O T I C E
  26. ****************************************
  27. TURBO-LESSONS are being distributed as USER-SUPPORTED software.
  28. Suggested donation, $5.00, for this second set, lessons 11-17,
  29. may be sent to:
  30. Lyle Faurot
  31. Box 494
  32. Moorhead, MN 56560
  33. COPYING BY ORGANIZATIONS
  34. Clubs and other non-profit organizations may copy these lessons
  35. for their members, with the following conditions:
  36. 1. No charge is made for TURBO-LESSONS. A distribution charge
  37. may be made to cover the price of the diskette.
  38. 2. Club members are informed that TURBO-LESSONS are distributed
  39. as user-supported software.
  40. 3. TURBO-LESSONS are distributed unmodified.
  41. Œ
  42. T A B L E O F C O N T E N T S
  43. (Set # 2, Lessons 11 - 17)
  44. Page
  45. Introduction
  46. Distribution Notice
  47. TURBO-LESSON 11: INTRODUCTION TO FUNCTIONS
  48. Pascal subprograms . . . . . . . . . . . . . . . . . 50 *
  49. FUNCTION declaration . . . . . . . . . . . . . . . . 50
  50. User-defined function . . . . . . . . . . . . . . . . 51
  51. TURBO-LESSON 12: FUNCTION APPLICATION - ERROR DETECTION
  52. Error detection . . . . . . . . . . . . . . . . . . . 55
  53. Using a predefined function . . . . . . . . . . . . . 56
  54. Writing your own function . . . . . . . . . . . . . . 58
  55. TURBO-LESSON 13: STRINGS
  56. Strings . . . . . . . . . . . . . . . . . . . . . . . 60
  57. String replacement statement . . . . . . . . . . . . 60
  58. Predefined string function, LENGTH . . . . . . . . . 63
  59. TURBO-LESSON 14: INTRODUCTION TO PROCEDURES
  60. PROCEDURE declaration . . . . . . . . . . . . . . . . 65
  61. Using a procedure . . . . . . . . . . . . . . . . . . 66
  62. Using parameters . . . . . . . . . . . . . . . . . . 67
  63. A counter with error checking . . . . . . . . . . . . 68
  64. TURBO-LESSON 15: INTERACTIVE SCREEN HANDLING
  65. Setting up a data entry screen . . . . . . . . . . . 70
  66. Being nice to users - ClrScr . . . . . . . . . . . . 71
  67. Getting around the screen - GotoXY . . . . . . . . . 72
  68. Screen messages and accepting user input . . . . . . 73
  69. TURBO-LESSON 16: REAL NUMBERS
  70. Range of real numbers . . . . . . . . . . . . . . . . 74
  71. Input/Output of real numbers . . . . . . . . . . . . 75
  72. Calculations with real numbers . . . . . . . . . . . 76
  73. Calculations with integers and real numbers . . . . . 78
  74. TURBO-LESSON 17: A TIMING FUNCTION
  75. Include files . . . . . . . . . . . . . . . . . 79
  76. A timing function . . . . . . . . . . . . . . . . . . 80
  77. Improving timing accuracy . . . . . . . . . . . . . . 81
  78. Sources of errors in timing . . . . . . . . . . . . . 82
  79. * (Note - page 42 was the last page of lesson 10. Some page
  80. numbers were left unused to make it easier to incorporate
  81. the changes you suggest.)
  82. Œ
  83. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 50
  84. TURBO-LESSON 11: INTRODUCTION TO FUNCTIONS
  85. OBJECTIVES - In this lesson, you will learn about:
  86. 1, Pascal Subprograms
  87. 2. FUNCTION declaration
  88. 3. User-defined function
  89. 1. Pascal Subprograms.
  90. The example programs in earlier lessons have been rather small.
  91. As programs grow in size and complexity, it is essential that you
  92. utilize the appropriate tools to simplify the programming effort.
  93. The subprogram is one of the most powerful tools for simplifying
  94. a complex program. Subprograms may be viewed as "building
  95. blocks" for constructing programs.
  96. As you learn to write Pascal programs, you will find Pascal
  97. subprograms rather easy to write - they are almost identical to
  98. programs!
  99. There are two types of subprograms in Pascal: FUNCTIONS and
  100. PROCEDURES. Both types may be either user-defined (written by
  101. you) or pre-defined as a part of Pascal.
  102. In this lesson you will work with a user-defined FUNCTION.
  103. Most of what you learn about FUNCTIONS also applies to
  104. PROCEDURES.
  105. 2. FUNCTION declaration.
  106. Below is a simplified structure of a Program, Function, and
  107. Procedure:
  108. PROGRAM FUNCTION PROCEDURE
  109. LABEL LABEL LABEL
  110. CONST CONST CONST
  111. TYPE TYPE TYPE
  112. VAR VAR VAR
  113. FUNCTION(s) FUNCTION(s) FUNCTION(s) (0 or more FUNCTIONS)
  114. PROCEDURE(s) PROCEDURE(s) PROCEDURE(s) ( and PROCEDURES )
  115. BEGIN BEGIN BEGIN
  116. (processing (processing (processing
  117. statements) statements) statements)
  118. END. END; END;
  119. Notice that they look very similar.
  120. Œ
  121. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 51
  122. One difference, visible here, is the END of the processing block:
  123. A period follows the END of a PROGRAM block, but a semi-colon
  124. follows the END of a FUNCTION or PROCEDURE block.
  125. 3. User-defined function.
  126. ##### DO:
  127. Examine FUNCTION Cube in PROG11 using the editor.
  128. FUNCTION Cube(Number:Integer) : Integer;
  129. The FUNCTION declaration above means:
  130. FUNCTION Type of subprogram, a FUNCTION, not a PROCEDURE.
  131. Cube The name of the FUNCTION.
  132. Cube must be assigned a value by one of the
  133. statements in the FUNCTION BEGIN END block.
  134. (Number:Integer) An integer "parameter" called Number is used
  135. within the FUNCTION.
  136. This is the number to be cubed.
  137. : Integer; Type of value to be assigned to Cube.
  138. ##### DO:
  139. Study the connection between the following two statements:
  140. In FUNCTION Cube:
  141. FUNCTION Cube(Number:Integer) : Integer;
  142. In PROG11:
  143. WriteLn('The cube is: ', Cube(No) );
  144. Cube(No) in the statement above invokes (calls) the FUNCTION Cube
  145. to operate on the number called No.
  146. PROG11, No -----> Number, in FUNCTION Cube.
  147. The value of the variable called No in PROG11 is provided to the
  148. FUNCTION to use as a value of its variable called Number.
  149. Œ
  150. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 52
  151. ##### DO:
  152. Determine how Cube obtains its value in the FUNCTION.
  153. Cube := Number * Number * Number;
  154. This statement cubes the value in the variable Number (which
  155. Number received from the variable, No, in the main program) and
  156. assigns the result to the FUNCTION name, Cube.
  157. The WriteLn statement prints this resulting value in the position
  158. indicated by the reference to Cube(No) in the WriteLn list.
  159. ##### DO:
  160. Run PROG11 several times using the following values for input:
  161. 3, -3, 0
  162. 31 (cube should be 29791)
  163. 32 (cube should be 32768) Is it? Remember this problem from
  164. an earlier lesson? You will explore some techniques for
  165. detecting this problem in the next lesson.
  166. Now, let's see what happens if you change some of the things in
  167. the FUNCTION and the reference to it.
  168. What would happen if you used a constant instead of No in the
  169. reference, Cube(No)?
  170. ##### DO:
  171. Change the WriteLn statement to:
  172. WriteLn('The cube is: ', Cube(3) );
  173. Run the program several times with different input values.
  174. Do you get the same result no matter what you input?
  175. Notice that the variable, No, which you input, is no longer used
  176. in the FUNCTION reference, Cube(3).
  177. What about expressions in the reference to the function?
  178. Œ
  179. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 53
  180. ##### DO:
  181. Change the FUNCTION reference to Cube(No - 2).
  182. Run the program several times with input values:
  183. 5 (No - 2) is 3, so cube should be 27.
  184. 1 (No - 2) is -1, so cube should be -1.
  185. -1 (No - 2) is -3, so cube should be -27.
  186. Do integer expressions work o.k?
  187. What if you tried to use a non-integer value in the Cube(No)
  188. reference?
  189. ##### DO:
  190. Modify the WriteLn statement to:
  191. WriteLn('The cube is: ', Cube('This is a string') )
  192. Run the program.
  193. Did the compiler complain?
  194. Error 44: Type mismatch.
  195. When you press ESC, the cursor stops at the offending reference.
  196. The problem: 'This is a string' was given to the function to use
  197. as an integer value, but it is a string, not an integer.
  198. A good way to learn to recognize errors, is to introduce one
  199. error at a time and check the results.
  200. Let's look at some errors which you might make in setting up the
  201. FUNCTION itself.
  202. Œ
  203. TURBO-LESSONS - A Pascal Tutorial Version 1.01 Page 54
  204. ##### DO:
  205. Change the type of Number to Char. The FUNCTION declaration
  206. should appear:
  207. FUNCTION Cube(Number:Char) : Integer;
  208. What happens when you attempt to run or compile the program?
  209. Error 47: Operand type(s) does not match operator.
  210. When you press ESC, the cursor stops, not at the line you
  211. modified, but at the line:
  212. Cube := Number * Number * Number;
  213. Why?
  214. There is nothing wrong with the line you changed. It may not do
  215. what you want it to, but to the computer, it is just an Integer
  216. FUNCTION with a character for input.
  217. The problem appears (to the compiler) when an attempt is made to
  218. multiply a character times a character in the line marked by the
  219. cursor. At this point the compiler alerts you to the problem.
  220. DEBUGGING NOTE: DON'T FORGET, THE CURSOR POINTS TO THE PLACE
  221. WHERE THE ERROR WAS DETECTED BY THE COMPILER.
  222. THE CAUSE OF THE ERROR MAY BE ELSEWHERE!
  223. What would happen if you inadvertently designated the function as
  224. something other than Integer?
  225. ##### DO:
  226. Change the FUNCTION declaration to:
  227. FUNCTION Cube(Number:Integer) : Char;
  228. Run the program. What results did you get?
  229. Error 44 again - Type mismatch.
  230. Again, the cursor points to the calculation.
  231. This time though, the calculation itself is o.k. The problem
  232. occurs when an attempt is made to assign the integer result to
  233. the Character FUNCTION, Cube.
  234. Œ
  235.