WishWiki

Closure (programming)

A closure is a function that "remembers" variables from the scope where it was created, even after that scope has ended. When a function is defined inside another function, it can capture and use the outer function's variables—these bundled together form a closure.

Closures are fundamental to functional programming and enable powerful patterns like callbacks, decorators, and data encapsulation. They let you create specialized functions on the fly: a function that adds 5 to any number, or one that remembers a user's name across multiple calls.

Most modern languages support closures—Python, JavaScript, Rust, and Scheme make them especially convenient. However, closures can consume memory if they capture large objects, and their static analysis poses challenges for optimizing compilers.

The concept emerged from lambda calculus in theoretical computer science and became practical with the rise of functional languages. Understanding closures deeply unlocks more expressive, concise code and reveals how scope and first-class functions interact.

Related

Function (programming), Scope (programming), First-class function, Functional programming, Lexical scoping, Variable capture

Wishing…
your wish is being written

✨ Wish for a new page

👁 Wish for another view of this page

Sign in to WishWiki

Keep your wishes together, see your activity — and later, get your own private wiki space.

⏱ Page history