Quiz Entry - updated: 2026.07.06
How do you write a simple lambda that adds two numbers?
auto add = [](int a, int b) { return a + b; }; — then call it like any function: add(3, 4).
auto add = [](int a, int b) { return a + b; };
int result = add(3, 4); // result = 7
Breakdown:
auto add— let the compiler deduce the lambda's (unnameable) type[]— no captures needed; it uses only its parameters(int a, int b)— two int parameters{ return a + b; }— body returns the sum
Using it immediately, without naming it:
int result = [](int a, int b) { return a + b; }(3, 4);