Sinclair QL Beginner's Manual page 109

Hide thumbs Also See for QL:
Table of Contents

Advertisement

You do not want the mail 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 for 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.
EXAMPLE
An order is placed for six dishes at Chan's Take Away where the menu is:
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.
3. Pass the three numbers to a procedure, waiter, which passes back the cost of the order to the
main program using a parameter cost. Procedure waiter calls two other procedures, compute and
cook, which compute the cost and simulate "cooking"
4. The procedure, cook, simply prints the number required and the name of each dish.
Item Number
1
2
3
Dish
Price
Prawns
3.50
Chicken
2.80
Special
3.30

Advertisement

Table of Contents
loading

Table of Contents