Chapter 14 - Subroutines
Sometimes different parts of your program will have rather similar jobs to do, & you will find yourslef typing
the same lines in twice or more; however this is not necessary. You can type the lines in once, in the form
known as a subroutine
, & then use, or call, them anywhere else in the program without having to type them
in again.
To do this, you use the statements
GOSUB
(GO to SUBroutine) &
RETURN
.
GOSUB
n
where n is the line number of the first line in the subroutine, is just like
GOTO
n except that the computer
remembers the line number of the
GOSUB
statement so that it can come back again after doing the
subroutine. It does this remembering by putting the line number (the return address) on top of a pie of them
(the GOSUB stack).
RETURN
takes the top line number off the GOSUB stack, & goes to the line after it.
As a first example,
10
PRINT
"THIS IS THE MAIN PROGRAM",
20
GOSUB
1000
30
PRINT
"AND AGAIN";
40
GOSUB
1000
50
PRINT
"AND THAT IS ALL."
60
STOP
1000
REM
SUBROUTINE STARTS HERE
1010
PRINT
"THIS IS THE SUBROUTINE,"
1020
RETURN
The
STOP
statement in line 60 is very important because otherwise the program will run on into the
subroutine & cause error 7 when the
RETURN
statement is reached.
For a less trivial example, suppose you want to write a computer program to handle pounds, shillings
and pence. Those with long memories will remember that before 1971 a pound was divided into twenty
shillings - so a shilling is 5p - & a shilling was subdivided into twelve old pence; d was the abbreviation for
an old penny.) You will have three variables L, S & D (any maybe others - L1, S1, D1 & so on), and
arithmetic is dead easy. First you do it seperately on the pounds, shillings and pence - for instance, to add
two sums of money, you add the pance, add the shillings and add the pounds; to double a sum of money
you double the pence, double the shillings and double the pounds; and so on. When all that is done, adjust
it to the correct form so that the pence are between 0 & 11, and the shillings between 0 & 19. This last
stage is common to all the operations, so we can make it into a subroutine.
Laying aside the notion of subroutines for a moment, it is worth your while trying to write the program
yourself. Give the arbitrary numbers L, S
& D, how do you convert them into proper pounds, shillings & pence? Part of the problem is that you will
start thinking of odder & odder cases.
What first springs to mind will probably be something like œ1..25s..17d, which you want to convert to
œ2..6s..5d. Not so difficult. But suppose you have negative numbers? A dept of œ1..25s..17d, or œ-1..-
25s..-17d, might well turn out as œ-3..13s..7d, which is rather an odd way of expressing it (as though
people only ever lend each other whole pounds). And what about fractions? If you divide œ1..25s..17d by
two, you get œ5..12.5s..8.5d, & although this has the pence, 8.5, between 0 & 11; the shillings, 12.5,
between 0 & 19, it is certainly not as good as œ1..3s..2.5d. Try & work out your own answers to all this - &
use them in a computer program - before you read any further.
Here is one solution.
1000
REM
SUBROUTINE TO ADJUST L.S.D. TO THE NORMAL FORM FOR POUNDS, SHILLINGS
AND PENCE
1010
LET
D=240*L+12*S+D
1020
REM
NOW EVERYTHING IS IN PENCE
1030
LET
E=
SGN
D
1040
LET
D=
ABS
D
1050
REM
WE WORK WITH D POSITIVE, HOLDING ITS SIGN IN E