LOGBOOK

HELP

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);

From Quiz: REVE1 / C++ Programming | Updated: Jul 06, 2026