(lispref.info)Defining Functions

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' 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)
               => foo
               => 5
          (defun bar (a &optional b &rest c)
              (list a b c))
               => bar
          (bar 1 2 3 4 5)
               => (1 2 (3 4 5))
          (bar 1)
               => (1 nil nil)
          error--> Wrong number of arguments.
          (defun capitalize-backwards ()
            "Upcase the last letter of a word."
            (backward-word 1)
            (forward-word 1)
            (backward-char 1)
            (capitalize-word 1))
               => capitalize-backwards

     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