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.

At a glance

Purpose
Systems and general-purpose software
File extension
.cpp ยท .hpp
Runs in
Compiled native applications
Usually used with
Compiler and standard library

Specifications & further reading

Related C++ documentation