Skip to main content

Lambda Function

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/lambda-function

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, lambda in 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

Sorting with a lambda in Pythonpython
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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings