next up previous
Contents Next: Local and Global Up: Defining LISP functions Previous: Defining LISP functions

Defining Functions: Defun

Use defun to define your own functions in LISP. Defun requires you to provide three things. The first is the name of the function, the second is a list of parameters for the function, and the third is the body of the function -- i.e. LISP instructions that tell the interpreter what to do when the function is called.

Schematically then, a function definition looks like this:

    (defun <name> <parameter-list> <body>)
Here is an example of a function to calculate the square of a number. It is short enough to be typed directly at the interpreter's prompt:

>(defun square (x)
   (* x x))
SQUARE

>(square 2)
4

>(square 1.4142158)
2.0000063289696399
The name of a user-defined function can be any symbol. (Recall: A symbol is any atom that is not a number.) It is even possible to redefine LISP's predefined functions such as first, cons, etc. Avoid doing this!

For present purposes, the parameter list should be a list of symbols. The symbols in this list must be variables, not constants. (Recall: T and nil are constants.) In some versions of LISP, pi is also given as a predefined constant that may not be set. The number of symbols in the parameter list determines the number of arguments that must be passed to the function when it is called, otherwise an error message will occur. The function square must be given exactly one argument. Check this out by typing (square 2 3) or (square).

(More advanced programming allows the use of &rest, &optional, &key in the parameter list to permit variable numbers of arguments. The use of these will not be covered here. You are referred to Steele for details.)

Inside the parameter list, x is used as a variable to stand for whatever argument is provided when square is called. Other symbols would have worked just as well in the definition of square. x is short, so we like it here, but sometimes it makes more sense to use something like nmbr that can play a mnemonic role when you look back at your code. Choosing to use number-to-be-squared for the name of the variable would soon get tiresome after you had mistyped it for the umpteenth time!

The body of a function can be a single LISP instruction, or it can be an indefinitely long set of instructions. Good programming style dictates that you keep the body of a function reasonably short (short enough to read on one screen, for example). Good programming technique also includes building small functions that perform specialized tasks and then using those as building blocks for more complicated tasks. The advantage of this technique is that the building blocks can be written and tested separately. Simple, short functions are much easier to debug than 30-line monsters.

Even though we don't want the interpreter to evaluate the name, parameters, and body components when the function is being defined, notice that none of them requires a quote. Defun, like setq and setf, takes care of this automatically.



next up previous
Contents Next: Local and Global Up: Defining LISP functions Previous: Defining LISP functions



© Colin Allen & Maneesh Dhagat
November 1999