codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ lambda

C++ lambda

Small unnamed functions.

Syntax: [capture](params){ body }. Capture outside variables by value [=] or by reference [&].

Example code

#include <iostream>
using namespace std;
int main() {
  int k = 3;
  auto mul = [k](int x) { return x * k; };
  cout << mul(5) << endl;
  return 0;
}

Run this code in the online editor

← C++ recursionC++ OOP, classes and objects →