Flash cards
Review the key moves
What is the main idea behind C++ Lambda Functions?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
] ( ___ ) {Put the learning moves in the order that makes the concept easiest to apply.
Lambda Functions
A lambda function is a small, anonymous function you can write directly in your code. It's useful when you need a quick function without naming it or declaring it separately.
Think of it as a "mini function on the fly."
Syntax
[ capture
] ( parameters ) {
code
};Don't worry: We'll explain what [ capture ] means later. For now, let's just use an empty pair of brackets.
Basic Lambda Example
Here, message holds a lambda function that prints a message to the screen:
Example
int main() {
auto message = []() {
cout << "Hello World!\n";
};
message();
return 0;
}Lambda with Parameters
You can pass values into a lambda just like a regular function:
Runnable example
#include <iostream>
using namespace std;
int main() {
auto add = [](int a, int b) {
return a + b;
};
cout << add(3, 4);
return 0;
}Passing Lambdas to Functions
You can also pass a lambda function as an argument to another function.
This is useful when you want to tell a function what to do , not just what data to use.
In the example below, we send a small lambda function to another function, which then runs it twice:
Runnable example
#include <iostream>
#include <functional> // Needed for std::function
using namespace std;
// A function that takes another function as parameter void myFunction(function<void()> func) { func(); func();
}
int main() {
auto message = []() {
cout << "Hello World!\n";
};
myFunction(message);
return 0;
}Note that you must include the <functional> library for this example to work.
Using Lambdas in Loops
You can define and use a lambda function inside a loop, which are great for quick actions:
Runnable example
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 3; i++) {
auto show = [i]() {
cout << "Number: " << i << "\n";
};
show();
}
return 0;
}Capture Clause []
You can use the [ ] brackets to give a lambda access to variables outside of it.
This is called the capture clause .
In this example, the lambda captures the variable x by value (a copy):
Runnable example
int main() {
int x = 10;
auto show = [x]() {
cout << x;
};
show();
return 0;
}Note
The lambda uses a copy of x . If you change x after defining the lambda, it won't affect the value inside the lambda.
Note
You can also use [&] to capture by reference.
Regular Functions vs Lambda Functions
Both regular functions and lambda functions let you group code and run it later, but they are used in slightly different situations.
- You plan to reuse the function in multiple places
- You want to give the function a clear, meaningful name
- The logic is long or complex
- You only need the function once
- The code is short and simple
- You want to pass a quick function into another function
Both of these examples do the same thing. They return the sum of two numbers:
int add(int a, int b) {
return a + b;
}auto add = [](int a, int b) {
return a + b;
};Note
The lambda version is great when you don't need to reuse the function later. It's quick and works well inside blocks or as arguments to other functions.