1060
LET
S=
INT
(D/12)
1070
LET
D=(D-12*S)*E
1080
LET
L=
INT
(S/20)*E
1090
LET
S=S*E-20*L
1100
RETURN
On its own, this is not much use because there is no program to set up L, S & d beforehand, nor to do
anything with them afterwards. Type in the main program, & also another subroutine to print out L, S & D.
10
INPUT
L
20
INPUT
S
30
INPUT
D
40
GOSUB
2000
45
REM
PRINT THE VALUES
50
PRINT
60
PRINT
" = ";
70
GOSUB
1000
75
REM
THE ADJUSTMENT
80
GOSUB
2000
85
REM
PRINT THE VALUES
90
PRINT
100
GOTO
10
2000
REM
SUBROUTINE TO PRINT L,S AND D
2010
PRINT
"£";L;"..";S;"S..";D;"D";
2020
RETURN
(Recall from chapter 9 that the empty
PRINT
statement in line 50 prints a black line.)
Clearly we have saved on program by using the printing subroutine at 2000, & this in itself is a very
common use for subroutines: to storten programs. However, the adjustment subroutine in fact makes the
program longer - by a
GOSUB
& a
RETURN
; so program length is not the only consideration. Used with
skill, subroutines can make programs easier to understand for the ones that matter, humans.
The main program is simplified by its using more powerful statements: each
GOSUB
represents some
complicated BASIC, but you can forget that - only the net result matters. Because of this, it is much easier
to grasp the main structure of the program.
The subroutines, on the other hand, are simplified for a very different reason, namely that they are
shorter. They still use the same old plodding
LET
&
PRINT
statements, but they only have to do a part of
the whole jub & so are easier to write.
The skill lies in choosing the level - or levels - at which to write the subroutines. They must be big
enough to have a significant impact on the main program, yet small enough to be significantly easier to
write than a complete program without subroutines. These examples (not
recommended) illustrate this.
First,
10
GOSUB
1000
20
GOTO
10
1000
INPUT
L
1010
INPUT
S
1020
INPUT
D
1030
PRINT
" ";L;"..";S;"S..";D;"D";
TAB
8;"=";
1040
LET
D=240*L+12*S+D
: :
: :
2000
RETURN
& second
10
GOSUB
1010
20
GOSUB
1020