CHAPTER 10

SUBROUTINES


Let's try another guessing game:

10 REM ANOTHER GUESSING GAME
20 INPUT "WHO IS BURIED IN GRANT'S TOMB";GR$
30 IF GR$<>"GRANT" THEN 60
40 GOSUB 200
50 GOTO 70
60 PRINT "WRONG. THE ANSWER IS GRANT."
70 INPUT "WHAT COLOR IS AN ORANGE";OJ$
80 IF OJ$<>"ORANGE" THEN 110
90 GOSUB 200
100 GOTO 120
110 PRINT "NO, NO, NO! ORANGES ARE ORANGE."
120 INPUT "WHO WROTE BEETHOVEN'S FIFTH";BN$
130 IF BN$<>"BEETHOVEN" THEN 160
140 GOSUB 200
150 GOTO 170
160 PRINT "HEACENS, NO! IT WAS BEETHOVEN."
170 PRINT
180 PRINT "CORRECT RESPONSES =";X
190 END
200 REM SUBROUTINE
210 X=X+1
220 PRINT "CONGRATULATIONS! HAVE";X;"CIGAR";
230 IF X>1 THEN PRINT "S";
240 PRINT "."
250 RETURN
RUN

WHO IS BURIED IN GRANT'S TOMB? GRANT
CONGRATULATIONS! HAVE 1 CIGAR.
WHAT COLOR IS AN ORANGE? ORANGE
CONGRATULATIONS! HAVE 2 CIGARS.
WHO WROTE BEETHOVEN'S FIFTH? BEETHOVEN
CONGRATULATIONS! HAVE 3 CIGARS.

CORRECT RESPONSES = 3

Lines 200 to 250 are a subroutine-- a subprogram of a larger program. You should use a subroutine: whenever there is a complex action you want to perform repeatedly in different sections of a program. In the program above, the Sorcerer goes to the subroutine as many as three times. If you wish to keep track of how many times a subroutine is used, you may put a counter in it (X, above).

Use the GOSUB statement to send the Sorcerer to a subroutine The format is:

GOSUB<line number>

where <line number> is the number of the first line of the subroutine.

GOSUB is a lot like GOTO, with one big exception. When you send the Sorcerer somewhere with a GOTO, it forgets where it was when you sent it (that is, it forgets the line number of the GOTO instruction). But when the Sorcerer sees a GOSUB instruction, it remembers where it came from. When it finishes the subroutine, it goes right back to the next statement after the GOSUB. If there are several GOSUBs to the same subroutine, the Sorcerer always goes back to the right one--it can't do that with a GOTO instruction.

(Note that, while you may put a space into GOTO and use GO TO, if you try to use GO SUB instead of GOSUB, you get the SN error message (SYNTAX ERROR).)

The statement RETURN sends the Sorcerer out of the subroutine, back to its proper place in the main program. The last line of your subroutine should be a RETURN statement, and you can have RETURN statements elsewhere in the subroutine (as part of an IF...THEN branch, for instance).

Example:

10 REM A PROGRAM HAVING A SUBROUTINE WITH MORE THAN ONE RETURN
20 X=0:REM INITIALIZATION
30 PRINT
40 X=X+1
50 IF X>3 THEN 200
60 PRINT "LET'S JUMP TO THE SUBROUTINE."
70 GOSUB 100
80 GOTO 30
100 REM SUBROUTINE
110 PRINT " NOW WE'RE IN THE SUBROUTINE, THE ";
120 ON X GOTO 130, 150, 170
130 PRINT "FIRST TIME THROUGH."
140 RETURN
150 PRINT "SECOND TIME THROUGH."
160 RETURN
170 PRINT "THIRD TIME THROUGH."
180 RETURN
200 PRINT "NOW WE'RE DONE."
RUN

LET'S JUMP TO THE SUBROUTINE.
NOW WE'RE IN THE SUBROUTINE,
THE FIRST TIME THROUGHT.
LET'S JUMP TO THE SUBROUTINE.
NOW WE'RE IN THE SUBROUTINE,
THE SECOND TIME THROUGHT.
LET'S JUMP TO THE SUBROUTINE.
NOW WE'RE IN THE SUBROUTINE,
THE THIRD TIME THROUGHT.
NOW WE'RE DONE.

A subroutine can call other subroutines if you wish; the only limit to this is the capacity of the Sorcerer's memory. But when the Sorcerer sees a RETURN statement, it always goes straight back to the instruction after the most recently encountered GOSUB.

Place subroutines together, so that you can find them easily when you look for them in a program. Then put a STOP or END statement just before the first subroutine, or use a GOTO to send the program away from the subroutine. This will prevent the Sorcerer from "falling" into the subroutine when it finishes the main program.


THE CONDITIONAL GOSUB


The format for this statement is:

ON <numerical expression> GOSUB <list of line numbers>

Here, the line numbers in the list are all the first line numbers of subroutines (each ending with a RETURN). The ON...GOSUB statement works the same way as ON...GOTO, except that when the Sorcerer finishes one of the subroutines it goes straight to the instruction following the ON...GOSUB.

Example:

10 REM DEMONSTRATING THE ON...GOSUB STATEMENT
20 PRINT
30 FOR A=1 TO 3
40 PRINT "MAIN BODY"
50 ON A GOSUB 80, 110, 140
60 NEXT A
70 PRINT "END OF PROGRAM":END
80 REM FIRST SUBROUTINE
90 PRINT " FIRST SUBROUTINE"
100 RETURN
110 REM SECOND SUBROUTINE
120 PRINT " SECOND SUBROUTINE"
130 RETURN
140 REM THIRD SUBROUTINE
150 REM IT CONTAINS ITS OWN "NESTED" SUBROUTINE
160 PRINT " BEGINNING OF THIRD SUBROUTINE"
170 GOSUB 210
180 PRINT " END OF THIRD SUBROUTINE"
190 RETURN
200 REM STATMENT 190 RETURNS CONTROL TO STATEMENT 60
210 REM "NESTED" SUBROUTINE
220 PRINT " 'NESTED' SUBROUTINE"
230 RETURN
240 REM STATEMENT 230 RETURNS CONTROL TO STATEMENT 190
RUN

MAIN BODY
FIRST SUBROUTINE
MAIN BODY
SECOND SUBROUTINE
MAIN BODY
BEGINNING OF THIRD SUBROUTINE
'NEST' SUBROUTINE
END OF THIRD SUBROUTINE
END OF PROGRAM

Table of Contents | Prev | Next

The Trailing Edge