summaryrefslogtreecommitdiff
path: root/devdocs/elisp/function-names.html
blob: 997eae467a89be81217033b504fabdfa7cad8578 (plain)
1
2
3
4
5
6
 <h3 class="section">Naming a Function</h3>    <p>A symbol can serve as the name of a function. This happens when the symbol’s <em>function cell</em> (see <a href="symbol-components">Symbol Components</a>) contains a function object (e.g., a lambda expression). Then the symbol itself becomes a valid, callable function, equivalent to the function object in its function cell. </p> <p>The contents of the function cell are also called the symbol’s <em>function definition</em>. The procedure of using a symbol’s function definition in place of the symbol is called <em>symbol function indirection</em>; see <a href="function-indirection">Function Indirection</a>. If you have not given a symbol a function definition, its function cell is said to be <em>void</em>, and it cannot be used as a function. </p> <p>In practice, nearly all functions have names, and are referred to by their names. You can create a named Lisp function by defining a lambda expression and putting it in a function cell (see <a href="function-cells">Function Cells</a>). However, it is more common to use the <code>defun</code> special form, described in the next section. See <a href="defining-functions">Defining Functions</a>. </p> <p>We give functions names because it is convenient to refer to them by their names in Lisp expressions. Also, a named Lisp function can easily refer to itself—it can be recursive. Furthermore, primitives can only be referred to textually by their names, since primitive function objects (see <a href="primitive-function-type">Primitive Function Type</a>) have no read syntax. </p> <p>A function need not have a unique name. A given function object <em>usually</em> appears in the function cell of only one symbol, but this is just a convention. It is easy to store it in several symbols using <code>fset</code>; then each of the symbols is a valid name for the same function. </p> <p>Note that 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. (This is not the case in some dialects of Lisp, like Scheme.) </p> <p>By convention, if a function’s symbol consists of two names separated by ‘<samp>--</samp>’, the function is intended for internal use and the first part names the file defining the function. For example, a function named <code>vc-git--rev-parse</code> is an internal function defined in <samp>vc-git.el</samp>. Internal-use functions written in C have names ending in ‘<samp>-internal</samp>’, e.g., <code>bury-buffer-internal</code>. Emacs code contributed before 2018 may follow other internal-use naming conventions, which are being phased out. </p><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-Names.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Names.html</a>
  </p>
</div>