What Is a Function?
In a general sense, a function is a rule for carrying out a computation given input values called arguments. The result of the computation is called the value or return value of the function. The computation can also have side effects, such as lasting changes in the values of variables or the contents of data structures (see Definition of side effect). A pure function is a function which, in addition to having no side effects, always returns the same value for the same combination of arguments, regardless of external factors such as machine type or system state.
In most computer languages, every function has a name. But in Lisp, a function in the strictest sense has no name: it is an object which can optionally be associated with a symbol (e.g., car) that serves as the function name. See Naming a Function. When a function has been given a name, we usually also refer to that symbol as a “function” (e.g., we refer to “the function car”). In this manual, the distinction between a function name and the function object itself is usually unimportant, but we will take note wherever it is relevant.
Certain function-like objects, called special forms and macros, also accept arguments to carry out computations. However, as explained below, these are not considered functions in Emacs Lisp.
Here are important terms for functions and function-like objects:
- lambda expression
-
A function (in the strict sense, i.e., a function object) which is written in Lisp. These are described in the following section. See Lambda Expressions.
- primitive
-
A function which is callable from Lisp but is actually written in C. Primitives are also called built-in functions, or subrs. Examples include functions like
carandappend. In addition, all special forms (see below) are also considered primitives.Usually, a function is implemented as a primitive because it is a fundamental part of Lisp (e.g.,
car), or because it provides a low-level interface to operating system services, or because it needs to run fast. Unlike functions defined in Lisp, primitives can be modified or added only by changing the C sources and recompiling Emacs. See Writing Emacs Primitives. - special form
-
A primitive that is like a function but does not evaluate all of its arguments in the usual way. It may evaluate only some of the arguments, or may evaluate them in an unusual order, or several times. Examples include
if,and, andwhile. See Special Forms. - macro
-
A construct defined in Lisp, which differs from a function in that it translates a Lisp expression into another expression which is to be evaluated instead of the original expression. Macros enable Lisp programmers to do the sorts of things that special forms can do. See Macros.
- command
-
An object which can be invoked via the
command-executeprimitive, usually due to the user typing in a key sequence bound to that command. See Interactive Call. A command is usually a function; if the function is written in Lisp, it is made into a command by aninteractiveform in the function definition (see Defining Commands). Commands that are functions can also be called from Lisp expressions, just like other functions.Keyboard macros (strings and vectors) are commands also, even though they are not functions. See Keyboard Macros. We say that a symbol is a command if its function cell contains a command (see Symbol Components); such a named command can be invoked with M-x.
- closure
-
A function object that is much like a lambda expression, except that it also encloses an environment of lexical variable bindings. See Closures.
- byte-code function
-
A function that has been compiled by the byte compiler. See Closure Function Type.
- autoload object
A place-holder for a real function. If the autoload object is called, Emacs loads the file containing the definition of the real function, and then calls the real function. See Autoload.
You can use the function functionp to test if an object is a function:
- Function: functionp object
-
This function returns
tif object is any kind of function, i.e., can be passed tofuncall. Note thatfunctionpreturnstfor symbols that are function names, and returnsnilfor symbols that are macros or special forms.If object is not a function, this function ordinarily returns
nil. However, the representation of function objects is complicated, and for efficiency reasons in rare cases this function can returnteven when object is not a function.
It is also possible to find out how many arguments an arbitrary function expects:
- Function: func-arity function
-
This function provides information about the argument list of the specified function. The returned value is a cons cell of the form
(min . max), where min is the minimum number of arguments, and max is either the maximum number of arguments, or the symbolmanyfor functions with&restarguments, or the symbolunevalledif function is a special form.Note that this function might return inaccurate results in some situations, such as the following:
- Functions defined using
apply-partially(see apply-partially). - Functions that are advised using
advice-add(see Advising Named Functions). - Functions that determine the argument list dynamically, as part of their code.
- Functions defined using
Unlike functionp, the next functions do not treat a symbol as its function definition.
- Function: subrp object
-
This function returns
tif object is a built-in function (i.e., a Lisp primitive).(subrp 'message) ;
messageis a symbol, ⇒ nil ; not a subr object.(subrp (symbol-function 'message)) ⇒ t
- Function: byte-code-function-p object
-
This function returns
tif object is a byte-code function. For example:(byte-code-function-p (symbol-function 'next-line)) ⇒ t
- Function: compiled-function-p object
This function returns
tif object is a function object that is not in the form of Lisp source code but something like machine code or byte code instead. More specifically it returnstif the function is built-in (a.k.a. “primitive”, see What Is a Function?), or byte-compiled (see Byte Compilation), or native-compiled (see Compilation of Lisp to Native Code), or a function loaded from a dynamic module (see Emacs Dynamic Modules).
- Function: interpreted-function-p object
This function returns
tif object is an interpreted function.
- Function: closurep object
This function returns
tif object is a closure, which is a particular kind of function object. Currently closures are used for all byte-code functions and all interpreted functions.
- Function: subr-arity subr
This works like
func-arity, but only for built-in functions and without symbol indirection. It signals an error for non-built-in functions. We recommend to usefunc-arityinstead.
- Function: cl-functionp object
This function is like
functionp, except it returnsnilfor lists and symbols.
- Function: primitive-function-p object
This function returns
tif object is a built-in primitive written in C (see Primitive Function Type). Note that special forms are explicitly excluded, as they are not functions. Usesubr-primitive-pif you need to recognize special forms as well.
Copyright © 1990-1996, 1998-2026 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/What-Is-a-Function.html