(lispref.info)Function Names

Next: Defining Functions Prev: Lambda Expressions Up: Functions

Naming a Function

   In most computer languages, every function has a name; the idea of a
function without a name is nonsensical.  In Lisp, a function in the
strictest sense has no name.  It is simply a list whose first element is
`lambda', or a primitive subr-object.

   However, a symbol can serve as the name of a function.  This happens
when you put the function in the symbol's "function cell" (Note: Symbol
Components.).  Then the symbol itself becomes a valid, callable
function, equivalent to the list or subr-object that its function cell
refers to.  The contents of the function cell are also called the
symbol's "function definition".  When the evaluator finds the function
definition to use in place of the symbol, we call that "symbol function
indirection"; see Note: Function Indirection.

   In practice, nearly all functions are given names in this way and
referred to through their names.  For example, the symbol `car' works
as a function and does what it does because the primitive subr-object
`#<subr car>' is stored in its function cell.

   We give functions names because it is more convenient to refer to
them by their names in other functions.  For primitive subr-objects
such as `#<subr car>', names are the only way you can refer to them:
there is no read syntax for such objects.  For functions written in
Lisp, the name is more convenient to use in a call than an explicit
lambda expression.  Also, a function with a name can refer to
itself--it can be recursive.  Writing the function's name in its own
definition is much more convenient than making the function definition
point to itself (something that is not impossible but that has various
disadvantages in practice).

   Functions are often identified with the symbols used to name them.
For example, we often speak of "the function `car'", not distinguishing
between the symbol `car' and the primitive subr-object that is its
function definition.  For most purposes, there is no need to

   Even so, keep in mind that a function need not have a unique name.
While a given function object *usually* appears in the function cell of
only one symbol, this is just a matter of convenience.  It is easy to
store it in several symbols using `fset'; then each of the symbols is
equally well a name for the same function.

   A symbol used as a function name may also be used as a variable;
these two uses of a symbol are independent and do not conflict.

automatically generated by info2www