Next: , Previous: , Up: Functions   [Contents][Index]

13.9 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.

See also the function indirect-function. See Definition of indirect-function.

Function: symbol-function symbol

This returns the object in the function cell of symbol. It does not check that the returned object is a legitimate function. If the function is void, the return value is nil.

(defun bar (n) (+ n 2))
(symbol-function 'bar)
     ⇒ #f(lambda (n) [t] (+ n 2))
(fset 'baz 'bar)
     ⇒ bar
(symbol-function 'baz)
     ⇒ bar

If you have never given a symbol any function definition, its function cell contains the default value nil and we say that that function is void. If you try to call the symbol as a function, Emacs signals a void-function error.

Unlike with void variables (see When a Variable is Void), a symbol’s function cell that contains nil is indistinguishable from the function’s being void. Note that void is not the same as the symbol void: void can be a valid function if you define it with defun.

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

This function returns t if the symbol has a non-nil object in its function cell, nil otherwise. It does not check that the object is a legitimate function.

Function: fmakunbound symbol

This function makes symbol’s function cell nil, so that a subsequent attempt to access this cell will cause a void-function error. It returns symbol. (See also makunbound, in When a Variable is Void.)

(defun foo (x) x)
(foo 1)
     ⇒1
(fmakunbound 'foo)
     ⇒ foo
(foo 1)
error→ Symbol's function definition is void: foo
Function: fset symbol definition

This function stores definition in the function cell of symbol. The result is definition. Normally definition should be a function or the name of a function, but this is not checked. The argument symbol is an ordinary evaluated argument.

The primary use of this function is as a subroutine by constructs that define or alter functions, like defun or advice-add (see Advising Emacs Lisp Functions). You can also use it to give a symbol a function definition that is not a function, e.g., a keyboard macro (see Keyboard Macros):

;; Define a named keyboard macro.
(fset 'kill-two-lines "\^u2\^k")
     ⇒ "\^u2\^k"

If you wish to use fset to make an alternate name for a function, consider using defalias instead. See Definition of defalias.

If the resulting function definition chain would be circular, then Emacs will signal a cyclic-function-indirection error.

Next: Closures, Previous: Generic Functions, Up: Functions   [Contents][Index]