boomerang
when the
Computer hits a RETURN. The
GOSUB
is not completed
and
will not move
onto the
next program line until a RETURN
is
found.
Lines
30800
through
30840
contain this rather
simple
subroutine.
Line
30840
contains
the RETURN which
sends control back
to
line
20,
which
silently
acknowledges the return
and
allows
movement
to
the next
line.
Line
30
is an
ordinary
ON-GOTO statement,
but adds 2 to
the
value
of its
variable,
in
this
case "T".
Line
3(9
is really
saying, "If T is
-1
then GOTO
line
50.
If it
is zero
then
GOTO line
60,
and if it is
+1
GOTO line
70.
By adding 2
to
each of
those values
we
have "matched" them up with the
1, 2,
and 3 which are built
into the
ON-GOTO.
Lines
45, 55,
and
65 are routine protective blocks.
Preview
of Coming
Attractions?
Like so much of what
we
are
learning,
this is just the
tip of
the iceberg. The
ON-GOTO and
SGN
functions
have
many more clever applications,
and
they
will evolve
as we need
them.
As
a hint for
restless
minds,
note
that
the value
of
X
which
we input was not used, but it didn't
go away. All we did was find its SGN.
Hmmm
. . .
Routines vs
SUBroutines
We studied
a
special-purpose
routine
used as a subroutine. It is one of the few
that we
can
both use and
really
understand. All the routines, understandable
or
not,
can be built directly
into any program instead
of being
set aside and "called" as subroutines. Their main value
as subroutines is that they can be
"called"
repeatedly from different parts of a program,
which is
often desirable.
As
ordinary routines they are usually only used once, and lines
containing GOSUB and RETURN are not needed.
One value of using special routines as SUBroutines is that some are
exceedingly complex
to
type
without
error, and if each is typed once and saved
on
cassette tape, it can be
quickly
and
accurately loaded
into the Computer as the first step
in
creating a new program.
Another
good idea is to type all the subroutines at one
time, then
record on one tape. You
can
later load that tape and erase out of the Computer
those subroutines that are not need-
ed for the program
you
are creating.
Now it's your turn.
EXERCISE
15-1:
Delete
lines
30800-3084QI
from the resident program. Build the SGN
routine
into the program so it works just as well
as
if
we were calling it as a SUBroutine.
This
problem
will
probably take you quite a few lines
—
so rather than give you a
lot
of
blank
space here, why not take advantage
of
the pad
of
Program sheets
we've
given you and
write
your program answer
there. Then check Part
B for our suggested answer.
82
By the way,
most
subroutines
are
not this simple
-
as
a
matter of
fact, they
get
into
rather hairy
mathematical
derivations.
We won't
bother
faying
to explain any
of them
—
if you're one
of
those
Math
nuts, you
gor^htahead
andp&ywith
iiie
numbers.
.',
Well hav&
iftOie to sayin a
later Chapter. Whan
you see just how powerful
subroutines
are,
you^t
;
feel
like your TRS-80
is.
even
smarterthanit
•'.'.
thinks
it is (blush,
blush)?