🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C++ Notes
Topic #85

C++ Lambda


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:

#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:

#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:

#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):

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.


Capture by Reference

If you want the lambda to use the latest value of a variable (not just a copy), you can use [&] to capture it by reference.

This means the lambda will work with the original variable, not a separate copy:

int main() {
  int x = 10;

  auto show = [&x]() {
    cout << x;
  };

  x = 20;   // Change x after the lambda is created

  show();
  return 0;
}

Why? The lambda sees the original x variable, so when you change x, the lambda uses the updated value.


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.

Use a regular function when:

  • 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

Use a lambda function when:

  • 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:

Regular Function

int add(int a, int b) {
  return a + b;
}

Lambda Function

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.

Want to go beyond the notes?

Join CodingNow 2.0's C++ course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C++ Lambda – FAQs

Quick answers about learning C++ Lambda in C++.

This free note from CodingNow 2.0 explains C++ Lambda in C++ — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C++ topic on CodingNow 2.0, including C++ Lambda, is 100% free with no signup required.
With focused practice, most students grasp C++ Lambda in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now