---
title: "What is a lambda expression in C++?"  
description: "What is a lambda expression in C++?"  
author: "Sandra Emily"  
published: 2023-08-17  
updated: 2023-08-18  
canonical: https://www.mindstick.com/forum/159552/what-is-a-lambda-expression-in-c-plus-plus  
category: "visual c++"  
tags: ["c++", "lambda expression"]  
reading_time: 2 minutes  

---

# What is a lambda expression in C++?

What is a [lambda expression](https://www.mindstick.com/forum/159550/how-to-implement-an-interface-with-two-abstract-methods-by-a-lambda-expression) in C++11?

## Replies

### Reply by Aryan Kumar

A [lambda](https://www.mindstick.com/blog/181/lambda-expression-in-c-sharp) [expression](https://www.mindstick.com/articles/1861/sqlite-expressions) (also known as an anonymous function) in C++ is a short function that is defined inline. It is typically used to pass a function as an argument to another function or to create a closure.

Lambda expressions are defined using the `lambda` keyword. The syntax for a lambda expression is as follows:

C++

```plaintext
[capture] (parameters) mutable -> return_type { body }
```

- `capture`: The capture list, which specifies the variables that are captured from the surrounding scope.
- `parameters`: The parameter list, which specifies the arguments to the function.
- `mutable`: The `mutable` keyword, which specifies that the captured variables can be modified by the lambda expression.
- `return_type`: The return type of the function.
- `body`: The body of the function, which is the code that is executed when the function is called.

The following is an example of a lambda expression that adds two numbers:

C++

```plaintext
auto add_two_numbers = [](int x, int y) { return x + y; };
```

In this code, the `add_two_numbers` lambda expression takes two numbers as its arguments and returns the sum of the two numbers. The `auto` keyword is used to infer the return type of the function.

Lambda expressions can be used in a variety of places in C++, including:

- **As the argument to a function:** Lambda expressions can be used as the argument to a function. For example, the following code uses a lambda expression as the argument to the `std::sort()` function to sort a list of numbers in ascending order:

C++

```plaintext
std::vector<int> numbers = { 10, 5, 20 };

std::sort(numbers.begin(), numbers.end(), [](int x, int y) { return x < y; });
```

- **As a value:** Lambda expressions can be used as a value. For example, the following code defines a variable that stores a lambda expression that adds two numbers:

C++

```plaintext
auto add_two_numbers = [](int x, int y) { return x + y; };

int sum = add_two_numbers(10, 20);
```

- **In comprehensions:** Lambda expressions can be used in comprehensions. For example, the following code creates a list of the squares of the numbers from 1 to 10:

C++

```plaintext
std::vector<int> squares = { x * x for x in range(1, 11) };
```


---

Original Source: https://www.mindstick.com/forum/159552/what-is-a-lambda-expression-in-c-plus-plus

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
