← Back to C++ Course | Chapter 5: Functions | Lesson 11 of 13

C++ Lambda Functions

A lambda is a small nameless function you write right where you need it, like a quick sticky-note instruction. It can also remember values from around it.
Syntax
cpp
auto lambda_name = [](type param) {
  return expression;
};

lambda_name(argument);

Basic Lambda Syntax

A lambda is a small, unnamed function you can define right where you need it, inside an expression, using the syntax [](parameters) { body }.

They're especially useful for short pieces of logic you only need once, like a custom comparison passed directly into std::sort.

Example: Basic Lambda Syntax

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in sayHello
	auto sayHello = []() { std::cout << "Hello from a lambda!" << std::endl; };
	sayHello();
	// Return 0 to signal that the program finished successfully
	return 0;
}

Lambda with Parameters

Lambdas accept parameters exactly the way regular functions do, written inside the parentheses — [](int a, int b) { return a + b; } takes two ints and returns their sum, just as a named function with the same signature would.

Example: Lambda with Parameters

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in add
	auto add = [](int a, int b) { return a + b; };
	// Print to the console with cout
	std::cout << add(3, 4) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Lambda Return Types

A lambda's return type is usually inferred automatically from its body, but you can specify it explicitly with a trailing arrow, like [](int x) -> double { return x / 2.0; }, which is occasionally necessary when the compiler can't unambiguously infer the intended type on its own.

Example: Lambda Return Types

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in divide
	auto divide = [](int a, int b) -> double { return (double)a / b; };
	// Print to the console with cout
	std::cout << divide(7, 2) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Lambda Capture Clause

The capture clause [] controls which variables from the surrounding scope the lambda can access inside its body: [=] captures everything by value (a copy), while [&] captures everything by reference (direct access to the originals), and you can also list specific variable names to capture just those.

Example: Lambda Capture Clause

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare factor and set it to 10
	int factor = 10;
	// Define a lambda (anonymous function) stored in multiply
	auto multiply = [=](int x) { return x * factor; };
	// Print to the console with cout
	std::cout << multiply(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Generic Lambdas

Since C++14, marking a lambda's parameters with auto instead of a specific type — [](auto x, auto y) { return x + y; } — creates a generic lambda that can accept different argument types at different call sites, similar in spirit to a template function but written inline.

Example: Generic Lambdas

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Define a lambda (anonymous function) stored in add
	auto add = [](auto x, auto y) { return x + y; };
	// Print to the console with cout
	std::cout << add(2, 3) << std::endl;
	// Print to the console with cout
	std::cout << add(2.5, 3.5) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using a variable inside the lambda without capturing it, which is a compile error.
  2. Capturing by value with [x] and expecting the lambda to change the original, when it only modifies its own copy (and needs mutable).
  3. Capturing a local by reference [&] and calling the lambda after the variable is destroyed, which is a dangling reference.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.