The SGN function
examines any number to see whether it is
negative, zero,
or
positive.
It
tells us the
number
is negative by giving
us
a
(—1).
If the number is zero
it
gives us a
(0).
If
positive, we get a
(+1).
It's
a very simple function.
First,
the BAD
News
Unfortunately,
LEVEL
I
BASIC does
not have the SGN function built-in.
Then,
the GOOD
News
Fortunately, through the use of
a
computer (yours) it is
possible
to
create or
simulate func-
tions
we
don't have. That's why
Appendix
A is full
of
good
things
called SUBROUTINES.
So
What Is a
Subroutine?
Funny you
should ask. A sub-routine is a short but very specialized program (or routine)
which
you
build into a large program to meet a specialized need. LEVEL II BASIC stores
many of
them
in
a
special place in memory and they can be called up by a simple set
of
letters.
{We
have several at LEVEL I, like INT.)
We
don't
have enough memory to spare here at LEVEL I to hold all the routines in
memory, so we
are going to use
a
five-line subroutine
instead of the
"SGN"
function to
accomplish
the
same
thing. Even if you have LEVEL II BASIC in your computer, you
should
complete
this Chapter to be sure
you
learn about subroutines. We
don't want
to
turn out
dummies, you know.
Turn to Appendix A. Find the subroutine
marked
SGN. "Scratch" the program now in the
computer by typing
NEW,
then
—
very carefully, so you don't make
any mistakes,
type
in
the SGN subroutine:
30000
END
30800 REM
*
SGN(X)
*
INPUT X, OUTPUT T
30810 IF X<0 THEN T
=
-1
30820 IF
X
=
THEN
T
=
30830 IF X >0 THEN T
=
-1-
1
30840
RETURN
=
-1,0, OR +1
You can only get
so many
people
in a
telephone
booth. {There b
supposed to
be
an
analogy
of
sorts
there.)
80
"CAREFULLY"