Lambda
Programming
This program demonstrates lambda expressions (anonymous functions) in C++, which allow defining functions inline without a name.
Implementation
#include <iostream>
int main() {
auto add = [](int a, int b) { return a + b; };
std::cout << "Sum: " << add(3, 7) << std::endl;
return 0;
} Lambda Syntax
A lambda expression has the following syntax:
[capture](parameters) -> return_type { body } Key Concepts
- Lambda:
[](int a, int b) { return a + b; }defines an anonymous function - Capture Clause:
[](empty) means no variables are captured from the enclosing scope - auto: Keyword that deduces the type automatically
- Lambdas are useful for short, one-time-use functions and as arguments to algorithms
Output
Sum: 10