EasyManua.ls Logo

Radio Shack TRS-80 - Page 83

Radio Shack TRS-80
236 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
"Calling"
a
Subroutine
(Sort
of
like calling
hogs.)
When
you
want to
use a
subroutine, use
the
GOSUB#####
statement.
This
directs
the
Computer to
go to that
line number,
execute what it says
there
and in the
lines following,
and when
done
RETURN
back to the line
containing
the GOSUB
state-
ment. We
will
use line
20
here.
20
GOSUB
30800
A
RETURN is
always
built
into a
subroutine,
and
you'll find it at
line
30840.
We
have
reserved
line
number
30000
to
hold
a
protective
END
block for all of
our
subroutines, so
the
Computer
doesn't
come crashing into
them
when
it
is
done
with the main program.
Getting
Down to
Business
Okay, now let's
combine GOSUB and SGN
(using a subroutine)
to
see
what
all this fuss is
about. Type:
10 INPUT
"TYPE ANY NUMBER" ;X
20 GOSUB
30800
30 ON T+2 GOTO
50,60,70
45
END
50 PRINT
"THE NUMBER IS
NEGATIVE."
55
END
60
PRINT "THE
NUMBER IS
ZERO."
65
END
70
PRINT
"THE NUMBER IS
POSITIVE."
. ,
.
etc.
(the
subroutine is already typed
in)
.
.
.
and
RUN.
Try entering
negative, zero and
positive
numbers
to
be sure it works.
Most of
the program
is already
obvious to you,
but here
is
an
analysis:
Line
10
inputs any
number.
Line
20
sends the
Computer
to line
30800
by a GOSUB
statement.
This is
different from
an ordinary GOTO, since
a
GOSUB
will return control
to
the originating
line
like a
.;;:.
'."##.##.#"
represents
the line number, All
at o«r
appendix
A subroutines use line
:..y..
*
.
:
. \
..'"',
'".?V.
^~y;
!;'
--y^r.'.'.--
1
^::'^"--*
-
-''•
81

Other manuals for Radio Shack TRS-80

Related product manuals