What is Closure in Programing Language

By Xah Lee. Date: . Last updated: .

Closure is a feature of programing language design, for functions.

Closure means the programing language function maintains its free variables properly. (free variables are variables used in a function that are neither local variables nor parameters of that function. Usually, a global variable or a variable of a outer function the current function is in.)

  1. Let's say you define a function f.
  2. Inside f, you declare local variable x.
  3. Inside f, you also define a function g (a inner function).
  4. The function g uses variable x.
  5. Let's say the function f returns the function g.
  6. Now, outside f, you call f, it returns g.
  7. Now, g uses x, but x is inside f, but f is already gone. What to do with x?
  8. If the programing language is designed in such way that x still exist, we say, the programing language support closure (it properly manages free variable), and g is a closure. Otherwise, the programing language does not support closure (such as programing language C)

Closure Explained, Way 2

Closure is a programing language feature for a specific property of how the language's function handles variable. It means, a function such that all variables it uses will still work in abnormal situations. Abnormal situation means, when function returns a function and the inner function uses outer function's variable but the inner function is no longer in the context of outer function.

Closure Explained, Way 3

Closure is function that carries a environment for all the variables it uses.

Closure Explained, Way 4

Closure is a computer language feature. It is a function with persistent local variable.

In other words, a function that has local variables that retain its values when the function call is finished, but the variable is not accessible outside the function definition.

Meaning of Closure in Practice

In practice, in most programing languages, a closure is a function that maintains a internal state, in a clean way, without using global variable. The variable of the function are hidden and private to the function.

Example of Closure

Meaning of Closure Outside Computer Science

Outside of computer science, “closure” also means:

Closure is Programing Language Design Concept, Not Math/Algorithm Concept

Closure, as a feature of computer language design, is not a mathematical concept. It has no point in the science of algorithms.

A function, by having closure, ceases to be a function in a mathematical sense. Because now it may have side-effect. That is, the function's output, is not purely dependent on its arguments. But the concept of “function” in math has no side-effect, by definition.

Closure as Stateful Function, Has the Same Interface as Function Using Global Variable for State

If you consider closure in practice as function with a internal state, and if you consider it as an Interface, as in API, you can see that the so-called “closure” is no different from a function using global variables. No difference in Interface, but just implementation difference.

Global Variable as Alternative to Closure

Basically, if your language can define a function, and maintain a permanent internal variable inside, it's closure.

For example, if you define a global var, and your function access and or set that global var, your function will be able to maintain a state.

However, we all know global vars are bad because, for example, you have this big pool where everyone and every function accesses; it's hard to maintain and could be over-ridden by mistake.

So, a crude way to remedy this, is by following a naming convention. For example, create global variables whose whose name starts with a dollar sign, followed by the function name, followed by a random string. e.g. $getSession__ttlqtpvz8i. Now your function getSession can maintain a state, while the typical problem with using global vars is avoided.

Now, effectively, your function is said to have “closure”. Typically, that word is used when the language provide means to allow function to have a state without the above hackish method. However, it's no more complicated than that. In fact, it is effectively the only means to implement closure. If the language hides such global vars from user, and provide transparent ways when you create or access them inside your function without some naming scheme hack, that's closure.

Closure's Connection to Object Oriented Programing

Once a functional language's function has private state, it is natural to extend the concept to become “object” as in Object Oriented Programing.

When you start to program in a way of closures, that is, a function uses permanent internal variables to maintain states, and you have many functions like that, naturally, you might systematically explore this paradigm. Namely, instead of just one or two permanent internal variables, each of your function uses tens of such permanent internal vars. You might also start to define inner subroutines. Specifically, functions that lives only inside your function. You might apply these inner functions on your internal vars. This is the starting concept of so-called Object Oriented Programing. (For detail, see: What are OOP's Jargons and Complexities.)

Note: with respect to terminology, “closure” is a bad term. It spreads endless confusion and non-understanding. See:

Better Name: Lexical Enclosure

Tom Novelli suggested that “closure” should be named “enclosure”, or the full “lexical enclosure”. Excellent suggestion!

Different Meaning of Closure: Language Feature vs API

when we think of closure, we think of function with state. however, in some tech sense, closure is just a feature of f of the lang, not necessarily having a state. So, emacs lisp calls all lexical scoped function closure, even if it's just a normal function.

emacs lisp closure 2018-07-23
emacs lisp closure 2018-07-23

the thing to note here is that, often, a tech jargon have entirely different meanings. e.g. closure as language feature in lang design sense is entirely different from closure as function with state from programer perspective.

Programing Language Design Problem, Conflict of Interest, Closure or Side-Effect

closure (function with state) should be ban'd. Because, it is no longer a function in the math sense. Side effect becomes the point. It's, a abomination. From a lang design feature point of view, closure is watered down form of class in Object Oriented Programing.

in programing language design, you want simplicity principle. e.g. since subroutine take value and return value, it should consider function as value, and able to take function or return function. No exceptions. Simplicity.

But now you got a problem, when some coder write inner f2 that uses outer f1 var and return f2, a closure! thus creating side effect. So, how to resolve this problem?

One naive solution, is to still allow function as input/output, but ban function with side effect. Usually, you can't just ban side effect function. First your language must to be able to detect it. The solution of ban side effect function usually comes in other forms, e.g. Language with typed var.

Ban Nested Closure?

now let's consider nested closure…

when closure is nested, you have realm within a realm.

by the way, did you know, realm is a technical jargon in ECMA-262? #JavaScript

JavaScript spec realm 2018-07-24 9c3c2
JavaScript spec realm 2018-07-24