summaryrefslogtreecommitdiff
path: root/devdocs/elisp/function-cells.html
blob: 6d6f810feb748c29e13a3dc8e683fafe6fecca5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 <h3 class="section">Accessing Function Cell Contents</h3> <p>The <em>function definition</em> 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. </p> <p>See also the function <code>indirect-function</code>. See <a href="function-indirection#Definition-of-indirect_002dfunction">Definition of indirect-function</a>. </p> <dl> <dt id="symbol-function">Function: <strong>symbol-function</strong> <em>symbol</em>
</dt> <dd>
 <p>This returns the object in the function cell of <var>symbol</var>. It does not check that the returned object is a legitimate function. </p> <p>If the function cell is void, the return value is <code>nil</code>. To distinguish between a function cell that is void and one set to <code>nil</code>, use <code>fboundp</code> (see below). </p> <div class="example"> <pre class="example">(defun bar (n) (+ n 2))
(symbol-function 'bar)
     ⇒ (lambda (n) (+ n 2))
</pre>
<pre class="example">(fset 'baz 'bar)
     ⇒ bar
</pre>
<pre class="example">(symbol-function 'baz)
     ⇒ bar
</pre>
</div> </dd>
</dl>  <p>If you have never given a symbol any function definition, we say that that symbol’s function cell is <em>void</em>. In other words, the function cell does not have any Lisp object in it. If you try to call the symbol as a function, Emacs signals a <code>void-function</code> error. </p> <p>Note that void is not the same as <code>nil</code> or the symbol <code>void</code>. The symbols <code>nil</code> and <code>void</code> 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 <code>defun</code>). A void function cell contains no object whatsoever. </p> <p>You can test the voidness of a symbol’s function definition with <code>fboundp</code>. After you have given a symbol a function definition, you can make it void once more using <code>fmakunbound</code>. </p> <dl> <dt id="fboundp">Function: <strong>fboundp</strong> <em>symbol</em>
</dt> <dd><p>This function returns <code>t</code> if the symbol has an object in its function cell, <code>nil</code> otherwise. It does not check that the object is a legitimate function. </p></dd>
</dl> <dl> <dt id="fmakunbound">Function: <strong>fmakunbound</strong> <em>symbol</em>
</dt> <dd>
<p>This function makes <var>symbol</var>’s function cell void, so that a subsequent attempt to access this cell will cause a <code>void-function</code> error. It returns <var>symbol</var>. (See also <code>makunbound</code>, in <a href="void-variables">Void Variables</a>.) </p> <div class="example"> <pre class="example">(defun foo (x) x)
(foo 1)
     ⇒1
</pre>
<pre class="example">(fmakunbound 'foo)
     ⇒ foo
</pre>
<pre class="example">(foo 1)
error→ Symbol's function definition is void: foo
</pre>
</div> </dd>
</dl> <dl> <dt id="fset">Function: <strong>fset</strong> <em>symbol definition</em>
</dt> <dd>
<p>This function stores <var>definition</var> in the function cell of <var>symbol</var>. The result is <var>definition</var>. Normally <var>definition</var> should be a function or the name of a function, but this is not checked. The argument <var>symbol</var> is an ordinary evaluated argument. </p> <p>The primary use of this function is as a subroutine by constructs that define or alter functions, like <code>defun</code> or <code>advice-add</code> (see <a href="advising-functions">Advising Functions</a>). You can also use it to give a symbol a function definition that is not a function, e.g., a keyboard macro (see <a href="keyboard-macros">Keyboard Macros</a>): </p> <div class="example"> <pre class="example">;; <span class="roman">Define a named keyboard macro.</span>
(fset 'kill-two-lines "\^u2\^k")
     ⇒ "\^u2\^k"
</pre>
</div> <p>If you wish to use <code>fset</code> to make an alternate name for a function, consider using <code>defalias</code> instead. See <a href="defining-functions#Definition-of-defalias">Definition of defalias</a>. </p>
</dd>
</dl><div class="_attribution">
  <p class="_attribution-p">
    Copyright &copy; 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
    <a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Cells.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Cells.html</a>
  </p>
</div>