Tsallaka zuwa abun ciki
Justice Tech School

Lambdas

A lambda is an inline function. [ ] captures outside variables.

Example
#include <iostream>

int main() {
  int k = 3;
  auto add = [k](int x) { return x + k; };
  auto sq = [](int x) { return x * x; };
  std::cout << add(2) << " " << sq(4) << "\n";
  return 0;
}

Result

5 16

Exercise

Capture list.

[](int x) {}

Quiz Test

What does [k] do?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.