Lambda Function
- In Turkish
- Lambda fonksiyonu
In short
A lambda function is a small function written inline without a name, usually passed straight to another function such as a sorting or filtering helper.
What is a lambda function?
A lambda function, also called an anonymous function or lambda expression, is a function defined without giving it a name. It is usually short and written right where it's needed, most often as an argument to another function. The name comes from lambda calculus, a mathematical model of computation from the 1930s that uses the Greek letter λ to define functions.
Each language has its own syntax. JavaScript has arrow functions like (x) => x * 2, Python has lambda x: x * 2, Java writes x -> x * 2, C# writes x => x * 2, and C++ uses [](int x) { return x * 2; }. Lambdas can usually read variables from the surrounding code, which makes them closures. Python deliberately limits lambdas to a single expression, so anything longer should be a normal named function.
A lambda is like a sticky note with a quick instruction handed to a coworker, such as "sort these by date", instead of a formal written procedure. Lambdas are used for sort keys, list filters, event handlers and short callbacks, where a separate named function would add noise without adding clarity.
A lambda is not a different kind of function; it behaves like any other function and simply has no name. It is also unrelated to serverless cloud platforms that call their hosted functions "lambdas", which are small programs run on demand and can be written with or without lambda expressions. And a lambda is not automatically a higher-order function: it is usually the function passed into one.
Key takeaways
- A lambda is a function without a name, defined inline where it is used.
- Syntax varies:
=>in JavaScript and C#,->in Java,lambdain Python. - Lambdas usually capture surrounding variables, which makes them closures.
- They are ideal for short callbacks such as sort keys and filters.
- Serverless cloud functions share the name but are a different concept.
Example
users = [
{"name": "Grace", "age": 45},
{"name": "Ada", "age": 36},
{"name": "Linus", "age": 28},
]
# The lambda tells sorted() which value to sort by
by_age = sorted(users, key=lambda user: user["age"])
print([u["name"] for u in by_age]) # ['Linus', 'Ada', 'Grace']
# The same idea in JavaScript: users.sort((a, b) => a.age - b.age)Readers ask
What is the difference between a lambda and a regular function?
A lambda has no name and is written inline, usually as a single expression, while a regular function is declared with a name and can contain many statements. Once created, both are called the same way.
Are arrow functions the same as lambdas?
Arrow functions are JavaScript's lambda syntax. They also differ from regular JavaScript functions in one important way: they don't have their own this, so they use the this of the surrounding code.
Why is it called a lambda?
The name comes from lambda calculus, a formal system created by mathematician Alonzo Church in the 1930s that uses the Greek letter λ to write functions. Lisp adopted the word, and many languages followed.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- Higher-Order FunctionProgramming Fundamentals, p. 25A higher-order function is a function that takes another function as an argument, returns a function, or both, so behavior can be passed around like data.
- CallbackProgramming Fundamentals, p. 7A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
- Functional ProgrammingProgramming Fundamentals, p. 22Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
- ServerlessDevOps & Cloud, p. 47Serverless is a cloud model in which the provider runs your code on demand, manages all the servers, scales automatically, and bills only for actual use.
Spotted a mistake or something missing on this page?Suggest an edit