Merge from trunk
[bpt/emacs.git] / doc / lispref / functions.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2011
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../../info/functions
7 @node Functions, Macros, Variables, Top
8 @chapter Functions
9
10 A Lisp program is composed mainly of Lisp functions. This chapter
11 explains what functions are, how they accept arguments, and how to
12 define them.
13
14 @menu
15 * What Is a Function:: Lisp functions vs. primitives; terminology.
16 * Lambda Expressions:: How functions are expressed as Lisp objects.
17 * Function Names:: A symbol can serve as the name of a function.
18 * Defining Functions:: Lisp expressions for defining functions.
19 * Calling Functions:: How to use an existing function.
20 * Mapping Functions:: Applying a function to each element of a list, etc.
21 * Anonymous Functions:: Lambda expressions are functions with no names.
22 * Function Cells:: Accessing or setting the function definition
23 of a symbol.
24 * Obsolete Functions:: Declaring functions obsolete.
25 * Inline Functions:: Defining functions that the compiler will open code.
26 * Function Currying:: Making wrapper functions that pre-specify
27 some arguments.
28 * Declaring Functions:: Telling the compiler that a function is defined.
29 * Function Safety:: Determining whether a function is safe to call.
30 * Related Topics:: Cross-references to specific Lisp primitives
31 that have a special bearing on how functions work.
32 @end menu
33
34 @node What Is a Function
35 @section What Is a Function?
36
37 In a general sense, a function is a rule for carrying on a computation
38 given several values called @dfn{arguments}. The result of the
39 computation is called the value of the function. The computation can
40 also have side effects: lasting changes in the values of variables or
41 the contents of data structures.
42
43 Here are important terms for functions in Emacs Lisp and for other
44 function-like objects.
45
46 @table @dfn
47 @item function
48 @cindex function
49 In Emacs Lisp, a @dfn{function} is anything that can be applied to
50 arguments in a Lisp program. In some cases, we use it more
51 specifically to mean a function written in Lisp. Special forms and
52 macros are not functions.
53
54 @item primitive
55 @cindex primitive
56 @cindex subr
57 @cindex built-in function
58 A @dfn{primitive} is a function callable from Lisp that is written in C,
59 such as @code{car} or @code{append}. These functions are also called
60 @dfn{built-in functions}, or @dfn{subrs}. (Special forms are also
61 considered primitives.)
62
63 Usually the reason we implement a function as a primitive is either
64 because it is fundamental, because it provides a low-level interface
65 to operating system services, or because it needs to run fast.
66 Primitives can be modified or added only by changing the C sources and
67 recompiling the editor. See @ref{Writing Emacs Primitives}.
68
69 @item lambda expression
70 A @dfn{lambda expression} is a function written in Lisp.
71 These are described in the following section.
72 @ifnottex
73 @xref{Lambda Expressions}.
74 @end ifnottex
75
76 @item special form
77 A @dfn{special form} is a primitive that is like a function but does not
78 evaluate all of its arguments in the usual way. It may evaluate only
79 some of the arguments, or may evaluate them in an unusual order, or
80 several times. Many special forms are described in @ref{Control
81 Structures}.
82
83 @item macro
84 @cindex macro
85 A @dfn{macro} is a construct defined in Lisp by the programmer. It
86 differs from a function in that it translates a Lisp expression that you
87 write into an equivalent expression to be evaluated instead of the
88 original expression. Macros enable Lisp programmers to do the sorts of
89 things that special forms can do. @xref{Macros}, for how to define and
90 use macros.
91
92 @item command
93 @cindex command
94 A @dfn{command} is an object that @code{command-execute} can invoke; it
95 is a possible definition for a key sequence. Some functions are
96 commands; a function written in Lisp is a command if it contains an
97 interactive declaration (@pxref{Defining Commands}). Such a function
98 can be called from Lisp expressions like other functions; in this case,
99 the fact that the function is a command makes no difference.
100
101 Keyboard macros (strings and vectors) are commands also, even though
102 they are not functions. A symbol is a command if its function
103 definition is a command; such symbols can be invoked with @kbd{M-x}.
104 The symbol is a function as well if the definition is a function.
105 @xref{Interactive Call}.
106
107 @item keystroke command
108 @cindex keystroke command
109 A @dfn{keystroke command} is a command that is bound to a key sequence
110 (typically one to three keystrokes). The distinction is made here
111 merely to avoid confusion with the meaning of ``command'' in non-Emacs
112 editors; for Lisp programs, the distinction is normally unimportant.
113
114 @item byte-code function
115 A @dfn{byte-code function} is a function that has been compiled by the
116 byte compiler. A byte-code function is actually a special case of a
117 @dfn{funvec} object (see below).
118
119 @item function vector
120 A @dfn{function vector}, or @dfn{funvec} is a vector-like object whose
121 purpose is to define special kinds of functions. @xref{Funvec Type}.
122
123 The exact meaning of the vector elements is determined by the type of
124 funvec: the most common use is byte-code functions, which have a
125 list---the argument list---as the first element. Further types of
126 funvec object are:
127
128 @table @code
129 @item curry
130 A curried function. Remaining arguments in the funvec are function to
131 call, and arguments to prepend to user arguments at the time of the
132 call; @xref{Function Currying}.
133 @end table
134
135 @end table
136
137 @defun functionp object
138 This function returns @code{t} if @var{object} is any kind of
139 function, i.e.@: can be passed to @code{funcall}. Note that
140 @code{functionp} returns @code{nil} for special forms (@pxref{Special
141 Forms}).
142 @end defun
143
144 Unlike @code{functionp}, the next three functions do @emph{not}
145 treat a symbol as its function definition.
146
147 @defun subrp object
148 This function returns @code{t} if @var{object} is a built-in function
149 (i.e., a Lisp primitive).
150
151 @example
152 @group
153 (subrp 'message) ; @r{@code{message} is a symbol,}
154 @result{} nil ; @r{not a subr object.}
155 @end group
156 @group
157 (subrp (symbol-function 'message))
158 @result{} t
159 @end group
160 @end example
161 @end defun
162
163 @defun byte-code-function-p object
164 This function returns @code{t} if @var{object} is a byte-code
165 function. For example:
166
167 @example
168 @group
169 (byte-code-function-p (symbol-function 'next-line))
170 @result{} t
171 @end group
172 @end example
173 @end defun
174
175 @defun funvecp object
176 @code{funvecp} returns @code{t} if @var{object} is a function vector
177 object (including byte-code objects), and @code{nil} otherwise.
178 @end defun
179
180 @defun subr-arity subr
181 This function provides information about the argument list of a
182 primitive, @var{subr}. The returned value is a pair
183 @code{(@var{min} . @var{max})}. @var{min} is the minimum number of
184 args. @var{max} is the maximum number or the symbol @code{many}, for a
185 function with @code{&rest} arguments, or the symbol @code{unevalled} if
186 @var{subr} is a special form.
187 @end defun
188
189 @node Lambda Expressions
190 @section Lambda Expressions
191 @cindex lambda expression
192
193 A function written in Lisp is a list that looks like this:
194
195 @example
196 (lambda (@var{arg-variables}@dots{})
197 @r{[}@var{documentation-string}@r{]}
198 @r{[}@var{interactive-declaration}@r{]}
199 @var{body-forms}@dots{})
200 @end example
201
202 @noindent
203 Such a list is called a @dfn{lambda expression}. In Emacs Lisp, it
204 actually is valid as an expression---it evaluates to itself. In some
205 other Lisp dialects, a lambda expression is not a valid expression at
206 all. In either case, its main use is not to be evaluated as an
207 expression, but to be called as a function.
208
209 @menu
210 * Lambda Components:: The parts of a lambda expression.
211 * Simple Lambda:: A simple example.
212 * Argument List:: Details and special features of argument lists.
213 * Function Documentation:: How to put documentation in a function.
214 @end menu
215
216 @node Lambda Components
217 @subsection Components of a Lambda Expression
218
219 @ifnottex
220
221 A function written in Lisp (a ``lambda expression'') is a list that
222 looks like this:
223
224 @example
225 (lambda (@var{arg-variables}@dots{})
226 [@var{documentation-string}]
227 [@var{interactive-declaration}]
228 @var{body-forms}@dots{})
229 @end example
230 @end ifnottex
231
232 @cindex lambda list
233 The first element of a lambda expression is always the symbol
234 @code{lambda}. This indicates that the list represents a function. The
235 reason functions are defined to start with @code{lambda} is so that
236 other lists, intended for other uses, will not accidentally be valid as
237 functions.
238
239 The second element is a list of symbols---the argument variable names.
240 This is called the @dfn{lambda list}. When a Lisp function is called,
241 the argument values are matched up against the variables in the lambda
242 list, which are given local bindings with the values provided.
243 @xref{Local Variables}.
244
245 The documentation string is a Lisp string object placed within the
246 function definition to describe the function for the Emacs help
247 facilities. @xref{Function Documentation}.
248
249 The interactive declaration is a list of the form @code{(interactive
250 @var{code-string})}. This declares how to provide arguments if the
251 function is used interactively. Functions with this declaration are called
252 @dfn{commands}; they can be called using @kbd{M-x} or bound to a key.
253 Functions not intended to be called in this way should not have interactive
254 declarations. @xref{Defining Commands}, for how to write an interactive
255 declaration.
256
257 @cindex body of function
258 The rest of the elements are the @dfn{body} of the function: the Lisp
259 code to do the work of the function (or, as a Lisp programmer would say,
260 ``a list of Lisp forms to evaluate''). The value returned by the
261 function is the value returned by the last element of the body.
262
263 @node Simple Lambda
264 @subsection A Simple Lambda-Expression Example
265
266 Consider for example the following function:
267
268 @example
269 (lambda (a b c) (+ a b c))
270 @end example
271
272 @noindent
273 We can call this function by writing it as the @sc{car} of an
274 expression, like this:
275
276 @example
277 @group
278 ((lambda (a b c) (+ a b c))
279 1 2 3)
280 @end group
281 @end example
282
283 @noindent
284 This call evaluates the body of the lambda expression with the variable
285 @code{a} bound to 1, @code{b} bound to 2, and @code{c} bound to 3.
286 Evaluation of the body adds these three numbers, producing the result 6;
287 therefore, this call to the function returns the value 6.
288
289 Note that the arguments can be the results of other function calls, as in
290 this example:
291
292 @example
293 @group
294 ((lambda (a b c) (+ a b c))
295 1 (* 2 3) (- 5 4))
296 @end group
297 @end example
298
299 @noindent
300 This evaluates the arguments @code{1}, @code{(* 2 3)}, and @code{(- 5
301 4)} from left to right. Then it applies the lambda expression to the
302 argument values 1, 6 and 1 to produce the value 8.
303
304 It is not often useful to write a lambda expression as the @sc{car} of
305 a form in this way. You can get the same result, of making local
306 variables and giving them values, using the special form @code{let}
307 (@pxref{Local Variables}). And @code{let} is clearer and easier to use.
308 In practice, lambda expressions are either stored as the function
309 definitions of symbols, to produce named functions, or passed as
310 arguments to other functions (@pxref{Anonymous Functions}).
311
312 However, calls to explicit lambda expressions were very useful in the
313 old days of Lisp, before the special form @code{let} was invented. At
314 that time, they were the only way to bind and initialize local
315 variables.
316
317 @node Argument List
318 @subsection Other Features of Argument Lists
319 @kindex wrong-number-of-arguments
320 @cindex argument binding
321 @cindex binding arguments
322 @cindex argument lists, features
323
324 Our simple sample function, @code{(lambda (a b c) (+ a b c))},
325 specifies three argument variables, so it must be called with three
326 arguments: if you try to call it with only two arguments or four
327 arguments, you get a @code{wrong-number-of-arguments} error.
328
329 It is often convenient to write a function that allows certain
330 arguments to be omitted. For example, the function @code{substring}
331 accepts three arguments---a string, the start index and the end
332 index---but the third argument defaults to the @var{length} of the
333 string if you omit it. It is also convenient for certain functions to
334 accept an indefinite number of arguments, as the functions @code{list}
335 and @code{+} do.
336
337 @cindex optional arguments
338 @cindex rest arguments
339 @kindex &optional
340 @kindex &rest
341 To specify optional arguments that may be omitted when a function
342 is called, simply include the keyword @code{&optional} before the optional
343 arguments. To specify a list of zero or more extra arguments, include the
344 keyword @code{&rest} before one final argument.
345
346 Thus, the complete syntax for an argument list is as follows:
347
348 @example
349 @group
350 (@var{required-vars}@dots{}
351 @r{[}&optional @var{optional-vars}@dots{}@r{]}
352 @r{[}&rest @var{rest-var}@r{]})
353 @end group
354 @end example
355
356 @noindent
357 The square brackets indicate that the @code{&optional} and @code{&rest}
358 clauses, and the variables that follow them, are optional.
359
360 A call to the function requires one actual argument for each of the
361 @var{required-vars}. There may be actual arguments for zero or more of
362 the @var{optional-vars}, and there cannot be any actual arguments beyond
363 that unless the lambda list uses @code{&rest}. In that case, there may
364 be any number of extra actual arguments.
365
366 If actual arguments for the optional and rest variables are omitted,
367 then they always default to @code{nil}. There is no way for the
368 function to distinguish between an explicit argument of @code{nil} and
369 an omitted argument. However, the body of the function is free to
370 consider @code{nil} an abbreviation for some other meaningful value.
371 This is what @code{substring} does; @code{nil} as the third argument to
372 @code{substring} means to use the length of the string supplied.
373
374 @cindex CL note---default optional arg
375 @quotation
376 @b{Common Lisp note:} Common Lisp allows the function to specify what
377 default value to use when an optional argument is omitted; Emacs Lisp
378 always uses @code{nil}. Emacs Lisp does not support ``supplied-p''
379 variables that tell you whether an argument was explicitly passed.
380 @end quotation
381
382 For example, an argument list that looks like this:
383
384 @example
385 (a b &optional c d &rest e)
386 @end example
387
388 @noindent
389 binds @code{a} and @code{b} to the first two actual arguments, which are
390 required. If one or two more arguments are provided, @code{c} and
391 @code{d} are bound to them respectively; any arguments after the first
392 four are collected into a list and @code{e} is bound to that list. If
393 there are only two arguments, @code{c} is @code{nil}; if two or three
394 arguments, @code{d} is @code{nil}; if four arguments or fewer, @code{e}
395 is @code{nil}.
396
397 There is no way to have required arguments following optional
398 ones---it would not make sense. To see why this must be so, suppose
399 that @code{c} in the example were optional and @code{d} were required.
400 Suppose three actual arguments are given; which variable would the
401 third argument be for? Would it be used for the @var{c}, or for
402 @var{d}? One can argue for both possibilities. Similarly, it makes
403 no sense to have any more arguments (either required or optional)
404 after a @code{&rest} argument.
405
406 Here are some examples of argument lists and proper calls:
407
408 @smallexample
409 ((lambda (n) (1+ n)) ; @r{One required:}
410 1) ; @r{requires exactly one argument.}
411 @result{} 2
412 ((lambda (n &optional n1) ; @r{One required and one optional:}
413 (if n1 (+ n n1) (1+ n))) ; @r{1 or 2 arguments.}
414 1 2)
415 @result{} 3
416 ((lambda (n &rest ns) ; @r{One required and one rest:}
417 (+ n (apply '+ ns))) ; @r{1 or more arguments.}
418 1 2 3 4 5)
419 @result{} 15
420 @end smallexample
421
422 @node Function Documentation
423 @subsection Documentation Strings of Functions
424 @cindex documentation of function
425
426 A lambda expression may optionally have a @dfn{documentation string} just
427 after the lambda list. This string does not affect execution of the
428 function; it is a kind of comment, but a systematized comment which
429 actually appears inside the Lisp world and can be used by the Emacs help
430 facilities. @xref{Documentation}, for how the @var{documentation-string} is
431 accessed.
432
433 It is a good idea to provide documentation strings for all the
434 functions in your program, even those that are called only from within
435 your program. Documentation strings are like comments, except that they
436 are easier to access.
437
438 The first line of the documentation string should stand on its own,
439 because @code{apropos} displays just this first line. It should consist
440 of one or two complete sentences that summarize the function's purpose.
441
442 The start of the documentation string is usually indented in the
443 source file, but since these spaces come before the starting
444 double-quote, they are not part of the string. Some people make a
445 practice of indenting any additional lines of the string so that the
446 text lines up in the program source. @emph{That is a mistake.} The
447 indentation of the following lines is inside the string; what looks
448 nice in the source code will look ugly when displayed by the help
449 commands.
450
451 You may wonder how the documentation string could be optional, since
452 there are required components of the function that follow it (the body).
453 Since evaluation of a string returns that string, without any side effects,
454 it has no effect if it is not the last form in the body. Thus, in
455 practice, there is no confusion between the first form of the body and the
456 documentation string; if the only body form is a string then it serves both
457 as the return value and as the documentation.
458
459 The last line of the documentation string can specify calling
460 conventions different from the actual function arguments. Write
461 text like this:
462
463 @example
464 \(fn @var{arglist})
465 @end example
466
467 @noindent
468 following a blank line, at the beginning of the line, with no newline
469 following it inside the documentation string. (The @samp{\} is used
470 to avoid confusing the Emacs motion commands.) The calling convention
471 specified in this way appears in help messages in place of the one
472 derived from the actual arguments of the function.
473
474 This feature is particularly useful for macro definitions, since the
475 arguments written in a macro definition often do not correspond to the
476 way users think of the parts of the macro call.
477
478 @node Function Names
479 @section Naming a Function
480 @cindex function definition
481 @cindex named function
482 @cindex function name
483
484 In most computer languages, every function has a name; the idea of a
485 function without a name is nonsensical. In Lisp, a function in the
486 strictest sense has no name. It is simply a list whose first element is
487 @code{lambda}, a byte-code function object, or a primitive subr-object.
488
489 However, a symbol can serve as the name of a function. This happens
490 when you put the function in the symbol's @dfn{function cell}
491 (@pxref{Symbol Components}). Then the symbol itself becomes a valid,
492 callable function, equivalent to the list or subr-object that its
493 function cell refers to. The contents of the function cell are also
494 called the symbol's @dfn{function definition}. The procedure of using a
495 symbol's function definition in place of the symbol is called
496 @dfn{symbol function indirection}; see @ref{Function Indirection}.
497
498 In practice, nearly all functions are given names in this way and
499 referred to through their names. For example, the symbol @code{car} works
500 as a function and does what it does because the primitive subr-object
501 @code{#<subr car>} is stored in its function cell.
502
503 We give functions names because it is convenient to refer to them by
504 their names in Lisp expressions. For primitive subr-objects such as
505 @code{#<subr car>}, names are the only way you can refer to them: there
506 is no read syntax for such objects. For functions written in Lisp, the
507 name is more convenient to use in a call than an explicit lambda
508 expression. Also, a function with a name can refer to itself---it can
509 be recursive. Writing the function's name in its own definition is much
510 more convenient than making the function definition point to itself
511 (something that is not impossible but that has various disadvantages in
512 practice).
513
514 We often identify functions with the symbols used to name them. For
515 example, we often speak of ``the function @code{car},'' not
516 distinguishing between the symbol @code{car} and the primitive
517 subr-object that is its function definition. For most purposes, the
518 distinction is not important.
519
520 Even so, keep in mind that a function need not have a unique name. While
521 a given function object @emph{usually} appears in the function cell of only
522 one symbol, this is just a matter of convenience. It is easy to store
523 it in several symbols using @code{fset}; then each of the symbols is
524 equally well a name for the same function.
525
526 A symbol used as a function name may also be used as a variable; these
527 two uses of a symbol are independent and do not conflict. (Some Lisp
528 dialects, such as Scheme, do not distinguish between a symbol's value
529 and its function definition; a symbol's value as a variable is also its
530 function definition.) If you have not given a symbol a function
531 definition, you cannot use it as a function; whether the symbol has a
532 value as a variable makes no difference to this.
533
534 @node Defining Functions
535 @section Defining Functions
536 @cindex defining a function
537
538 We usually give a name to a function when it is first created. This
539 is called @dfn{defining a function}, and it is done with the
540 @code{defun} special form.
541
542 @defspec defun name argument-list body-forms
543 @code{defun} is the usual way to define new Lisp functions. It
544 defines the symbol @var{name} as a function that looks like this:
545
546 @example
547 (lambda @var{argument-list} . @var{body-forms})
548 @end example
549
550 @code{defun} stores this lambda expression in the function cell of
551 @var{name}. It returns the value @var{name}, but usually we ignore this
552 value.
553
554 As described previously, @var{argument-list} is a list of argument
555 names and may include the keywords @code{&optional} and @code{&rest}
556 (@pxref{Lambda Expressions}). Also, the first two of the
557 @var{body-forms} may be a documentation string and an interactive
558 declaration.
559
560 There is no conflict if the same symbol @var{name} is also used as a
561 variable, since the symbol's value cell is independent of the function
562 cell. @xref{Symbol Components}.
563
564 Here are some examples:
565
566 @example
567 @group
568 (defun foo () 5)
569 @result{} foo
570 @end group
571 @group
572 (foo)
573 @result{} 5
574 @end group
575
576 @group
577 (defun bar (a &optional b &rest c)
578 (list a b c))
579 @result{} bar
580 @end group
581 @group
582 (bar 1 2 3 4 5)
583 @result{} (1 2 (3 4 5))
584 @end group
585 @group
586 (bar 1)
587 @result{} (1 nil nil)
588 @end group
589 @group
590 (bar)
591 @error{} Wrong number of arguments.
592 @end group
593
594 @group
595 (defun capitalize-backwards ()
596 "Upcase the last letter of a word."
597 (interactive)
598 (backward-word 1)
599 (forward-word 1)
600 (backward-char 1)
601 (capitalize-word 1))
602 @result{} capitalize-backwards
603 @end group
604 @end example
605
606 Be careful not to redefine existing functions unintentionally.
607 @code{defun} redefines even primitive functions such as @code{car}
608 without any hesitation or notification. Redefining a function already
609 defined is often done deliberately, and there is no way to distinguish
610 deliberate redefinition from unintentional redefinition.
611 @end defspec
612
613 @cindex function aliases
614 @defun defalias name definition &optional docstring
615 @anchor{Definition of defalias}
616 This special form defines the symbol @var{name} as a function, with
617 definition @var{definition} (which can be any valid Lisp function).
618 It returns @var{definition}.
619
620 If @var{docstring} is non-@code{nil}, it becomes the function
621 documentation of @var{name}. Otherwise, any documentation provided by
622 @var{definition} is used.
623
624 The proper place to use @code{defalias} is where a specific function
625 name is being defined---especially where that name appears explicitly in
626 the source file being loaded. This is because @code{defalias} records
627 which file defined the function, just like @code{defun}
628 (@pxref{Unloading}).
629
630 By contrast, in programs that manipulate function definitions for other
631 purposes, it is better to use @code{fset}, which does not keep such
632 records. @xref{Function Cells}.
633 @end defun
634
635 You cannot create a new primitive function with @code{defun} or
636 @code{defalias}, but you can use them to change the function definition of
637 any symbol, even one such as @code{car} or @code{x-popup-menu} whose
638 normal definition is a primitive. However, this is risky: for
639 instance, it is next to impossible to redefine @code{car} without
640 breaking Lisp completely. Redefining an obscure function such as
641 @code{x-popup-menu} is less dangerous, but it still may not work as
642 you expect. If there are calls to the primitive from C code, they
643 call the primitive's C definition directly, so changing the symbol's
644 definition will have no effect on them.
645
646 See also @code{defsubst}, which defines a function like @code{defun}
647 and tells the Lisp compiler to open-code it. @xref{Inline Functions}.
648
649 @node Calling Functions
650 @section Calling Functions
651 @cindex function invocation
652 @cindex calling a function
653
654 Defining functions is only half the battle. Functions don't do
655 anything until you @dfn{call} them, i.e., tell them to run. Calling a
656 function is also known as @dfn{invocation}.
657
658 The most common way of invoking a function is by evaluating a list.
659 For example, evaluating the list @code{(concat "a" "b")} calls the
660 function @code{concat} with arguments @code{"a"} and @code{"b"}.
661 @xref{Evaluation}, for a description of evaluation.
662
663 When you write a list as an expression in your program, you specify
664 which function to call, and how many arguments to give it, in the text
665 of the program. Usually that's just what you want. Occasionally you
666 need to compute at run time which function to call. To do that, use
667 the function @code{funcall}. When you also need to determine at run
668 time how many arguments to pass, use @code{apply}.
669
670 @defun funcall function &rest arguments
671 @code{funcall} calls @var{function} with @var{arguments}, and returns
672 whatever @var{function} returns.
673
674 Since @code{funcall} is a function, all of its arguments, including
675 @var{function}, are evaluated before @code{funcall} is called. This
676 means that you can use any expression to obtain the function to be
677 called. It also means that @code{funcall} does not see the
678 expressions you write for the @var{arguments}, only their values.
679 These values are @emph{not} evaluated a second time in the act of
680 calling @var{function}; the operation of @code{funcall} is like the
681 normal procedure for calling a function, once its arguments have
682 already been evaluated.
683
684 The argument @var{function} must be either a Lisp function or a
685 primitive function. Special forms and macros are not allowed, because
686 they make sense only when given the ``unevaluated'' argument
687 expressions. @code{funcall} cannot provide these because, as we saw
688 above, it never knows them in the first place.
689
690 @example
691 @group
692 (setq f 'list)
693 @result{} list
694 @end group
695 @group
696 (funcall f 'x 'y 'z)
697 @result{} (x y z)
698 @end group
699 @group
700 (funcall f 'x 'y '(z))
701 @result{} (x y (z))
702 @end group
703 @group
704 (funcall 'and t nil)
705 @error{} Invalid function: #<subr and>
706 @end group
707 @end example
708
709 Compare these examples with the examples of @code{apply}.
710 @end defun
711
712 @defun apply function &rest arguments
713 @code{apply} calls @var{function} with @var{arguments}, just like
714 @code{funcall} but with one difference: the last of @var{arguments} is a
715 list of objects, which are passed to @var{function} as separate
716 arguments, rather than a single list. We say that @code{apply}
717 @dfn{spreads} this list so that each individual element becomes an
718 argument.
719
720 @code{apply} returns the result of calling @var{function}. As with
721 @code{funcall}, @var{function} must either be a Lisp function or a
722 primitive function; special forms and macros do not make sense in
723 @code{apply}.
724
725 @example
726 @group
727 (setq f 'list)
728 @result{} list
729 @end group
730 @group
731 (apply f 'x 'y 'z)
732 @error{} Wrong type argument: listp, z
733 @end group
734 @group
735 (apply '+ 1 2 '(3 4))
736 @result{} 10
737 @end group
738 @group
739 (apply '+ '(1 2 3 4))
740 @result{} 10
741 @end group
742
743 @group
744 (apply 'append '((a b c) nil (x y z) nil))
745 @result{} (a b c x y z)
746 @end group
747 @end example
748
749 For an interesting example of using @code{apply}, see @ref{Definition
750 of mapcar}.
751 @end defun
752
753 @cindex partial application of functions
754 @cindex currying
755 Sometimes it is useful to fix some of the function's arguments at
756 certain values, and leave the rest of arguments for when the function
757 is actually called. The act of fixing some of the function's
758 arguments is called @dfn{partial application} of the function@footnote{
759 This is related to, but different from @dfn{currying}, which
760 transforms a function that takes multiple arguments in such a way that
761 it can be called as a chain of functions, each one with a single
762 argument.}.
763 The result is a new function that accepts the rest of
764 arguments and calls the original function with all the arguments
765 combined.
766
767 Here's how to do partial application in Emacs Lisp:
768
769 @defun apply-partially func &rest args
770 This function returns a new function which, when called, will call
771 @var{func} with the list of arguments composed from @var{args} and
772 additional arguments specified at the time of the call. If @var{func}
773 accepts @var{n} arguments, then a call to @code{apply-partially} with
774 @w{@code{@var{m} < @var{n}}} arguments will produce a new function of
775 @w{@code{@var{n} - @var{m}}} arguments.
776
777 Here's how we could define the built-in function @code{1+}, if it
778 didn't exist, using @code{apply-partially} and @code{+}, another
779 built-in function:
780
781 @example
782 @group
783 (defalias '1+ (apply-partially '+ 1)
784 "Increment argument by one.")
785 @end group
786 @group
787 (1+ 10)
788 @result{} 11
789 @end group
790 @end example
791 @end defun
792
793 @cindex functionals
794 It is common for Lisp functions to accept functions as arguments or
795 find them in data structures (especially in hook variables and property
796 lists) and call them using @code{funcall} or @code{apply}. Functions
797 that accept function arguments are often called @dfn{functionals}.
798
799 Sometimes, when you call a functional, it is useful to supply a no-op
800 function as the argument. Here are two different kinds of no-op
801 function:
802
803 @defun identity arg
804 This function returns @var{arg} and has no side effects.
805 @end defun
806
807 @defun ignore &rest args
808 This function ignores any arguments and returns @code{nil}.
809 @end defun
810
811 @node Mapping Functions
812 @section Mapping Functions
813 @cindex mapping functions
814
815 A @dfn{mapping function} applies a given function (@emph{not} a
816 special form or macro) to each element of a list or other collection.
817 Emacs Lisp has several such functions; @code{mapcar} and
818 @code{mapconcat}, which scan a list, are described here.
819 @xref{Definition of mapatoms}, for the function @code{mapatoms} which
820 maps over the symbols in an obarray. @xref{Definition of maphash},
821 for the function @code{maphash} which maps over key/value associations
822 in a hash table.
823
824 These mapping functions do not allow char-tables because a char-table
825 is a sparse array whose nominal range of indices is very large. To map
826 over a char-table in a way that deals properly with its sparse nature,
827 use the function @code{map-char-table} (@pxref{Char-Tables}).
828
829 @defun mapcar function sequence
830 @anchor{Definition of mapcar}
831 @code{mapcar} applies @var{function} to each element of @var{sequence}
832 in turn, and returns a list of the results.
833
834 The argument @var{sequence} can be any kind of sequence except a
835 char-table; that is, a list, a vector, a bool-vector, or a string. The
836 result is always a list. The length of the result is the same as the
837 length of @var{sequence}. For example:
838
839 @smallexample
840 @group
841 (mapcar 'car '((a b) (c d) (e f)))
842 @result{} (a c e)
843 (mapcar '1+ [1 2 3])
844 @result{} (2 3 4)
845 (mapcar 'string "abc")
846 @result{} ("a" "b" "c")
847 @end group
848
849 @group
850 ;; @r{Call each function in @code{my-hooks}.}
851 (mapcar 'funcall my-hooks)
852 @end group
853
854 @group
855 (defun mapcar* (function &rest args)
856 "Apply FUNCTION to successive cars of all ARGS.
857 Return the list of results."
858 ;; @r{If no list is exhausted,}
859 (if (not (memq nil args))
860 ;; @r{apply function to @sc{car}s.}
861 (cons (apply function (mapcar 'car args))
862 (apply 'mapcar* function
863 ;; @r{Recurse for rest of elements.}
864 (mapcar 'cdr args)))))
865 @end group
866
867 @group
868 (mapcar* 'cons '(a b c) '(1 2 3 4))
869 @result{} ((a . 1) (b . 2) (c . 3))
870 @end group
871 @end smallexample
872 @end defun
873
874 @defun mapc function sequence
875 @code{mapc} is like @code{mapcar} except that @var{function} is used for
876 side-effects only---the values it returns are ignored, not collected
877 into a list. @code{mapc} always returns @var{sequence}.
878 @end defun
879
880 @defun mapconcat function sequence separator
881 @code{mapconcat} applies @var{function} to each element of
882 @var{sequence}: the results, which must be strings, are concatenated.
883 Between each pair of result strings, @code{mapconcat} inserts the string
884 @var{separator}. Usually @var{separator} contains a space or comma or
885 other suitable punctuation.
886
887 The argument @var{function} must be a function that can take one
888 argument and return a string. The argument @var{sequence} can be any
889 kind of sequence except a char-table; that is, a list, a vector, a
890 bool-vector, or a string.
891
892 @smallexample
893 @group
894 (mapconcat 'symbol-name
895 '(The cat in the hat)
896 " ")
897 @result{} "The cat in the hat"
898 @end group
899
900 @group
901 (mapconcat (function (lambda (x) (format "%c" (1+ x))))
902 "HAL-8000"
903 "")
904 @result{} "IBM.9111"
905 @end group
906 @end smallexample
907 @end defun
908
909 @node Anonymous Functions
910 @section Anonymous Functions
911 @cindex anonymous function
912
913 In Lisp, a function is a list that starts with @code{lambda}, a
914 byte-code function compiled from such a list, or alternatively a
915 primitive subr-object; names are ``extra.'' Although functions are
916 usually defined with @code{defun} and given names at the same time, it
917 is occasionally more concise to use an explicit lambda expression---an
918 anonymous function. Such a list is valid wherever a function name is.
919
920 Any method of creating such a list makes a valid function. Even this:
921
922 @smallexample
923 @group
924 (setq silly (append '(lambda (x)) (list (list '+ (* 3 4) 'x))))
925 @result{} (lambda (x) (+ 12 x))
926 @end group
927 @end smallexample
928
929 @noindent
930 This computes a list that looks like @code{(lambda (x) (+ 12 x))} and
931 makes it the value (@emph{not} the function definition!) of
932 @code{silly}.
933
934 Here is how we might call this function:
935
936 @example
937 @group
938 (funcall silly 1)
939 @result{} 13
940 @end group
941 @end example
942
943 @noindent
944 It does @emph{not} work to write @code{(silly 1)}, because this
945 function is not the @emph{function definition} of @code{silly}. We
946 have not given @code{silly} any function definition, just a value as a
947 variable.
948
949 Most of the time, anonymous functions are constants that appear in
950 your program. For instance, you might want to pass one as an argument
951 to the function @code{mapcar}, which applies any given function to
952 each element of a list (@pxref{Mapping Functions}).
953 @xref{describe-symbols example}, for a realistic example of this.
954
955 In the following example, we define a @code{change-property}
956 function that takes a function as its third argument, followed by a
957 @code{double-property} function that makes use of
958 @code{change-property} by passing it an anonymous function:
959
960 @example
961 @group
962 (defun change-property (symbol prop function)
963 (let ((value (get symbol prop)))
964 (put symbol prop (funcall function value))))
965 @end group
966
967 @group
968 (defun double-property (symbol prop)
969 (change-property symbol prop (lambda (x) (* 2 x))))
970 @end group
971 @end example
972
973 @noindent
974 In the @code{double-property} function, we did not quote the
975 @code{lambda} form. This is permissible, because a @code{lambda} form
976 is @dfn{self-quoting}: evaluating the form yields the form itself.
977
978 Whether or not you quote a @code{lambda} form makes a difference if
979 you compile the code (@pxref{Byte Compilation}). If the @code{lambda}
980 form is unquoted, as in the above example, the anonymous function is
981 also compiled. Suppose, however, that we quoted the @code{lambda}
982 form:
983
984 @example
985 @group
986 (defun double-property (symbol prop)
987 (change-property symbol prop '(lambda (x) (* 2 x))))
988 @end group
989 @end example
990
991 @noindent
992 If you compile this, the argument passed to @code{change-property} is
993 the precise list shown:
994
995 @example
996 (lambda (x) (* x 2))
997 @end example
998
999 @noindent
1000 The Lisp compiler cannot assume this list is a function, even though
1001 it looks like one, since it does not know what @code{change-property}
1002 will do with the list. Perhaps it will check whether the @sc{car} of
1003 the third element is the symbol @code{*}!
1004
1005 @findex function
1006 The @code{function} special form explicitly tells the byte-compiler
1007 that its argument is a function:
1008
1009 @defspec function function-object
1010 @cindex function quoting
1011 This special form returns @var{function-object} without evaluating it.
1012 In this, it is equivalent to @code{quote}. However, it serves as a
1013 note to the Emacs Lisp compiler that @var{function-object} is intended
1014 to be used only as a function, and therefore can safely be compiled.
1015 Contrast this with @code{quote}, in @ref{Quoting}.
1016 @end defspec
1017
1018 @cindex @samp{#'} syntax
1019 The read syntax @code{#'} is a short-hand for using @code{function}.
1020 Generally, it is not necessary to use either @code{#'} or
1021 @code{function}; just use an unquoted @code{lambda} form instead.
1022 (Actually, @code{lambda} is a macro defined using @code{function}.)
1023 The following forms are all equivalent:
1024
1025 @example
1026 #'(lambda (x) (* x x))
1027 (function (lambda (x) (* x x)))
1028 (lambda (x) (* x x))
1029 @end example
1030
1031 We sometimes write @code{function} instead of @code{quote} when
1032 quoting the name of a function, but this usage is just a sort of
1033 comment:
1034
1035 @example
1036 (function @var{symbol}) @equiv{} (quote @var{symbol}) @equiv{} '@var{symbol}
1037 @end example
1038
1039 @node Function Cells
1040 @section Accessing Function Cell Contents
1041
1042 The @dfn{function definition} of a symbol is the object stored in the
1043 function cell of the symbol. The functions described here access, test,
1044 and set the function cell of symbols.
1045
1046 See also the function @code{indirect-function}. @xref{Definition of
1047 indirect-function}.
1048
1049 @defun symbol-function symbol
1050 @kindex void-function
1051 This returns the object in the function cell of @var{symbol}. If the
1052 symbol's function cell is void, a @code{void-function} error is
1053 signaled.
1054
1055 This function does not check that the returned object is a legitimate
1056 function.
1057
1058 @example
1059 @group
1060 (defun bar (n) (+ n 2))
1061 @result{} bar
1062 @end group
1063 @group
1064 (symbol-function 'bar)
1065 @result{} (lambda (n) (+ n 2))
1066 @end group
1067 @group
1068 (fset 'baz 'bar)
1069 @result{} bar
1070 @end group
1071 @group
1072 (symbol-function 'baz)
1073 @result{} bar
1074 @end group
1075 @end example
1076 @end defun
1077
1078 @cindex void function cell
1079 If you have never given a symbol any function definition, we say that
1080 that symbol's function cell is @dfn{void}. In other words, the function
1081 cell does not have any Lisp object in it. If you try to call such a symbol
1082 as a function, it signals a @code{void-function} error.
1083
1084 Note that void is not the same as @code{nil} or the symbol
1085 @code{void}. The symbols @code{nil} and @code{void} are Lisp objects,
1086 and can be stored into a function cell just as any other object can be
1087 (and they can be valid functions if you define them in turn with
1088 @code{defun}). A void function cell contains no object whatsoever.
1089
1090 You can test the voidness of a symbol's function definition with
1091 @code{fboundp}. After you have given a symbol a function definition, you
1092 can make it void once more using @code{fmakunbound}.
1093
1094 @defun fboundp symbol
1095 This function returns @code{t} if the symbol has an object in its
1096 function cell, @code{nil} otherwise. It does not check that the object
1097 is a legitimate function.
1098 @end defun
1099
1100 @defun fmakunbound symbol
1101 This function makes @var{symbol}'s function cell void, so that a
1102 subsequent attempt to access this cell will cause a
1103 @code{void-function} error. It returns @var{symbol}. (See also
1104 @code{makunbound}, in @ref{Void Variables}.)
1105
1106 @example
1107 @group
1108 (defun foo (x) x)
1109 @result{} foo
1110 @end group
1111 @group
1112 (foo 1)
1113 @result{}1
1114 @end group
1115 @group
1116 (fmakunbound 'foo)
1117 @result{} foo
1118 @end group
1119 @group
1120 (foo 1)
1121 @error{} Symbol's function definition is void: foo
1122 @end group
1123 @end example
1124 @end defun
1125
1126 @defun fset symbol definition
1127 This function stores @var{definition} in the function cell of
1128 @var{symbol}. The result is @var{definition}. Normally
1129 @var{definition} should be a function or the name of a function, but
1130 this is not checked. The argument @var{symbol} is an ordinary evaluated
1131 argument.
1132
1133 There are three normal uses of this function:
1134
1135 @itemize @bullet
1136 @item
1137 Copying one symbol's function definition to another---in other words,
1138 making an alternate name for a function. (If you think of this as the
1139 definition of the new name, you should use @code{defalias} instead of
1140 @code{fset}; see @ref{Definition of defalias}.)
1141
1142 @item
1143 Giving a symbol a function definition that is not a list and therefore
1144 cannot be made with @code{defun}. For example, you can use @code{fset}
1145 to give a symbol @code{s1} a function definition which is another symbol
1146 @code{s2}; then @code{s1} serves as an alias for whatever definition
1147 @code{s2} presently has. (Once again use @code{defalias} instead of
1148 @code{fset} if you think of this as the definition of @code{s1}.)
1149
1150 @item
1151 In constructs for defining or altering functions. If @code{defun}
1152 were not a primitive, it could be written in Lisp (as a macro) using
1153 @code{fset}.
1154 @end itemize
1155
1156 Here are examples of these uses:
1157
1158 @example
1159 @group
1160 ;; @r{Save @code{foo}'s definition in @code{old-foo}.}
1161 (fset 'old-foo (symbol-function 'foo))
1162 @end group
1163
1164 @group
1165 ;; @r{Make the symbol @code{car} the function definition of @code{xfirst}.}
1166 ;; @r{(Most likely, @code{defalias} would be better than @code{fset} here.)}
1167 (fset 'xfirst 'car)
1168 @result{} car
1169 @end group
1170 @group
1171 (xfirst '(1 2 3))
1172 @result{} 1
1173 @end group
1174 @group
1175 (symbol-function 'xfirst)
1176 @result{} car
1177 @end group
1178 @group
1179 (symbol-function (symbol-function 'xfirst))
1180 @result{} #<subr car>
1181 @end group
1182
1183 @group
1184 ;; @r{Define a named keyboard macro.}
1185 (fset 'kill-two-lines "\^u2\^k")
1186 @result{} "\^u2\^k"
1187 @end group
1188
1189 @group
1190 ;; @r{Here is a function that alters other functions.}
1191 (defun copy-function-definition (new old)
1192 "Define NEW with the same function definition as OLD."
1193 (fset new (symbol-function old)))
1194 @end group
1195 @end example
1196 @end defun
1197
1198 @code{fset} is sometimes used to save the old definition of a
1199 function before redefining it. That permits the new definition to
1200 invoke the old definition. But it is unmodular and unclean for a Lisp
1201 file to redefine a function defined elsewhere. If you want to modify
1202 a function defined by another package, it is cleaner to use
1203 @code{defadvice} (@pxref{Advising Functions}).
1204
1205 @node Obsolete Functions
1206 @section Declaring Functions Obsolete
1207
1208 You can use @code{make-obsolete} to declare a function obsolete. This
1209 indicates that the function may be removed at some stage in the future.
1210
1211 @defun make-obsolete obsolete-name current-name &optional when
1212 This function makes the byte compiler warn that the function
1213 @var{obsolete-name} is obsolete. If @var{current-name} is a symbol, the
1214 warning message says to use @var{current-name} instead of
1215 @var{obsolete-name}. @var{current-name} does not need to be an alias for
1216 @var{obsolete-name}; it can be a different function with similar
1217 functionality. If @var{current-name} is a string, it is the warning
1218 message.
1219
1220 If provided, @var{when} should be a string indicating when the function
1221 was first made obsolete---for example, a date or a release number.
1222 @end defun
1223
1224 You can define a function as an alias and declare it obsolete at the
1225 same time using the macro @code{define-obsolete-function-alias}:
1226
1227 @defmac define-obsolete-function-alias obsolete-name current-name &optional when docstring
1228 This macro marks the function @var{obsolete-name} obsolete and also
1229 defines it as an alias for the function @var{current-name}. It is
1230 equivalent to the following:
1231
1232 @example
1233 (defalias @var{obsolete-name} @var{current-name} @var{docstring})
1234 (make-obsolete @var{obsolete-name} @var{current-name} @var{when})
1235 @end example
1236 @end defmac
1237
1238 In addition, you can mark a certain a particular calling convention
1239 for a function as obsolete:
1240
1241 @defun set-advertised-calling-convention function signature
1242 This function specifies the argument list @var{signature} as the
1243 correct way to call @var{function}. This causes the Emacs byte
1244 compiler to issue a warning whenever it comes across an Emacs Lisp
1245 program that calls @var{function} any other way (however, it will
1246 still allow the code to be byte compiled).
1247
1248 For instance, in old versions of Emacs the @code{sit-for} function
1249 accepted three arguments, like this
1250
1251 @smallexample
1252 (sit-for seconds milliseconds nodisp)
1253 @end smallexample
1254
1255 However, calling @code{sit-for} this way is considered obsolete
1256 (@pxref{Waiting}). The old calling convention is deprecated like
1257 this:
1258
1259 @smallexample
1260 (set-advertised-calling-convention
1261 'sit-for '(seconds &optional nodisp))
1262 @end smallexample
1263 @end defun
1264
1265 @node Inline Functions
1266 @section Inline Functions
1267 @cindex inline functions
1268
1269 @findex defsubst
1270 You can define an @dfn{inline function} by using @code{defsubst} instead
1271 of @code{defun}. An inline function works just like an ordinary
1272 function except for one thing: when you compile a call to the function,
1273 the function's definition is open-coded into the caller.
1274
1275 Making a function inline makes explicit calls run faster. But it also
1276 has disadvantages. For one thing, it reduces flexibility; if you
1277 change the definition of the function, calls already inlined still use
1278 the old definition until you recompile them.
1279
1280 Another disadvantage is that making a large function inline can increase
1281 the size of compiled code both in files and in memory. Since the speed
1282 advantage of inline functions is greatest for small functions, you
1283 generally should not make large functions inline.
1284
1285 Also, inline functions do not behave well with respect to debugging,
1286 tracing, and advising (@pxref{Advising Functions}). Since ease of
1287 debugging and the flexibility of redefining functions are important
1288 features of Emacs, you should not make a function inline, even if it's
1289 small, unless its speed is really crucial, and you've timed the code
1290 to verify that using @code{defun} actually has performance problems.
1291
1292 It's possible to define a macro to expand into the same code that an
1293 inline function would execute. (@xref{Macros}.) But the macro would be
1294 limited to direct use in expressions---a macro cannot be called with
1295 @code{apply}, @code{mapcar} and so on. Also, it takes some work to
1296 convert an ordinary function into a macro. To convert it into an inline
1297 function is very easy; simply replace @code{defun} with @code{defsubst}.
1298 Since each argument of an inline function is evaluated exactly once, you
1299 needn't worry about how many times the body uses the arguments, as you
1300 do for macros. (@xref{Argument Evaluation}.)
1301
1302 Inline functions can be used and open-coded later on in the same file,
1303 following the definition, just like macros.
1304
1305 @node Function Currying
1306 @section Function Currying
1307 @cindex function currying
1308 @cindex currying
1309 @cindex partial-application
1310
1311 Function currying is a way to make a new function that calls an
1312 existing function with a partially pre-determined argument list.
1313
1314 @defun curry function &rest args
1315 Return a function-like object that will append any arguments it is
1316 called with to @var{args}, and call @var{function} with the resulting
1317 list of arguments.
1318
1319 For example, @code{(curry 'concat "The ")} returns a function that
1320 concatenates @code{"The "} and its arguments. Calling this function
1321 on @code{"end"} returns @code{"The end"}:
1322
1323 @example
1324 (funcall (curry 'concat "The ") "end")
1325 @result{} "The end"
1326 @end example
1327
1328 The @dfn{curried function} is useful as an argument to @code{mapcar}:
1329
1330 @example
1331 (mapcar (curry 'concat "The ") '("big" "red" "balloon"))
1332 @result{} ("The big" "The red" "The balloon")
1333 @end example
1334 @end defun
1335
1336 Function currying may be implemented in any Lisp by constructing a
1337 @code{lambda} expression, for instance:
1338
1339 @example
1340 (defun curry (function &rest args)
1341 `(lambda (&rest call-args)
1342 (apply #',function ,@@args call-args)))
1343 @end example
1344
1345 However in Emacs Lisp, a special curried function object is used for
1346 efficiency. @xref{Funvec Type}.
1347
1348 @node Declaring Functions
1349 @section Telling the Compiler that a Function is Defined
1350 @cindex function declaration
1351 @cindex declaring functions
1352 @findex declare-function
1353
1354 Byte-compiling a file often produces warnings about functions that the
1355 compiler doesn't know about (@pxref{Compiler Errors}). Sometimes this
1356 indicates a real problem, but usually the functions in question are
1357 defined in other files which would be loaded if that code is run. For
1358 example, byte-compiling @file{fortran.el} used to warn:
1359
1360 @smallexample
1361 In end of data:
1362 fortran.el:2152:1:Warning: the function `gud-find-c-expr' is not known
1363 to be defined.
1364 @end smallexample
1365
1366 In fact, @code{gud-find-c-expr} is only used in the function that
1367 Fortran mode uses for the local value of
1368 @code{gud-find-expr-function}, which is a callback from GUD; if it is
1369 called, the GUD functions will be loaded. When you know that such a
1370 warning does not indicate a real problem, it is good to suppress the
1371 warning. That makes new warnings which might mean real problems more
1372 visible. You do that with @code{declare-function}.
1373
1374 All you need to do is add a @code{declare-function} statement before the
1375 first use of the function in question:
1376
1377 @smallexample
1378 (declare-function gud-find-c-expr "gud.el" nil)
1379 @end smallexample
1380
1381 This says that @code{gud-find-c-expr} is defined in @file{gud.el} (the
1382 @samp{.el} can be omitted). The compiler takes for granted that that file
1383 really defines the function, and does not check.
1384
1385 The optional third argument specifies the argument list of
1386 @code{gud-find-c-expr}. In this case, it takes no arguments
1387 (@code{nil} is different from not specifying a value). In other
1388 cases, this might be something like @code{(file &optional overwrite)}.
1389 You don't have to specify the argument list, but if you do the
1390 byte compiler can check that the calls match the declaration.
1391
1392 @defmac declare-function function file &optional arglist fileonly
1393 Tell the byte compiler to assume that @var{function} is defined, with
1394 arguments @var{arglist}, and that the definition should come from the
1395 file @var{file}. @var{fileonly} non-@code{nil} means only check that
1396 @var{file} exists, not that it actually defines @var{function}.
1397 @end defmac
1398
1399 To verify that these functions really are declared where
1400 @code{declare-function} says they are, use @code{check-declare-file}
1401 to check all @code{declare-function} calls in one source file, or use
1402 @code{check-declare-directory} check all the files in and under a
1403 certain directory.
1404
1405 These commands find the file that ought to contain a function's
1406 definition using @code{locate-library}; if that finds no file, they
1407 expand the definition file name relative to the directory of the file
1408 that contains the @code{declare-function} call.
1409
1410 You can also say that a function is defined by C code by specifying a
1411 file name ending in @samp{.c} or @samp{.m}. @code{check-declare-file}
1412 looks for these files in the C source code directory. This is useful
1413 only when you call a function that is defined only on certain systems.
1414 Most of the primitive functions of Emacs are always defined so they will
1415 never give you a warning.
1416
1417 Sometimes a file will optionally use functions from an external package.
1418 If you prefix the filename in the @code{declare-function} statement with
1419 @samp{ext:}, then it will be checked if it is found, otherwise skipped
1420 without error.
1421
1422 There are some function definitions that @samp{check-declare} does not
1423 understand (e.g. @code{defstruct} and some other macros). In such cases,
1424 you can pass a non-@code{nil} @var{fileonly} argument to
1425 @code{declare-function}, meaning to only check that the file exists, not
1426 that it actually defines the function. Note that to do this without
1427 having to specify an argument list, you should set the @var{arglist}
1428 argument to @code{t} (because @code{nil} means an empty argument list, as
1429 opposed to an unspecified one).
1430
1431 @node Function Safety
1432 @section Determining whether a Function is Safe to Call
1433 @cindex function safety
1434 @cindex safety of functions
1435
1436 Some major modes such as SES call functions that are stored in user
1437 files. (@inforef{Top, ,ses}, for more information on SES.) User
1438 files sometimes have poor pedigrees---you can get a spreadsheet from
1439 someone you've just met, or you can get one through email from someone
1440 you've never met. So it is risky to call a function whose source code
1441 is stored in a user file until you have determined that it is safe.
1442
1443 @defun unsafep form &optional unsafep-vars
1444 Returns @code{nil} if @var{form} is a @dfn{safe} Lisp expression, or
1445 returns a list that describes why it might be unsafe. The argument
1446 @var{unsafep-vars} is a list of symbols known to have temporary
1447 bindings at this point; it is mainly used for internal recursive
1448 calls. The current buffer is an implicit argument, which provides a
1449 list of buffer-local bindings.
1450 @end defun
1451
1452 Being quick and simple, @code{unsafep} does a very light analysis and
1453 rejects many Lisp expressions that are actually safe. There are no
1454 known cases where @code{unsafep} returns @code{nil} for an unsafe
1455 expression. However, a ``safe'' Lisp expression can return a string
1456 with a @code{display} property, containing an associated Lisp
1457 expression to be executed after the string is inserted into a buffer.
1458 This associated expression can be a virus. In order to be safe, you
1459 must delete properties from all strings calculated by user code before
1460 inserting them into buffers.
1461
1462 @ignore
1463 What is a safe Lisp expression? Basically, it's an expression that
1464 calls only built-in functions with no side effects (or only innocuous
1465 ones). Innocuous side effects include displaying messages and
1466 altering non-risky buffer-local variables (but not global variables).
1467
1468 @table @dfn
1469 @item Safe expression
1470 @itemize
1471 @item
1472 An atom or quoted thing.
1473 @item
1474 A call to a safe function (see below), if all its arguments are
1475 safe expressions.
1476 @item
1477 One of the special forms @code{and}, @code{catch}, @code{cond},
1478 @code{if}, @code{or}, @code{prog1}, @code{prog2}, @code{progn},
1479 @code{while}, and @code{unwind-protect}], if all its arguments are
1480 safe.
1481 @item
1482 A form that creates temporary bindings (@code{condition-case},
1483 @code{dolist}, @code{dotimes}, @code{lambda}, @code{let}, or
1484 @code{let*}), if all args are safe and the symbols to be bound are not
1485 explicitly risky (see @pxref{File Local Variables}).
1486 @item
1487 An assignment using @code{add-to-list}, @code{setq}, @code{push}, or
1488 @code{pop}, if all args are safe and the symbols to be assigned are
1489 not explicitly risky and they already have temporary or buffer-local
1490 bindings.
1491 @item
1492 One of [apply, mapc, mapcar, mapconcat] if the first argument is a
1493 safe explicit lambda and the other args are safe expressions.
1494 @end itemize
1495
1496 @item Safe function
1497 @itemize
1498 @item
1499 A lambda containing safe expressions.
1500 @item
1501 A symbol on the list @code{safe-functions}, so the user says it's safe.
1502 @item
1503 A symbol with a non-@code{nil} @code{side-effect-free} property.
1504 @item
1505 A symbol with a non-@code{nil} @code{safe-function} property. The
1506 value @code{t} indicates a function that is safe but has innocuous
1507 side effects. Other values will someday indicate functions with
1508 classes of side effects that are not always safe.
1509 @end itemize
1510
1511 The @code{side-effect-free} and @code{safe-function} properties are
1512 provided for built-in functions and for low-level functions and macros
1513 defined in @file{subr.el}. You can assign these properties for the
1514 functions you write.
1515 @end table
1516 @end ignore
1517
1518 @node Related Topics
1519 @section Other Topics Related to Functions
1520
1521 Here is a table of several functions that do things related to
1522 function calling and function definitions. They are documented
1523 elsewhere, but we provide cross references here.
1524
1525 @table @code
1526 @item apply
1527 See @ref{Calling Functions}.
1528
1529 @item autoload
1530 See @ref{Autoload}.
1531
1532 @item call-interactively
1533 See @ref{Interactive Call}.
1534
1535 @item called-interactively-p
1536 See @ref{Distinguish Interactive}.
1537
1538 @item commandp
1539 See @ref{Interactive Call}.
1540
1541 @item documentation
1542 See @ref{Accessing Documentation}.
1543
1544 @item eval
1545 See @ref{Eval}.
1546
1547 @item funcall
1548 See @ref{Calling Functions}.
1549
1550 @item function
1551 See @ref{Anonymous Functions}.
1552
1553 @item ignore
1554 See @ref{Calling Functions}.
1555
1556 @item indirect-function
1557 See @ref{Function Indirection}.
1558
1559 @item interactive
1560 See @ref{Using Interactive}.
1561
1562 @item interactive-p
1563 See @ref{Distinguish Interactive}.
1564
1565 @item mapatoms
1566 See @ref{Creating Symbols}.
1567
1568 @item mapcar
1569 See @ref{Mapping Functions}.
1570
1571 @item map-char-table
1572 See @ref{Char-Tables}.
1573
1574 @item mapconcat
1575 See @ref{Mapping Functions}.
1576
1577 @item undefined
1578 See @ref{Functions for Key Lookup}.
1579 @end table