We have already seen some examples of procedure definitions. Here is a partial specification of the most common format for a procedure definition:
define then <name of procedure> then <formal parameters in parentheses, separated by commas> then -> (<output locals>) (if required, separated by commas). then semi-colon ; then local variable declarations, dlocal expressions, then actions to be performed (this may include conditionals, loops, additional local variable declarations, lexical blocks, nested "local" procedure definitions, etc.) then enddefine;Example - a procedure, named DOUBLESUM which takes two numbers, and produces a new number got by doubling the sum of the two.
define doublesum (num1, num2); 2 * (num1 + num2) enddefine;This definition produces a result, even though it does not use an output local. This is because the procedure calls first `+' (add) then `*' (multiply), and the latter leaves its result on the stack. So since DOUBLESUM does not remove the result of `*', it is left as the result of DOUBLESUM also. (See Chapter 3, and TEACH STACK).
We can make it clearer to ourselves, and readers of our programs if we indicate that a procedure is to produce a result, by using an `output local' variable in the procedure heading, as we did previously, thus:
define doublesum (num1, num2) -> result; 2 * (num1 + num2) -> result enddefine;(NOTE for experts: though clearer this is marginally less efficient, since it has an extra local variable, and does an extra assignment and stack operation. usually clarity is worth more than efficiency!)
Another example - if you already have the POP-11 rc_graphic package loaded, (See TEACH RC_GRAPHIC), which includes a procedure called rc_draw, which takes a number and draws a line, and a procedure called rc_turn, which takes a number in degrees representing the angle to turn, then you can define a procedure named SQUARE which will take a number, and will draw a square of the appropriate size:
define square (side); repeat 4 times rc_draw(side); rc_turn(90); endrepeat; enddefine;This procedure produces no result. So there is no need for an output local.
There are additional forms of procedure definitions, some of which have already been illustrated in previous chapters. E.g. procedures may produce multiple results, like the system procedure explode and the procedure join_two defined in Chapter 2.
It is also possible to define procedures whose names are used as infix operators (like rem, mod, +, * and <>). More on this below.
It is also possible to use the "define ... enddefine" syntax to specify other things than procedure definitions. This because the form can be extended by using the define_form mechanism, described in the only file HELP * DEFINE_FORM