Lambda Expression in C++
The lambda expression was introduced in C++ 11. It is used to write the inline function in C++. The code written in lambda expression cannot be reused further. The syntax for lambda expression is written as follows:
Syntax:
The general syntax to define a C++ lambda is as follows:
[capture clause] (parameters) mutable exception ->return_type
{
// definition of the lambda body
}
Return Value:
C++ lambda function executes a single expression. A value may or may not be returned by this expression. It also returns function objects using a lambda.
There are some types of variable that can be held by the lambda variable. These are as follows:
- Local variables
- Global Variables
- Captured variables (variables within [ ] )
- Arguments/Parameters
- Data members of a class
The compiler only re-evaluates the lambda expression. Below is some code for lambda expression using C++.
Example 1:
#include <iostream>
#include <string>
using namespace std;
int main()
{
auto sum = [](int p, int q) {
return p + q;
};
cout <<"Sum of two integers:"<< sum(7, 0) << endl;
return 0;
}
Output:

Example 2:
#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
std::vector<int> c = {1, 2, 3, 4, 5, 6, 7};
int x = 5;
c.erase(std::remove_if(c.begin(), c.end(), [x](int n) { return n < x; }), c.end());
std::cout << "c: ";
std::for_each(c.begin(), c.end(), [](int i){ std::cout << i << ' '; });
std::cout << '\n';
// the type of a closure cannot be named, but can be inferred with auto
// since C++14, lambda could own default arguments
auto func1 = [](int i = 6) { return i + 4; };
std::cout << "func1: " << func1() << '\n';
// like all callable objects, closures can be captured in std::function
// (this may incur unnecessary overhead)
std::function<int(int)> func2 = [](int i) { return i + 4; };
std::cout << "func2: " << func2(6) << '\n';
constexpr int fib_max {8};
std::cout << "Emulate `recursive lambda` calls:\nFibonacci numbers: ";
auto nth_fibonacci = [](int n)
{
std::function<int(int, int, int)> fib = [&](int n, int a, int b)
{
return n ? fib(n - 1, a + b, a) : b;
};
return fib(n, 0, 1);
};
for (int i{1}; i <= fib_max; ++i)
{
std::cout << nth_fibonacci(i) << (i < fib_max ? ", " : "\n");
}
std::cout << "Alternative approach to lambda recursion:\nFibonacci numbers: ";
auto nth_fibonacci2 = [](auto self, int n, int a = 0, int b = 1) -> int
{
return n ? self(self, n - 1, a + b, a) : b;
};
for (int i{1}; i <= fib_max; ++i)
{
std::cout << nth_fibonacci2(nth_fibonacci2, i) << (i < fib_max ? ", " : "\n");
}
#ifdef __cpp_explicit_this_parameter
std::cout << "C++23 approach to lambda recursion:\n";
auto nth_fibonacci3 = [](this auto self, int n, int a = 0, int b = 1)
{
return n ? self(n - 1, a + b, a) : b;
};
for (int i{1}; i <= fib_max; ++i)
{
std::cout << nth_fibonacci3(i) << (i < fib_max ? ", " : "\n");
}
#endif
}
Output:

Example 3:
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
vector<int> v = {6,7,8,9,10};
cout <<"The vector elements are: \n";
// define an inline lambda expression
// to print the vector elements
for_each(v.begin(), v.end(), [](int element) {
cout << element <<"";
});
cout <<"\n";
return 0;
}
Output:
