What Is Closure In A Programing Language

Xah Lee, 2008-05-02

There is no universal definition of the concept of closure in the context of programing languages.

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 convention, such that any global var who's name starts with a 2 underscore is meant to for special use. So, this way, you can have global vars __this , __that , __x, etc, and your function can call them. Now your function can maintain a state, while the typical problem with using global vars is avoided by a naming scheme.

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.

Once a functional language's function's has states, that is, having closure, it is natural to extend the concept to become “objects” 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 and further explanation, see: What are OOP's Jargons and Complexities.

Note: The terminology “Closure” is actually a very bad terminology. It spreads endless confusion and non-understanding. See:

The above was originally a post in comp.lang.lisp newsgroup. For most post in that thread from me, see comp.lang.lisp fight on The Meaning of Closure.


Page created: 2008-05.
© 2008 by Xah Lee.
Xah Signet