home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / basic / basictut.ark / TUTR.13 < prev    next >
Encoding:
Text File  |  1985-02-10  |  8.0 KB  |  171 lines

  1. 3000    ' tutor 13
  2. 3010    GOSUB 37100
  3. 3020    T5$ = SPACE$(5) : T10$ = SPACE$(10) : T15$ = SPACE$(15) :VT$ = CHR$(11)
  4. 3030    PRINT T10$;"   A subroutine may be thought of as a program within a program."
  5. 3040    PRINT T10$;"The standard functions are usually subroutines, since when one"
  6. 3050    PRINT T10$;"of them is called for, the machine substitutes the argument into"
  7. 3060    PRINT T10$;"a series (sum of terms) to evaluate the function. We may write"
  8. 3070    PRINT T10$;"our own subroutine for a process to be done more than once in"
  9. 3080    PRINT T10$;"our main program. The subroutine will be executed when the main"
  10. 3090    PRINT T10$;"program transfers control to it. Note these expressions:"
  11. 3100    PRINT
  12. 3110    PRINT T10$;"    (1) gosub    (2) dosub    (3) gofor    (4) routine"
  13. 3120    PRINT
  14. 3130    PRINT T10$;"Which looks like it might call in a subroutine? ";
  15. 3140    GOSUB 40900 : S$ = CVTSTR$
  16. 3150    PRINT
  17. 3160    IF S$ = "1" OR S$ = "GOSUB" THEN 3220
  18. 3170    IF (S$ = "2" OR S$ = "3" OR S$ = "4") OR 
  19.         (S$ = "DOSUB" OR S$ = "GOFOR" OR S$ = "ROUTINE")
  20.         THEN 3200
  21. 3180    PRINT ELR$;T10$;"Wait a minute, now. That's not an offered choice.";VT$;VT$;CR$;
  22. 3190    GOTO 3130
  23. 3200    PRINT ELR$;T10$;"It doesn't, though. Try another one, ok";VT$;VT$;CR$;
  24. 3210    GOTO 3130
  25. 3220    PRINT VT$;VT$;CR$;EES$;
  26. 3230    PRINT T10$;"   That's it. For instance, 'gosub 400' means 'go to the program"
  27. 3240    PRINT T10$;"(subroutine) which begins on line 400'. After the subprogram"
  28. 3250    PRINT T10$;"has been executed, control is returned to the statement after"
  29. 3260    PRINT T10$;"the one which called for the subroutine. How is this done:"
  30. 3270    PRINT
  31. 3280    PRINT T10$;"    (1) goback    (2) main    (3) return    (4) go to"
  32. 3290    PRINT
  33. 3300    PRINT T10$;"do you think? ";
  34. 3310    GOSUB 40900 : R$ = CVTSTR$
  35. 3320    PRINT
  36. 3330    IF R$ = "3" OR R$ = "RETURN" THEN 3360
  37. 3340    PRINT T10$;"You haven't guessed it yet. Try again.";VT$;VT$;CR$;
  38. 3350    GOTO 3300
  39. 3360    PRINT VT$;VT$;CR$;
  40. 3370    PRINT T10$;"   You've got it. You use this one as the last statement in"
  41. 3380    PRINT T10$;"your subprogram to RETURN to the point where you left the main"
  42. 3390    PRINT T10$;"program. The next statement in the main program is then executed."
  43. 3400    PRINT T10$;"We'll try an example. Do you prefer a (1) geometry or (2) logic"
  44. 3410    PRINT T10$;"subprogram as an illustration? ";
  45. 3420    GOSUB 40900 : E$ = CVTSTR$
  46. 3430    PRINT
  47. 3440    IF E$ = "1" OR LEFT$(E$,2) = "GE" THEN E$ = "1" : GOSUB 37100 : GOTO 3510
  48. 3450    IF E$ = "2" OR LEFT$(E$,2) = "LO" THEN GOSUB 37100 : GOTO 3480
  49. 3460    PRINT T10$;"We don't have that one. Is it '1' or '2'?";VT$;VT$;CR$;
  50. 3470    GOTO 3410
  51. 3480    PRINT T10$;"   Here's one that will give us the maximum and minimum values of"
  52. 3490    PRINT T10$;"n numbers, assuming no two numbers are equal. I'll leave one of"
  53. 3500    GOTO 3530
  54. 3510    PRINT T10$;"   Here's one to compute the radius of a circle, given two points"
  55. 3520    PRINT T10$;"(x1,y1) and (x2,y2) plus the crown height h. I'll leave one of"
  56. 3530    PRINT T10$;"the main problem statements blank; you fill it in correctly"
  57. 3540    PRINT T10$;"so that it will call in our subroutine."
  58. 3550    PRINT
  59. 3560    FOR J = 100 TO 130 STEP 10
  60. 3570    PRINT J; "' main prog. line"
  61. 3580    NEXT J
  62. 3590    PRINT J
  63. 3600    FOR J = 150 TO 170 STEP 10
  64. 3610    PRINT J; "' main prog. line"
  65. 3620    NEXT J
  66. 3630    PRINT J;"REM the next 8 statements constitute the subroutine"
  67. 3640    IF E$ = "1"  THEN 3980
  68. 3650    PRINT " 200 LET B = A(1)"
  69. 3660    PRINT " 210 LET C = A(1)"
  70. 3670    PRINT " 220 FOR I = 2 to N"
  71. 3680    PRINT " 230 IF A(i) <= B THEN 250"
  72. 3690    PRINT " 240 LET B = A(i)"
  73. 3700    PRINT " 250 IF A(i) >= C THEN 260"
  74. 3710    PRINT " 255 LET C = A(i)"
  75. 3720    PRINT " 260 NEXT I"
  76. 3730    PRINT " 270"
  77. 3740    PRINT FNPOSCUR$(10,25); "<< In appropriate lingo tell the computer where to go."
  78. 3750    PRINT FNPOSCUR$(10,6);
  79. 3760    GOSUB 40900 : GOSUB 40600 : U$ = CVTSTR$
  80. 3770    IF U$ = "GOSUB 200" THEN 3830
  81. 3780    IF U$ = "?" THEN 3810
  82. 3790    PRINT FNPOSCUR$(10,25);ELR$;"<< Try again or press'?' for answer."
  83. 3800    GOTO 3750
  84. 3810    PRINT FNPOSCUR$(10,6);"GOSUB 200";ELR$;" ' this is the only appropriate command."
  85. 3820    GOTO 3840
  86. 3830    PRINT VT$;CR$;ELR$;" 140 GOSUB 200           Right! 200 starts the subroutine."
  87. 3840    PRINT FNPOSCUR$(23,25);"<< What should go here?";CR$;
  88. 3850    PRINT FNPOSCUR$(23,6);
  89. 3860    GOSUB 40900 : GOSUB 40600 : L$ = CVTSTR$
  90. 3870    IF L$ = "RETURN" THEN  PRINT VT$;CR$;ELR$;" 270 RETURN"; CRLF$ :
  91.         PRINT T10$;"Right on!" : GOTO 3910
  92. 3880    PRINT VT$;CR$;ELR$;" 270 RETURN"
  93. 3890    PRINT
  94. 3900    PRINT T10$;"   It's 'RETURN'.  See how it's done?"
  95. 3910    PRINT T10$;"The computer will return control to line 150. All basic sub-"
  96. 3920    PRINT T10$;"routines SHOULD terminate with a 'RETURN' statement.";CRLF$
  97. 3930    PRINT T10$;"While MBASIC doesn't require an 'END' statement, it is" 
  98. 3940    PRINT T10$;"always good practice to include it at the finish of your"
  99. 3950    PRINT T10$;"main program or at the finish of the entire program."
  100. 3960    GOSUB 37000 : GOSUB 37100
  101. 3970    GOTO 4080
  102. 3980    PRINT " 200 IF H < .000001 THEN 240"
  103. 3990    PRINT " 210 IF SQR((X2 - X1)^2 + (Y2 - Y1)^2) < .000001 THEN 260"
  104. 4000    PRINT " 215 ' proper formula
  105. 4010    PRINT " 220 LET R = .5 * H + .25 / H * ((X2 - X1)^2 + (X2 - Y1)^2)"
  106. 4020    PRINT " 230 GOTO 270"
  107. 4030    PRINT " 240 PRINT "; CHR$(34); "Crown height cannot be zero"; CHR$(34)
  108. 4040    PRINT " 250 GOTO 270"
  109. 4050    PRINT " 260 PRINT "; CHR$(34); "You have defined only one point"; CHR$(34)
  110. 4060    PRINT " 270"
  111. 4070    GOTO 3740
  112. 4080    PRINT
  113. 4090    PRINT T10$;"   If the subroutine occurs between the main program and the"
  114. 4100    PRINT T10$;"'END' statement, a 'STOP' statement may also be needed to"
  115. 4110    PRINT T10$;"avoid a 'nonsense' output. A 'STOP' statement is equivalent"
  116. 4120    PRINT T10$;"to an 'END' statement and returns you to MBASIC command level."
  117. 4130    PRINT T10$;"So the final program could in general be:"
  118. 4140    PRINT
  119. 4150    PRINT "100 (main program begins)"
  120. 4160    PRINT " .   ...."
  121. 4170    PRINT "140 GOSUB 200"
  122. 4180    PRINT " . (main program resumes)"
  123. 4190    PRINT " .   ...."
  124. 4200    PRINT "190 STOP               ' (One could also say GOTO 300)"
  125. 4210    PRINT "200 (subroutine begins)"
  126. 4220    PRINT " .   ...."
  127. 4230    PRINT "270 RETURN"
  128. 4240    PRINT "300 END"
  129. 4250    PRINT T10$;"   I hope you will try using subroutines in your own programs,"
  130. 4260    PRINT T10$;"so that you can become proficient at writing them."
  131. 4270    GOSUB 37000 : GOSUB 37100
  132. 4280    PRINT T10$;"   There is an easy way of adding general info to your program."
  133. 4290    PRINT T10$;"the computer will not execute (but it will list) the statement."
  134. 4300    PRINT T10$;"Perhaps you've already noticed the use of REM and ' in this"
  135. 4310    PRINT T10$;"lesson. Here are some examples of a remark:"
  136. 4320    PRINT
  137. 4330    PRINT "460 REM Title: Convert to Uppercase Subroutine -- A$ is argument"
  138. 4340    PRINT "470 ' initialize variables
  139. 4350    PRINT "480 LET LOW = 97 : LET HIGH = 122 ' set range "
  140. 4360    PRINT "490 FOR I = 1 TO LEN(A$) : REM determine number of iterations"
  141. 4370    PRINT
  142. 4380    PRINT T10$;"   Note that a statement number is required. Mbasic ignores"
  143. 4390    PRINT T10$;"everything on the line following the REM or '. Obviously"
  144. 4400    PRINT T10$;"the purpose of 'REM' is to enable a programmer to put comments"
  145. 4410    PRINT T10$;"throughout his program so that others can understand what is"
  146. 4420    PRINT T10$;"taking place. (After a while even the original programmer may"
  147. 4430    PRINT T10$;"forget how he got his results)";CRLF$
  148. 4440    PRINT T10$;"Your next lesson is #14. See you soon!"
  149. 4450    PRINT CRLF$;FNCENTER$("press any key for menu");
  150. 4460    FOR CT = 1 TO 22 : PRINT CHR$(8); : NEXT CT : GOSUB 40800
  151. 4470    GOTO 39999
  152.  
  153. 37000    'press any key pause
  154. 37010    PRINT T10$;CRLF$;CRLF$;FNCENTER$("press any key to continue");
  155. 37020    FOR CT= 1 TO 25 : PRINT CHR$(8); : NEXT CT
  156. 37030    GOSUB 40800
  157. 37099    RETURN
  158.  
  159. 37100    ' heading
  160. 37110    PRINT CLS$;FNPOSCUR$(4,1);FNCENTER$("Tutor -- Lesson 13");CRLF$
  161. 37199    RETURN
  162.  
  163. 38000    ' converted for Non-linear Systems, Kaypro Educational Division
  164. 38010    ' by t.crew , Simple Software, San Diego CA
  165. 38020    ' has to be run with mentutr to get all variables
  166. 38030    ' tutor lesson 13 version 2.0  released to nls 1 aug 83
  167.  
  168. 39999    RUN "MENU" 'end of overlay
  169.  
  170. utr to get all variables
  171. 38030    ' tutor