Hardware Reference
In-Depth Information
Subroutines
Subroutines can be instrumental in making very significant reductions in the
size of BASIC programs and they should be employed whenever a section of
code is to be executed more than once. Note, however, that if your version of
BASIC supports the use of subprograms, procedures or user-defined functions,
then these should normally be used instead! A typical example of the use of
a subroutine might involve a delay routine which is required at various points
in a program. Assuming that such a routine was to be used in an early version
of BASIC which employs line numbers and that the subroutine starts at line
10100, it might take the following form:
10100 REM Delay subroutine
10110 FOR c%=0TO10000: NEXT e%
10120 RETURN
The subroutine may be called from several points within the main program as
follows:
340
350 GOSUB 10100
360
...
440
450 GOSUB 10100
460
...
710
720 GOSUB 10100
730
In each case, program execution resumes at the line immediately following the
GOSUB statement. Also note that, on exit from the subroutine, c% will have the
value 10001.
We could make the delay subroutine even more flexible (allowing for variable
length delays) by altering the upper limit of the loop using a variable which is
set immediately prior to the subroutine call. The modified subroutine would
then become:
10100 REM Delay subroutine
10110 FOR c%=0TOlim%: NEXT c%
10120 RETURN
As before, the routine may be called from several points in the main program
as follows:
340
350 lim% = 10000: GOSUB 10100
360
...
440
450 lim% = 20000: GOSUB 10100
460
...
710
720 lim% = 15000: GOSUB 10100
730
Search WWH ::




Custom Search