procedure
procedure(s_funcName(l_formalArglist)g_expr1... ) =>s_funcName
Description
Defines a function using a formal argument list. The body of the procedure is a list of expressions to evaluate.
The body of the procedure is a list of expressions to be evaluated one after another when s_funcName is called. There must be no white space between procedure and the open parenthesis that follows, nor between s_funcName and the open parenthesis of l_formalArglist. However, for defun there must be white space between s_funcName and the open parenthesis. This is the only difference between the two functions. defun has been provided principally so that you can your code appear more like other LISP dialects.
The last argument in l_formalArglist can be a string denoting type-checking characters, specified using the argument type template.
Expressions within a function can reference any variable on the formal argument list or any global variable defined outside the function. If necessary, local variables can be declared using the let or prog functions.
Arguments
|
Expression or expressions to be evaluated when s_funcName is called. |
Value Returned
Examples
procedure( cube(x) x**3 ) ; Defines a function to compute the
=> cube ; cube of a number using procedure.
cube( 3 ) => 27
defun( cube (x) x**3 ) ; Defines a function to compute the
=> cube ; cube of a number using defun.
The following function computes the factorial of its positive integer argument by recursively calling itself.
procedure( factorial(x)
if( (x == 0) then 1
else x * factorial(x - 1)))
=> factorial
defun( factorial (x)
if( (x == 0) then 1
else x * factorial( x - 1)))
=> factorial
factorial( 6 )
=> 720
ARGUMENT LIST PARAMETERS
Several parameters provide flexibility in procedure argument lists. These parameters are referred to as @ (“at”) options. The parameters are @rest, @optional, @key, and @aux.
@rest Option
The @rest option allows an arbitrary number of arguments to be passed into a function. Let’s say you need a function that takes any number of arguments and returns a list of them in reverse order. Using the @rest option simplifies this task.
procedure( myReverse( @rest r )
reverse( r ))
=> myReverse
myReverse( 'a 'b 'c )
=> (c b a)
@optional Option
The @optional option gives you another way to specify a flexible number of arguments. With @optional, each argument on the argument list is matched up with an argument on the formal argument list. If you place @optional in the argument list of a procedure definition, any argument following it is considered optional.
You can provide any optional argument with a default value. Specify the default value using a default form. The default form is a two-member list. The first member of this list is the optional argument’s name. The second member is the default value.
The default value is assigned only if no value is assigned when the function is called. If the procedure does not specify a default value for a given argument, nil is assigned.
The following is an outline of a procedure that builds a box of a certain length and width.
procedure(buildbox(length width @optional (xcoord 0)
(ycoord 0) color)
.
.
)
Both length and width must be specified when this function is called. However, the color and the coordinates of the box are declared as optional parameters. If only two parameters are specified, the optional parameters are given their default values. For xcoord and ycoord, those values are 0. Since no value is specified for color, color’s default value is nil.
Examine the following calls to buildbox and their return values:
buildbox(1 2); Builds a box of length 1, width 2
; at the coordinates (0,0) with the default color nil
buildbox(3 4 5.5 10.5); Builds a box of length 3, width 4
; at coordinates (5.5,10.5) with the default color nil
buildbox(3 4 5 5 'red); Builds a box of length 3, width 4
; at coordinates (5,5) with the default color red.
As illustrated in the above examples, @optional relies on order to determine what arguments are assigned to each formal argument. When relying on order is too lengthy or inconvenient, another “at” sign parameter, @key, provides an alternative.
@key Option
@key and @optional are mutually exclusive; they cannot appear in the same argument list. The @key option lets you specify the expected arguments in any order.
For example, examine the following function:
procedure(setTerm(@key (deviceType 'unknown)
(baudRate 9600)
keyClick )
.
.
)
If you call setTerm without arguments (that is, setTerm()), deviceType is set to unknown, baudRate to 9600, and keyClick to nil. Default forms work the same as they do for @optional. To specify a keyword for an argument (for example, deviceType, baudRate, and keyClick in the above function), precede the keyword with a question mark (?).
To set the baudRate to 4800 and the keyClick to ON, the call is:
setTerm(?baudRate 4800 ?keyClick 'ON)
; This sets baudRate and keyClick. Because nothing
; was specified for deviceType, it is set to its default,
; unknown.
setTerm(?keyClick 'ON ?baudRate 4800) ; Does
; the same as above.
In summary, there are two standard forms that procedure argument lists follow:
procedure(functionname([var1 var2 ...]
[@optional opt1 opt2 ...]
[@rest r])
.
.
)
procedure(functionname([var1 var2 ...]
[@key key1 key2 ...]
[@rest r])
.
.
)
Related Topics
Function and Program Structure
Return to top