Closure

A closure is a record storing a function together with an environment where that function was defined.
Environment here means a set of names like variables, functions, types and etc defined in a function's "surrounding context".

Let one function be defined inside another. Then the inner function can access the variables of the outer function, because the scope object and all its local variables tied with the internal function are saved. Closure will persist as long as inner function persists. As example, inner function can live after execution outer function.

Some languages have features which simulate the behavior of closures:

  • C - via callbacks
  • C++ - via function objects
  • Objective-C - via blocks
  • Java - via local classes and lambda functions