CHAPTER 26
Beginner
Lambda Expressions in C++
Updated: May 17, 2026
5 min read
# CHAPTER 26
Lambda Expressions
1. Introduction
Sometimes you need a tiny, throwaway function to do a very specific job just once (like sorting an array based on a custom rule). Writing a full function definition at the top of your file just for this one use is tedious. Lambda Expressions (introduced in C++11) allow you to write anonymous, unnamed functions directly inline exactly where you need them.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the syntax of a Lambda Expression.
-
Use the Capture Clause
[]to access external variables.
-
Pass parameters
()to a Lambda.
-
Define trailing return types
->.
-
Pass Lambdas to STL algorithms like
std::sort.
3. Syntax of a Lambda
A lambda expression consists of three main parts, plus an optional return type.Syntax: captureclause -> returntype { body };
cpp
4. Parameters and Return Types
Lambdas can take parameters just like regular functions. The return type is usually deduced automatically by the compiler, but you can specify it using the arrow->.
cpp
5. The Capture Clause []
Normal functions cannot access variables inside main(). Lambdas *can*, but you have to explicitly tell the lambda what variables to "capture".
-
[x]: Capture variablexby value (read-only copy).
-
[&x]: Capture variablexby reference (can modify the original).
-
[=]: Capture ALL external variables by value.
-
[&]: Capture ALL external variables by reference.
cpp
6. Lambdas with STL Algorithms
Lambdas shine the brightest when used as arguments for STL algorithms.
cpp
7. Memory-Level Explanation
Under the hood, a Lambda expression is not actually a function. The C++ compiler generates a hidden anonymousclass (a Functor). The variables inside the capture clause [] become private member variables of that class, and the code inside the body {} goes into an overloaded operator() function.
8. Common Mistakes
-
Modifying captured-by-value variables: If you write
[x]() { x++; }, the compiler throws an error because captured values areconst(read-only) by default. You must either capture by reference[&x]or use themutablekeyword:[x]() mutable { x++; }.
-
Dangling References: Capturing by reference
[&]in a lambda that outlives the scope of the variables it captured will cause memory corruption (just like a dangling pointer).
9. Exercises
-
1.
Write a lambda expression that takes an integer and returns
trueif it is even, andfalseotherwise.
-
2.
Create a vector of numbers. Use
std::count_ifwith a lambda to count how many numbers in the vector are greater than 10.
10. MCQ Quiz with Answers
Question 1
What is a Lambda Expression in C++?
Question 2
Which brackets represent the Capture Clause?
Question 3
What does [&] mean in the capture clause?
Question 4
What is the correct syntax to specify a return type of float for a lambda?
() b) No, they are parameterless
Answer: a) Yes, placed in the ()
Question 6
If int x = 10;, what happens in auto f = [x]() { x++; };?
Question 7
What C++ feature actually powers lambdas behind the scenes?
Question 8
Why use Lambdas with STL functions like std::sort?
return statement
Answer: b) No, the compiler usually deduces it from the return statement
Q10. Can you assign a lambda to an auto variable and call it later?
a) Yes b) No
Answer: a) Yes
11. Interview Questions
-
Q: Explain what the
mutablekeyword does in a Lambda expression.
- Q: Explain the hidden mechanics of a Lambda (how the compiler transforms it into a class/functor).
-
Q: What is the risk of capturing variables by reference
[&]in a lambda that is stored and executed later?
12. Summary
Lambda expressions provide a concise way to define anonymous functions inline. Using the capture clause[], they can seamlessly access local variables. They are heavily used in Modern C++ alongside the STL to write clean, localized, and expressive code.