Sinclair QL Beginner's Manual page 112

Hide thumbs Also See for QL:
Table of Contents

Advertisement

490 DATA "Prawns",3.5,"Chicken",2.8,"Special",3.3
The output depends on the random choice of dishes but the following choice illustrates the pattern,
and gives a sample of output.
3 Prawns
1 Chicken
2 Special
Total cost is £20.40
COMMENT
Obviously the use of procedures and parameters in such a simple program is necessary but imagine
that each sub-task might be much more complex. In such a situation the use of procedures would
allow a modular build-up of the program with testing at each stage. The above example merely
illustrates the main notations and relationships of procedures.
Similarly the next example illustrates the use of functions.
Note that in the previous example the procedures "waiter" and "compute" both return exactly one
value. Rewrite the procedures as functions and show any other changes necessary as a
consequence.
DEFine FuNction waiter(dish)
cook dish
RETurn compute(dish)
END DEFine
DEFine FuNction compute(dish)
LET total = 0
FOR k = 1 TO 3
LET total = total + dish(k) * price(k)
END FOR k
RETurn total
END DEFine
The function call to waiter also takes a different form
LET bill = waiter(dish)
This program works as before. Notice that there are fewer parameters though the program structure is
similar. That is because the function names are also serving as parameters retuning information to
the source of the function call.
EXAMPLE
All the variables used as formal parameters in procedures or functions are 'safe' because they are
automatically local. Which variables used in the procedures or functions are not local? What
additional statements would be needed to make them local?
Program Changes
The variables k, pick and num are not local. The necessary changes to make them so are:
LOCAL k
LOCAL pick,num
TYPELESS PARAMETERS

Advertisement

Table of Contents
loading

Table of Contents