15.2 Procedure Body

Following the delimiter that ends the procedure heading must be a single statement
defining the action to be performed or the value to be delivered. A terminator must follow
the statement. If it is a semicolon, the name of the procedure just defined is printed. It is
not printed if a dollar sign is used.

If the result wanted is given by a formula of some kind, the body is just that formula,
using the variables in the procedure heading.

Simple Example:

If f(x) is to mean (x+5)*(x+6)/(x+7), the entire procedure definition could
read

procedure f x; (x+5)*(x+6)/(x+7);

Then f(10) would evaluate to 240/17, f(a-6) to A*(A-1)/(A+1), and so
on.

More Complicated Example:

Suppose we need a function p(n,x) that, for any positive integer N, is the Legendre
polynomial of order n. We can define this operator using the textbook formula defining
these functions:

Put into words, the Legendre polynomial pn(x) is the result of substituting y = 0 in the
nth partial derivative with respect to y of a certain fraction involving x and y, then
dividing that by n!.

Procedures may also be defined recursively. In other words, the procedure body can
include references to the procedure name itself, or to other procedures that themselves
reference the given procedure. As an example, we can define the Legendre polynomial
through its standard recurrence relation:

procedure p(n,x);
if n<0 then rederr ~Invalid argument to P(N,X)~
else if n=0 then 1
else if n=1 then x
else ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n;

The operator REDERR in the above example provides for a simple error exit from an
algebraic procedure (and also a block). It can take a string as argument.

It should be noted however that all the above definitions of p(n,x) are quite inefficient
if extensive use is to be made of such polynomials, since each call effectively recomputes
all lower order polynomials. It would be better to store these expressions in an array, and
then use say the recurrence relation to compute only those polynomials that have not
already been derived. We leave it as an exercise for the reader to write such a
definition.