Next: Calling Functions Prev: Function Names Up: Functions
Defining Named Functions
We usually give a name to a function when it is first created. This
is called "defining a function", and it is done with the `defun'
- Special Form: defun NAME ARGUMENT-LIST BODY-FORMS
`defun' is the usual way to define new Lisp functions. It defines
the symbol NAME as a function that looks like this:
(lambda ARGUMENT-LIST . BODY-FORMS)
This lambda expression is stored in the function cell of NAME.
The value returned by evaluating the `defun' form is NAME, but
usually we ignore this value.
As described previously (Note: Lambda Expressions.),
ARGUMENT-LIST is a list of argument names and may include the
keywords `&optional' and `&rest'. Also, the first two forms in
BODY-FORMS may be a documentation string and an interactive
Note that the same symbol NAME may also be used as a global
variable, since the value cell is independent of the function cell.
Here are some examples:
(defun foo () 5)
(defun bar (a &optional b &rest c)
(list a b c))
(bar 1 2 3 4 5)
=> (1 2 (3 4 5))
=> (1 nil nil)
error--> Wrong number of arguments.
(defun capitalize-backwards ()
"Upcase the last letter of a word."
Be careful not to redefine existing functions unintentionally.
`defun' redefines even primitive functions such as `car' without
any hesitation or notification. Redefining a function already
defined is often done deliberately, and there is no way to
distinguish deliberate redefinition from unintentional
automatically generated by info2www