CHAPTER
15
PROCEDURES
AND
FUNCTIONS
VALUE
PARAMETERS
In
the
first
part
ot
this chapter
we
explain
the
more stralghtlorward
features
of
SuperBtlSIC's
procedures and functions.
We
do
this with very simple examples
so
that you can
understand the working of each feature
as
it
IS
described. Though the examples are
simple and contrived
you
will
appreciate that once understood,
the
Ideas can be applied
in
more complex situations where they really matter
Aher the first part there
IS
a discussion which attempts
to
explain Why procedures' .
If
you
understand, more or
less,
up
to
that
pOint
you
will
be doing
well
and you should
be able
to
use procedures and functions with Increasing effectiveness.
SuperBASIC
first
allows you
to
do the simpler things
in
simple
ways
and then offers you
more
If
you
want
It.
Extra
facilities and some technical matters are explained
in
the second
part
of
thiS
chapter but
you
could
Omit
these,
certainly
at
a
first
reading, and
still
be
In
a stronger position than most users of older types of BASIC
You
have seen
in
previous chapters how a value can be passed
to
a procedure. Here
is
another example.
•
•
EXAMPLE
In
'Chans Chinese
Take-Away"
there are
Just
SIX
Items
on
the menu.
Rice Dishes
1 prawns
2 chicken
3 special
Sweets
4 ice
5 fritter
6 Iychees
Program
Output
Chan has a simple way
of
computing prices. He works
in
pence and the pnces are:
for
a nce dish 300 +
10
times menu number
for a sweet
12
times menu number
Thus a customer who
ate
special nce and an
Ice
would pay:
300
+
10
* 3 +
12
* 4 =
378
pence
A procedure,
Item,
accepts a menu number
as
a value parameter and prints the
cost.
100
REMark
Cost
of
Di
sh
110
item
3
120
item
4
130
DEFine
PROCedure
item(num)
140
IF
num
<=
3
THEN
LET
price
=
300
+ 10*num
150
IF
num
>= 4
THEN
LET
pri
ce = 12*num
160 PRINT
I
price
I
170
END
DEFine
330
48
•
86
In
the
main program actual parameters 3 and 4 are used. The procedure definition has
a formal parameter, num, which takes the value passed
to
it
from the main program.
Note that the formal parameters must be
in
brackets, but that actual parameters need
not
be.
EXAMPLE Now suppose the working variable, pnce, was also used
In
the main program, meaning
something
else,
say
the price of a glass of
lager,
70p.
The following program
fails
to
give the deSired
result.
12184
•