Radio Shack TRS-80 User Manual page 162

Micro computer system
Hide thumbs Also See for TRS-80:
Table of Contents

Advertisement

Turn
to
page
217
of
Appendix
A
and
find
the
Exponentiation
Subroutine.
Type
it
in
slowly
(as
if
we
needed
to say that)
and
carefully.
Now
type
in
the following
"demonstration program".
10
PRINT "SEEKING
THE VALUE OF
X
TO THE
Y
POWER"
20
INPUT
" X =
"
;X
30
INPUT
"
Y
=
"
;
Y
40
GOSUB 30120
50
PRINT "THE ANSWER
IS";P
60
GOTO
10
And RUN
.
(Use [BREAK]
when you
want
to
get
out of
the
program.)
Without
trying to
understand
the
mathematics behind
it,
let's
trace
the
flow
of the
program
control
from main program
to
the various subroutines
and
back.
Lines
20 and 30
provide
values for
X
and
Y.
Line
40
transfers
program
control
from
the
main program
to
the
Exponentiation Sub-
routine
.
Line
30140
calls
the log subroutine to obtain log(X).
Line
30230
returns
L
= log(X) to the
exponentiation
subroutine.
(Note
that control passes
to
the
statement immediately
following the
last
GOSUB
command
even
though
that
state-
ment
is
on
the
same
line in this case.)
Y L
Line
30140
now
calls
the exponentiation subroutine to
compute
e
Finally,
Line
30150
adjusts
the
magnitude
of
P
(don't ask questions!)
and
returns
the
computed
value
P =
X
v
to
the
main
program, Line
50,
for
output.
Now
let's
go
off
on
a
tangent
about
Christmas
trees.
Selecting a
Christmas
tree
in
the
middle
of
a forest
on
a
snowy
evening
in
December
can
be
a trying process. Especially
when
you're seeking a
tree that's
exactly
28
feet
tall
(the tree
is
going
to
be
set
up
in
a
park
downtown).
You
can climb
up
each
tree,
attach a 28-foot
tape
measure, climb back
down
and check
to
see
if
the tape
just
touches
the
ground
and
160
It's
along one -afteryou
type
it
in
and
get-
it
running
properly,
you^l want
to save
it
on
tape
for
lateruse.
Note
that the input
variable
X
is
changed
by
the
subroutine.
Suppose
we
need
this
input value
later
in
the program.
We
can't
refertothis
original
value
by
calling
it
X,
:
because
X
has take
on
a
new
value,
The way
around
this
common
problem
is
to use
a
"dummy
variable" to
hold
the
original
value of
X.
The
dummy
variable
assignment
must
;
be
before
the
entry
to
the subroutine:
'.
35
S-X
Adding
thisline to
our
resident
program
saves
the
value for
later use.
For example,
we
can
now
change
line
59
to
.
%> PRINT
S;.*"I«THE";Y;"=
";P.
.
Watch out
-for
subroutines
which change
the
..-•.
.value-of
theisput
variable.
T&e
lowly
"dummy
variable"
ts
quite
handy
in
such
cases.

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents