30
GOSUB
1030
40
GOSUB
1040
50
GOSUB
1050
: :
: :
30
GOTO
10
1010
INPUT
L
1015
RETURN
1020
INPUT
S
1025
RETURN
1030
INPUT
D
1035
RETURN
1040
PRINT
" ";L;"..";S;"S..";D;"D";
TAB
8; "=";
1045
RETURN
1050
LET
D=240*L+12*S+D
1055
RETURN
: :
: :
The first, with its single powerful subroutine, & the second, with its many trivial ones, demonstrate quite
opposite extremes, but with equal futility.
A subroutine can happily call another, or even itself (a subroutine that calls itself is recursive), so don't
be afrid to having several layers.
Summary
Statements:
GOSUB
,
RETURN
Exercises
1. The example program is virtually a universal LSD calculator. How would you use it.
(i) to convert pounds & new pence into pounds, shillings & pence?
(ii) to convert guineas into pounds & shillings? (1 guinea = œ1..1s)
(iii) to find fractions of a pound? (e.g. a third of a pound, or a mark, is 6s..8d.
Put in a line to round the pence off to the nearest farthing (1/4d).
2. Add two statements to the program:
4
LET
ADJUST=1000
7
LET
LSDPRINT=2000
& change
GOSUB
1000 to
GOSUB
ADJUST
GOSUB
2000 to
GOSUB
LSDPRINT
This works exactly as you'd hope; in fact the line number in a
GOSUB
(or
GOTO
or
RUN
) statement can
be any numerical expression. (Don't expect this to work on computers other than the ZX81, because it is
not standard BASIC.)
This sort of stuff can work wonders for the clarity of your programs.
3. Rewrite the main program in the example to do something quite different, but still using the same
subroutines.
4. ...
GOSUB
n
...
RETURN
in consecutive lines can be replaced by
...
GOTO
n
Why?