PROCEDURE INDEX

PROCEDURE _ _ _ _ _ _ _ _ _ _ _ _ command

The procedure command allows you to define a mathematical operation as a function with arguments.

syntax:

_ _ _ <option> procedure <identifier> (<arg>{,<arg>}+);<body>

The <option> may be algebraic or symbolic, indicating the mode under which the procedure is executed, or real or integer, indicating the type of answer expected. The d efault is algebraic. Real or integer procedures are subtypes of algebraic procedures; type-checking is done on the results of integer procedures, but not on real procedures (in the current REDUCE release). <identifier> may be any valid REDUCE identifier that is not already a procedure name, operator, array or matrix. <arg> is a formal parameter that may be any valid REDUCE identifier. <body> is a single statement (a group or block statement may be used) with the desired activiti es in it.

examples:


procedure fac(n);
   if not (fixp(n) and n>=0)
     then rederr "Choose nonneg. integer only"
    else for i := 0:n-1 product i+1;

			 

  FAC 


fac(0); 

  1 


fac(5); 

  120 


fac(-5); 

  ***** choose nonneg. integer only

Procedures are automatically declared as operators upon definition . When REDUCE has parsed the procedure definition and successfully converted it to a form for its own use, it prints the name of the procedure. Procedure definitions cannot be nested. Procedures can call other procedures, or can recursively call themselves. Procedure identifiers can be cleared as you would clear an operator. Unlike let statements, new definitions under the same procedure name replace the previous definitions completely.

Be careful not to use the name of a system operator for your own procedure. REDUCE may or may not give you a warning message. If you redefine a system operator in your own procedure, the original function of the system operator is lost for the remainder of the REDUCE session.

Procedures may have none, one, or more than one parameter. A REDUCE parameter is a formal parameter only; the use of x as a parameter in a procedure definition has no connection with a value of x in the REDUCE session, and the results of calling a procedure have no effect on the value of x. If a procedure is called with x as a parameter, the current value of x is used as specified in the computation, but is not changed outside the procedure. Making an assignment statement by := with a formal parameter on the left-hand side only changes the value of the calling parameter within the procedure.

Using a let statement inside a procedure always changes the va lue globally: a let with a formal parameter makes the change to the calling parameter. let statements cannot be made on local variables inside begin...end blocks. When clear statements are used on formal parameters, the calling variables associated with them are cleared globally too. The use of let or clear statements inside procedures should be done with extreme caution.

Arrays and operators may be used as parameters to procedures. The body of the procedure can contain statements that appropriately manipulate these arguments. Changes are made to values of the calling arrays or operators. Simple expressions can also be used as arguments, in the place of scalar variables. Matrices may not be used as arguments to procedures.

A procedure that has no parameters is called by the procedure name, immediately followed by empty parentheses. The empty parentheses may be left out when writing a procedure with no parameters, but must appear in a call of the procedure. If this is a nuisance to you, use a let statement on the name of the procedure (i.e., let noargs = noargs()) after which you can call the procedure by just its name.

Procedures that have a single argument can leave out the parentheses around it both in the definition and procedure call. (You can use the parentheses if you wish.) Procedures with more than one argument must use parentheses, with the arguments separated by commas.

Procedures often have a begin...end block in them. Inside the block, local variables are declared using scalar, real or integer declarations. The declarations must be made immediately after the word begin, and if more than one type of declaration is made, they are separated by semicolons. REDUCE currently does no type checking on local variables; real and integer are treated just like scalar . Actions take place as specified in the statements inside the block statement. Any identifiers that are not formal parameters or local variables are treated as global variables, and activities involving these identifiers are global in effect.

If a return value is desired from a procedure call, a specific return command must be the last statement executed bef ore exiting from the procedure. If no return is used, a procedure returns a zero or no value.

Procedures are often written in a file using an editor, then the file is input using the command in. This method allows easy changes in development, and also allows you to load the named procedures whenever you like, by loading the files that contain them.