home *** CD-ROM | disk | FTP | other *** search
- 3000 ' tutor 13
- 3010 GOSUB 37100
- 3020 T5$ = SPACE$(5) : T10$ = SPACE$(10) : T15$ = SPACE$(15) :VT$ = CHR$(11)
- 3030 PRINT T10$;" A subroutine may be thought of as a program within a program."
- 3040 PRINT T10$;"The standard functions are usually subroutines, since when one"
- 3050 PRINT T10$;"of them is called for, the machine substitutes the argument into"
- 3060 PRINT T10$;"a series (sum of terms) to evaluate the function. We may write"
- 3070 PRINT T10$;"our own subroutine for a process to be done more than once in"
- 3080 PRINT T10$;"our main program. The subroutine will be executed when the main"
- 3090 PRINT T10$;"program transfers control to it. Note these expressions:"
- 3100 PRINT
- 3110 PRINT T10$;" (1) gosub (2) dosub (3) gofor (4) routine"
- 3120 PRINT
- 3130 PRINT T10$;"Which looks like it might call in a subroutine? ";
- 3140 GOSUB 40900 : S$ = CVTSTR$
- 3150 PRINT
- 3160 IF S$ = "1" OR S$ = "GOSUB" THEN 3220
- 3170 IF (S$ = "2" OR S$ = "3" OR S$ = "4") OR
- (S$ = "DOSUB" OR S$ = "GOFOR" OR S$ = "ROUTINE")
- THEN 3200
- 3180 PRINT ELR$;T10$;"Wait a minute, now. That's not an offered choice.";VT$;VT$;CR$;
- 3190 GOTO 3130
- 3200 PRINT ELR$;T10$;"It doesn't, though. Try another one, ok";VT$;VT$;CR$;
- 3210 GOTO 3130
- 3220 PRINT VT$;VT$;CR$;EES$;
- 3230 PRINT T10$;" That's it. For instance, 'gosub 400' means 'go to the program"
- 3240 PRINT T10$;"(subroutine) which begins on line 400'. After the subprogram"
- 3250 PRINT T10$;"has been executed, control is returned to the statement after"
- 3260 PRINT T10$;"the one which called for the subroutine. How is this done:"
- 3270 PRINT
- 3280 PRINT T10$;" (1) goback (2) main (3) return (4) go to"
- 3290 PRINT
- 3300 PRINT T10$;"do you think? ";
- 3310 GOSUB 40900 : R$ = CVTSTR$
- 3320 PRINT
- 3330 IF R$ = "3" OR R$ = "RETURN" THEN 3360
- 3340 PRINT T10$;"You haven't guessed it yet. Try again.";VT$;VT$;CR$;
- 3350 GOTO 3300
- 3360 PRINT VT$;VT$;CR$;
- 3370 PRINT T10$;" You've got it. You use this one as the last statement in"
- 3380 PRINT T10$;"your subprogram to RETURN to the point where you left the main"
- 3390 PRINT T10$;"program. The next statement in the main program is then executed."
- 3400 PRINT T10$;"We'll try an example. Do you prefer a (1) geometry or (2) logic"
- 3410 PRINT T10$;"subprogram as an illustration? ";
- 3420 GOSUB 40900 : E$ = CVTSTR$
- 3430 PRINT
- 3440 IF E$ = "1" OR LEFT$(E$,2) = "GE" THEN E$ = "1" : GOSUB 37100 : GOTO 3510
- 3450 IF E$ = "2" OR LEFT$(E$,2) = "LO" THEN GOSUB 37100 : GOTO 3480
- 3460 PRINT T10$;"We don't have that one. Is it '1' or '2'?";VT$;VT$;CR$;
- 3470 GOTO 3410
- 3480 PRINT T10$;" Here's one that will give us the maximum and minimum values of"
- 3490 PRINT T10$;"n numbers, assuming no two numbers are equal. I'll leave one of"
- 3500 GOTO 3530
- 3510 PRINT T10$;" Here's one to compute the radius of a circle, given two points"
- 3520 PRINT T10$;"(x1,y1) and (x2,y2) plus the crown height h. I'll leave one of"
- 3530 PRINT T10$;"the main problem statements blank; you fill it in correctly"
- 3540 PRINT T10$;"so that it will call in our subroutine."
- 3550 PRINT
- 3560 FOR J = 100 TO 130 STEP 10
- 3570 PRINT J; "' main prog. line"
- 3580 NEXT J
- 3590 PRINT J
- 3600 FOR J = 150 TO 170 STEP 10
- 3610 PRINT J; "' main prog. line"
- 3620 NEXT J
- 3630 PRINT J;"REM the next 8 statements constitute the subroutine"
- 3640 IF E$ = "1" THEN 3980
- 3650 PRINT " 200 LET B = A(1)"
- 3660 PRINT " 210 LET C = A(1)"
- 3670 PRINT " 220 FOR I = 2 to N"
- 3680 PRINT " 230 IF A(i) <= B THEN 250"
- 3690 PRINT " 240 LET B = A(i)"
- 3700 PRINT " 250 IF A(i) >= C THEN 260"
- 3710 PRINT " 255 LET C = A(i)"
- 3720 PRINT " 260 NEXT I"
- 3730 PRINT " 270"
- 3740 PRINT FNPOSCUR$(10,25); "<< In appropriate lingo tell the computer where to go."
- 3750 PRINT FNPOSCUR$(10,6);
- 3760 GOSUB 40900 : GOSUB 40600 : U$ = CVTSTR$
- 3770 IF U$ = "GOSUB 200" THEN 3830
- 3780 IF U$ = "?" THEN 3810
- 3790 PRINT FNPOSCUR$(10,25);ELR$;"<< Try again or press'?' for answer."
- 3800 GOTO 3750
- 3810 PRINT FNPOSCUR$(10,6);"GOSUB 200";ELR$;" ' this is the only appropriate command."
- 3820 GOTO 3840
- 3830 PRINT VT$;CR$;ELR$;" 140 GOSUB 200 Right! 200 starts the subroutine."
- 3840 PRINT FNPOSCUR$(23,25);"<< What should go here?";CR$;
- 3850 PRINT FNPOSCUR$(23,6);
- 3860 GOSUB 40900 : GOSUB 40600 : L$ = CVTSTR$
- 3870 IF L$ = "RETURN" THEN PRINT VT$;CR$;ELR$;" 270 RETURN"; CRLF$ :
- PRINT T10$;"Right on!" : GOTO 3910
- 3880 PRINT VT$;CR$;ELR$;" 270 RETURN"
- 3890 PRINT
- 3900 PRINT T10$;" It's 'RETURN'. See how it's done?"
- 3910 PRINT T10$;"The computer will return control to line 150. All basic sub-"
- 3920 PRINT T10$;"routines SHOULD terminate with a 'RETURN' statement.";CRLF$
- 3930 PRINT T10$;"While MBASIC doesn't require an 'END' statement, it is"
- 3940 PRINT T10$;"always good practice to include it at the finish of your"
- 3950 PRINT T10$;"main program or at the finish of the entire program."
- 3960 GOSUB 37000 : GOSUB 37100
- 3970 GOTO 4080
- 3980 PRINT " 200 IF H < .000001 THEN 240"
- 3990 PRINT " 210 IF SQR((X2 - X1)^2 + (Y2 - Y1)^2) < .000001 THEN 260"
- 4000 PRINT " 215 ' proper formula
- 4010 PRINT " 220 LET R = .5 * H + .25 / H * ((X2 - X1)^2 + (X2 - Y1)^2)"
- 4020 PRINT " 230 GOTO 270"
- 4030 PRINT " 240 PRINT "; CHR$(34); "Crown height cannot be zero"; CHR$(34)
- 4040 PRINT " 250 GOTO 270"
- 4050 PRINT " 260 PRINT "; CHR$(34); "You have defined only one point"; CHR$(34)
- 4060 PRINT " 270"
- 4070 GOTO 3740
- 4080 PRINT
- 4090 PRINT T10$;" If the subroutine occurs between the main program and the"
- 4100 PRINT T10$;"'END' statement, a 'STOP' statement may also be needed to"
- 4110 PRINT T10$;"avoid a 'nonsense' output. A 'STOP' statement is equivalent"
- 4120 PRINT T10$;"to an 'END' statement and returns you to MBASIC command level."
- 4130 PRINT T10$;"So the final program could in general be:"
- 4140 PRINT
- 4150 PRINT "100 (main program begins)"
- 4160 PRINT " . ...."
- 4170 PRINT "140 GOSUB 200"
- 4180 PRINT " . (main program resumes)"
- 4190 PRINT " . ...."
- 4200 PRINT "190 STOP ' (One could also say GOTO 300)"
- 4210 PRINT "200 (subroutine begins)"
- 4220 PRINT " . ...."
- 4230 PRINT "270 RETURN"
- 4240 PRINT "300 END"
- 4250 PRINT T10$;" I hope you will try using subroutines in your own programs,"
- 4260 PRINT T10$;"so that you can become proficient at writing them."
- 4270 GOSUB 37000 : GOSUB 37100
- 4280 PRINT T10$;" There is an easy way of adding general info to your program."
- 4290 PRINT T10$;"the computer will not execute (but it will list) the statement."
- 4300 PRINT T10$;"Perhaps you've already noticed the use of REM and ' in this"
- 4310 PRINT T10$;"lesson. Here are some examples of a remark:"
- 4320 PRINT
- 4330 PRINT "460 REM Title: Convert to Uppercase Subroutine -- A$ is argument"
- 4340 PRINT "470 ' initialize variables
- 4350 PRINT "480 LET LOW = 97 : LET HIGH = 122 ' set range "
- 4360 PRINT "490 FOR I = 1 TO LEN(A$) : REM determine number of iterations"
- 4370 PRINT
- 4380 PRINT T10$;" Note that a statement number is required. Mbasic ignores"
- 4390 PRINT T10$;"everything on the line following the REM or '. Obviously"
- 4400 PRINT T10$;"the purpose of 'REM' is to enable a programmer to put comments"
- 4410 PRINT T10$;"throughout his program so that others can understand what is"
- 4420 PRINT T10$;"taking place. (After a while even the original programmer may"
- 4430 PRINT T10$;"forget how he got his results)";CRLF$
- 4440 PRINT T10$;"Your next lesson is #14. See you soon!"
- 4450 PRINT CRLF$;FNCENTER$("press any key for menu");
- 4460 FOR CT = 1 TO 22 : PRINT CHR$(8); : NEXT CT : GOSUB 40800
- 4470 GOTO 39999
-
- 37000 'press any key pause
- 37010 PRINT T10$;CRLF$;CRLF$;FNCENTER$("press any key to continue");
- 37020 FOR CT= 1 TO 25 : PRINT CHR$(8); : NEXT CT
- 37030 GOSUB 40800
- 37099 RETURN
-
- 37100 ' heading
- 37110 PRINT CLS$;FNPOSCUR$(4,1);FNCENTER$("Tutor -- Lesson 13");CRLF$
- 37199 RETURN
-
- 38000 ' converted for Non-linear Systems, Kaypro Educational Division
- 38010 ' by t.crew , Simple Software, San Diego CA
- 38020 ' has to be run with mentutr to get all variables
- 38030 ' tutor lesson 13 version 2.0 released to nls 1 aug 83
-
- 39999 RUN "MENU" 'end of overlay
-
- utr to get all variables
- 38030 ' tutor