Next: Lambda Expressions, Up: Functions
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.
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 Function Names. 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:
car
and append
. 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.
if
, and
, and while
.
See Special Forms.
command-execute
primitive, 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 an interactive
form 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.
You can use the function functionp
to test if an object is a
function:
This function returns
t
if object is any kind of function, i.e., can be passed tofuncall
. Note thatfunctionp
returnst
for symbols that are function names, and returnsnil
for special forms.
Unlike functionp
, the next three functions do not treat
a symbol as its function definition.
This function returns
t
if object is a built-in function (i.e., a Lisp primitive).(subrp 'message) ;message
is a symbol, ⇒ nil ; not a subr object. (subrp (symbol-function 'message)) ⇒ t
This function returns
t
if object is a byte-code function. For example:(byte-code-function-p (symbol-function 'next-line)) ⇒ t
This function provides information about the argument list of a primitive, subr. The returned value is a pair
(
min.
max)
. min is the minimum number of args. max is the maximum number or the symbolmany
, for a function with&rest
arguments, or the symbolunevalled
if subr is a special form.