Codectionary / Developer documentation / C++

C++ Functions & Lambdas

Functions declare a return type, name, and parameters. Lambdas are anonymous callable objects and can capture surrounding values explicitly.

Syntax

int add(int left, int right) { return left + right; }
auto double_it = [](int value) { return value * 2; };

Examples

A simple lambda

A small, runnable example of this syntax.

#include <iostream>

int main() {
  auto double_it = [](int value) { return value * 2; };
  std::cout << double_it(6) << "\n";
}

Best practices

  • Keep lambda captures explicit: capture only the surrounding values the lambda needs.
  • Keep examples small while learning, then combine the idea with a real project.

Runtime & versions

Compiled native applications. Feature availability depends on the language and runtime version used by your project; consult the official reference below.

ISO C++ resources

Specifications & further reading