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.
| Language | Syntax Example | Max Statements | Return Keyword Needed? |
|---|---|---|---|
| Python | lambda x: x * 2 | Single expression | No (implicit) |
| JavaScript | (x) => x * 2 | Multiple (with braces) | Only with braces |
| Java | x -> x * 2 | Single expression / block | Only in block form |
| C# (LINQ) | x => x * 2 | Expression or block | Only in block form |
| Haskell | \x -> x * 2 | Single expression | No (implicit) |
Wikimedia Commons, CC BY-SA
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.
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 the Greek letter λ (lambda). Alonzo Church introduced λ-calculus in the 1930s to formalise mathematical functions. John McCarthy adopted the term for Lisp in 1958, embedding it permanently in programming language theory.