[Next] [Up] [Previous]
Next: Exercises Up: Appendix: Programming in POP-11 Previous: Assignment

User-Defined Procedures

The length procedure given earlier is already part of the POP-11 language. You can write other procedures yourself as you need them. Writing a user-defined procedure is a way of extending the POP-11 language, since the procedure functions in exactly the same way as a built-in one. Here is how you might define a procedure that works out how to add purchase tax to a price. Suppose that you have to add 15% tax to the cost of items you buy. To calculate the tax the procedure must multiply the price by 15%, or 0.15:

;;; This procedure multiplies the price by 0.15
;;; to get 15%
define tax(price) -> amount;
price * 0.15 -> amount;
enddefine;

All the words after the three semicolons ;;; until the end of the first line are taken as comments and are ignored by POP-11. Comments are useful to you, and to anyone who might read your program, to remind you of the purpose and effect of a procedure.

The line signalled by the word define is the procedure heading:

define tax(price) -> amount;

It tells the system that you want to define a new procedure tax that takes one input, or argument, and produces one output, or result. Since we do not know what the actual argument and result will be we, use a variables called price and amount to refer to them. We could have used any other names, e.g., abc and xyz, as in

define tax(abc) -> xyz;
abc * 0.15 -> xyz;
enddefine;

The -> amount is not an assignment. It indicates that there will be one result of the procedure and that the value of the result will be the value assigned to amount within the propcedure. The second line of the procedure is

price * 0.15 -> amount;

This carries out the calculation. When the procedure is called, the argument given is multiplied by 0.15 and that value of the expression is assigned to the variable amount. It is this value that is returned as the result. The last line ends the definition of the procedure. Neither price nor amount need to be declared with a vars statement since they are both local to the procedure and are declared in its heading.

A procedure definition is not a command, so POP-11 does not perform any action when you type it in. Only when you call the procedure will POP-11 respond. You do that by giving its name followed by arguments in brackets:

tax(10)=>
** 1.5

tax(50)=>
** 7.5

Thus the tax on a £10 item is £1.50 and the tax on a £50 item is £7.50. Now that the tax procedure has been defined, it acts in the same way as a built-in one, as a box with, in this case, one input and one result,

[IMAGE ]

but by following the commands within the procedure, line by line, you can follow its actions. If the tax procedure is called with an argument of 50, as in the second example above, then the first line of the procedure assigns the value of 50 to the variable price. The next line multiplies the assigned value of price by 0.15 and this is returned as the result of the procedure call. The procedure then ends. The returned value is then displayed.

A call to one procedure can form part of the definition of another procedure. Say we want to extend the tax calculation so that POP-11 returns the value of a given item with 15% tax added to the original price. This calculation divides neatly into two steps:

  1. Calculate the 15% tax for the original price.
  2. Add the tax onto the price and return it.

We already have a procedure, tax, to calculate 15% tax for any given price, so we merely need to write another procedure that adds the tax to the price:

define cost(price) -> amount;
price + tax(price) -> amount; ;;; add 15% tax
enddefine;

Having defined the cost procedure we can now call it

cost(10)=>
** 11.5

cost(50)=>
** 57.5

Thus a £50 article will cost £57.50 after 15% tax has been added. Notice that we have used the variables price and amount again within the cost procedure. This does not get confused with those of the same name within tax because an argument variable is local to the procedure in which it is used. Thus, tax creates its own storage space for its price and amount variables, which is quite separate to that of the variables inside cost.

You now know enough POP-11 to understand the first part of the Automated Tourist Guide: a procedure which we shall call welcome that displays the `Welcome' message shown in section 1.7.1.

define welcome;

   [Welcome to the London Tourist Guide]=>>

   [This guide can answer your queries about landmarks,]=>>

   [attractions and events in London]=>>

   [Please type your query in English, for example:]=>>

   [How do I get to the National Gallery?]=>>

   [Press the RETURN button to finish a query]=>>

enddefine;

The procedure can now be run by typing

welcome();

Welcome to the London Tourist Guide

This guide can answer your queries about landmarks ,

attractions and events in London

Please type your query in English , for example :

How do I get to the National Gallery ?

Press the RETURN button to finish a query

This is only the first procedure of a much larger program. The difficult part comes next: reading in the tourist's query and producing a useful response.


[Next] [Up] [Previous]
Next: Exercises Up: Appendix: Programming in POP-11 Previous: Assignment

Cogsweb Project: luisgh@cogs.susx.ac.uk