13.10 Closures

As explained in Scoping Rules for Variable Bindings, Emacs can optionally enable lexical binding of variables. When lexical binding is enabled, any named function that you create (e.g., with defun), as well as any anonymous function that you create using the lambda macro or the function special form or the #' syntax (see Anonymous Functions), is automatically converted into a closure.

A closure is a function that also carries a record of the lexical environment that existed when the function was defined. When it is invoked, any lexical variable references within its definition use the retained lexical environment. In all other respects, closures behave much like ordinary functions; in particular, they can be called in the same way as ordinary functions.

See Lexical Binding, for an example of using a closure.

;; lexical binding is enabled.
(lambda (x) (* x x))
     ⇒ #f(lambda (x) [t] (* x x))

The internal structure of a closure is an implementation matter and we recommend against examining or altering it directly. For the curious, see Closure Function Objects.