The short answer
A lambda function lets you write a small piece of behavior at the spot where you use it, instead of declaring a named function somewhere else. The code stays close to the place that needs it, and you don’t need a name for something you only use once.
A named function versus a lambda
Both of these programs sort a vector from largest to smallest. The first uses a named function:
#include <algorithm>
#include <vector>
bool bigger_first(int a, int b) { return a > b; }
int main() {
std::vector<int> nums = {4, 1, 7, 3};
std::sort(nums.begin(), nums.end(), bigger_first);
}
The second uses a lambda, written in the call itself:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> nums = {4, 1, 7, 3};
std::sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; });
}
std::sort does not need to know a name for the rule. What is the type of a lambda? It is a unique, unnamed class that the compiler creates for that lambda, and std::sort calls it like a function. The lambda keeps the rule next to the call. The named version is still fine, and it is often easier to read when the rule is long or used in several places.
Passing behavior to other code
Many functions accept a rule from you as an argument. Can you put a function as a parameter? Yes, and a lambda is often the simplest way to do it.
Sorting strings by length is a good example. A named function would need a name that is used only once:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"banana", "fig", "apple"};
std::sort(words.begin(), words.end(),
[](const std::string& a, const std::string& b) {
return a.size() < b.size();
});
for (const auto& w : words) {
std::cout << w << "\n"; // fig, apple, banana
}
}
Using values from the surrounding code
A lambda can capture local variables, so it can use values that exist where it is written. Without a lambda, you would have to pass that value to a named function as an extra argument, or store it in a global variable.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> scores = {55, 72, 90, 64};
int passing = 65;
auto count = std::count_if(scores.begin(), scores.end(),
[passing](int s) { return s >= passing; });
std::cout << count << "\n"; // 2, the scores 72 and 90
}
Callbacks
Many libraries let you give them a function to call later, when something happens. That function is called a callback. A lambda is a convenient way to write one, because the code that handles the event can sit next to the code that sets it up.
When a named function is better
Lambdas are not always the right choice. A named function is usually easier to work with when:
- the logic is long, or used in several places;
- a good name would explain the intent better than the code does;
- the function needs to call itself, since a lambda cannot easily refer to itself by the name it is stored under.
Are lambdas faster?
A lambda is not automatically faster or slower than a named function. When the compiler can see the lambda’s code, it can often inline it, so the cost is usually small. If speed matters for your program, measure it.
Quick answer
Lambdas let you write small behavior where you use it, pass it to other code and capture local values for it. They suit short, one-off logic best. For longer or reused logic, a named function is often the better choice.