•
•
Procedures
and
Functions
180
END
DEFine
190
DEFine
PROCedure
try
200
PRINT
number
210
END
DEFine
Obviously
the
first
number
to
be
printed
will
be
2 but
is
the
variable number
in
line 200
global?
The answer
is
that
the
value
of
number
in
line
160
will
be earned
into
the procedure
try. A variable which
is
local
to
a procedure
will
be the same variable
in
a second
procedure called by
the
first
Equally
if
the procedure try
is
called by
the
main program, the variable number
will
be the same number
In
both the main program and procedure,
try.
The implications
may seem strange
at
first
but they
are
logical.
1.
The variable
number
in
line
110
is
global.
2.
The
variable
number
in
procedure test
is
definitely local
to
the
procedure.
3.
The variable
number
in
procedure try 'belongs'
to
the part
of
the program which
was
the
last
call
to
it
We
have covered many concepts
in
this
chapter because SuperBASIC functions and
procedures
are
very powerful.
However,
you
should not expect
to
use
all
these features
immediately. Use procedures and functions
in
simple
ways
at
first
They
can
be
very
effecllve and the power
is
there
if
you
need
it
1.
2.
•
Six
employees
are
identified by
their
surnames
only.
Each
employee
has
a particular
pension fund
rate
expressed
as
a percentage. The following data represent
the
total
salaries and pension fund
rates
of
the
six
employees.
Benson
13,800
6.25
Hanson
8,700
6.00
Johnson
10,300
625
Robson
15,000
700
Thomson 6,200
6.00
Watson
5,100
5.75
Write
procedures
to:
input
the
data
into
arrays
compute the actual pension fund contnbullons
output the
lists
of
names and computed contributions.
Link the procedures
with
a main program calling them
in
sequence.
Write
a function select
with
two
arguments range and miss. The function should
return a random whole number
In
the
given range but
it
should not be
the
value
of
miss.
Use
the
function
In
a program which chooses a random PAPER colour and then
draws random
circles
in
random INK colours
so
that
none
is
in
the
colour
of
PAPER.
3.
Re-write
the solution
to
exercise 1
so
that a function pension
takes
salary and
contribution
rate
as
arguments and returns
the
computed pension contribution.
Use
two
procedures, one
to
input
the
data and one
to
output the required
information using
the
function pension.
4.
Wnte
the following:
a procedure which
sets
up a 'pack
of
cards:
a procedure which shuffles
the
cards.
a function which
takes
a number
as
an
argument and returns a string value
describing the card.
a procedure which
'deals'
and displays four poker hands
of
five
cards each.
a main program which calls the above procedures.
(see
chapter
16
for discussion
of
a similar problem)
PROBLEMS
ON
CHAPTER
15
12/84 93