•
The ultimate concept of a procedure
IS
that
It
should be a 'black box' which receives
speCific information from outside' and performs certain operations which may include
sending speCific Information back
to
the outside: The outside' may be the main program
or another procedure.
The term 'black box' Implies that
ItS
internal workings are not Important; you only think
about what goes
In
and what comes
out
It.
for example, a procedure uses a variable,
count. and changes
its
value, that might affect a variable of the same name
in
the main
program
Think of a mail order company
You
send them an order and cash; they send
you goods. Information
IS
sent
to
a procedure and
It
sends back action and/or new
Information.
Procedures
and
Functions
WHY
PROCEDURES?
•
Information
..........
-----
Action and/or
new information
Mail
Order
Company
Procedure
•
Goods
Order by cash
•
.........
-----
•
You
do
not want the mall order company
to
use your name and address or other
Information for other purposes. That would be an unwanted Side-effect Similarly you
do
not want a procedure
to
cause unplanned changes
to
values of variables used
in
the main program.
Of course you could make sure that there are no double uses of variable names
in
a
program. That will work up
to
a point but
we
have shown
in
this chapter how
to
avoid
trouble even
if
you forget what variables have been used
in
any particular procedure.
A second aim
In
uSing
procedures
IS
to
make a program modular. Rather than have
one long main program you can break the
JOb
down into what Seymour Papert, the
inventor of LOGO, calls 'MInd-Sized
bites:
These are the procedures, each one small
enough
to
understand and control
easily.
They are linked together by the procedure
calls
in
a sequence or hierarchy.
A third aim
IS
to
avoid writing the same code
tWice.
Write
It
once
as
a procedure and
call
it
tWice
If
necessary. Functions and procedures written tor one program can often
be directly used, Without change, by other programs, and one might create a library
of commonly used procedures and functions.
We
give below another example which shows how procedures make a program modular
An order
IS
placed for six dishes
at
Chan's
Take
Away,
where the menu
IS:
EXAMPLE
Item Number Dish
Price
1
2
3
Prawns
Chicken
SpeCial
3.50
280
3.30
Write procedures for the following
tasks.
1.
Set
up
two three-element arrays
shOWing
menu, dishes and prices. Use a
DATA
statement
2. Simulate
an
order for
SIX
randomly chosen dishes
uSing
a procedure, choose, and
make a tally of the number of times each dish
is
chosen.
12184
89
-