Next: Inline Functions Prev: Anonymous Functions Up: Functions
Accessing Function Cell Contents
The "function definition" of a symbol is the object stored in the
function cell of the symbol. The functions described here access, test,
and set the function cell of symbols.
- Function: symbol-function SYMBOL
This returns the object in the function cell of SYMBOL. If the
symbol's function cell is void, a `void-function' error is
This function does not check that the returned object is a
(defun bar (n) (+ n 2))
=> (lambda (n) (+ n 2))
(fset 'baz 'bar)
If you have never given a symbol any function definition, we say that
that symbol's function cell is "void". In other words, the function
cell does not have any Lisp object in it. If you try to call such a
symbol as a function, it signals a `void-function' error.
Note that void is not the same as `nil' or the symbol `void'. The
symbols `nil' and `void' are Lisp objects, and can be stored into a
function cell just as any other object can be (and they can be valid
functions if you define them in turn with `defun'); but `nil' or `void'
is *an object*. A void function cell contains no object whatsoever.
You can test the voidness of a symbol's function definition with
`fboundp'. After you have given a symbol a function definition, you
can make it void once more using `fmakunbound'.
- Function: fboundp SYMBOL
Returns `t' if the symbol has an object in its function cell,
`nil' otherwise. It does not check that the object is a legitimate
- Function: fmakunbound SYMBOL
This function makes SYMBOL's function cell void, so that a
subsequent attempt to access this cell will cause a `void-function'
error. (See also `makunbound', in Note: Local Variables.)
(defun foo (x) x)
error--> Symbol's function definition is void: foo
- Function: fset SYMBOL OBJECT
This function stores OBJECT in the function cell of SYMBOL. The
result is OBJECT. Normally OBJECT should be a function or the
name of a function, but this is not checked.
There are three normal uses of this function:
* Copying one symbol's function definition to another. (In
other words, making an alternate name for a function.)
* Giving a symbol a function definition that is not a list and
therefore cannot be made with `defun'. Note: Classifying
Lists, for an example of this usage.
* In constructs for defining or altering functions. If `defun'
were not a primitive, it could be written in Lisp (as a
macro) using `fset'.
Here are examples of the first two uses:
;; Give `first' the same definition `car' has.
(fset 'first (symbol-function 'car))
=> #<subr car>
(first '(1 2 3))
;; Make the symbol `car' the function definition of `xfirst'.
(fset 'xfirst 'car)
(xfirst '(1 2 3))
(symbol-function (symbol-function 'xfirst))
=> #<subr car>
;; Define a named keyboard macro.
(fset 'kill-two-lines "\^u2\^k")
When writing a function that extends a previously defined function,
the following idiom is often used:
(fset 'old-foo (symbol-function 'foo))
(defun foo ()
"Just like old-foo, except more so."
This does not work properly if `foo' has been defined to autoload. In
such a case, when `foo' calls `old-foo', Lisp attempts to define
`old-foo' by loading a file. Since this presumably defines `foo'
rather than `old-foo', it does not produce the proper results. The
only way to avoid this problem is to make sure the file is loaded
before moving aside the old definition of `foo'.
See also the function `indirect-function' in Note: Function
automatically generated by info2www