A closure is a function that retains access to the variables of its enclosing lexical scope even after that scope has finished executing. This allows inner functions to "close over" free variables from outer functions, preserving state without using global variables. Closures are fundamental to functional programming, event handling, and creating data privacy in languages like JavaScript, Python, and Go.
| Language | Closure Support | Capture Style | Common Use Case |
|---|---|---|---|
| JavaScript | Yes | By reference | Event handlers, module pattern |
| Python | Yes | By reference (late binding) | Decorators, callbacks |
| Go | Yes | By reference | Goroutine captures |
| Java | Partial (lambdas) | Effectively final only | Stream API, listeners |
| C | No (manual only) | N/A | Function pointers + structs |
Wikimedia Commons, CC BY-SA
A lambda function is an anonymous, inline function expression defined without a formal name, typically used for short, single-use operations. Derived from lambda calculus (λ-calculus), lambda functions can be passed as arguments, returned from other functions, and stored in variables, making them essential to functional programming paradigms. They are supported in Python (lambda keyword), Java (->), JavaScript (=>), Haskell, and most modern languages.
Functional programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state or mutable data. Core principles include pure functions (same input always yields same output, no side effects), immutability, first-class and higher-order functions, and function composition. FP promotes code that is predictable, testable, and parallelisable, and is embodied in languages such as Haskell, Erlang, Clojure, and adopted extensively in Scala, F#, and modern JavaScript/Python.
From Latin "claudere" (to close/shut). The computing term was formalised by Peter Landin in 1964 when describing SECD machine environments, referring to the "closing" of an expression over its free variables.