Anonymous Functions in C++

Anonymous functions in C++, also known as lambda expressions, are unnamed functions that can be defined inline and used immediately. Introduced in C++11, these functions are concise and allow for capturing variables from their surrounding scope. Anonymous functions are particularly useful for tasks like callbacks, functional programming, and passing logic to algorithms.


Syntax

</>
Copy
[capture_list](parameter_list) -> return_type {
    // Function body
};
capture_list
Specifies variables from the surrounding scope to be captured. Use [] for no capture.
parameter_list
Defines the parameters of the lambda, similar to regular functions.
return_type
Specifies the return type. If omitted, the compiler deduces it automatically.
Function body
The code executed when the lambda is called.

Examples

Example 1: Basic Anonymous Function

This example demonstrates how to define and use an anonymous function to calculate the square of a number.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    auto square = [](int x) {
        return x * x;
    };

    cout << "Square of 5: " << square(5) << endl;
    return 0;
}

Output:

Square of 5: 25

Explanation:

  1. The lambda square is defined as [](int x) { return x * x; }, which takes one integer and returns its square.
  2. The lambda is assigned to the variable square.
  3. The lambda is called with the argument 5, and the result is printed.

Example 2: Capturing Variables by Value

This example demonstrates how to capture variables from the surrounding scope by value.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int base = 10;

    auto addBase = [base](int x) {
        return base + x;
    };

    cout << "Base + 5: " << addBase(5) << endl;
    return 0;
}

Output:

Base + 5: 15

Explanation:

  1. The variable base is declared in the main function.
  2. The lambda addBase captures base by value using the capture list [base].
  3. When the lambda is called with 5, it adds the captured value of base to 5.

Example 3: Capturing Variables by Reference

This example shows how to capture variables from the surrounding scope by reference.

</>
Copy
#include <iostream>
using namespace std;

int main() {
    int count = 0;

    auto increment = [&count]() {
        ++count;
    };

    increment();
    increment();

    cout << "Count: " << count << endl;
    return 0;
}

Output:

Count: 2

Explanation:

  1. The variable count is declared in the main function.
  2. The lambda increment captures count by reference using [&count].
  3. Each call to increment modifies the original count variable, incrementing its value.

Key Points to Remember about Anonymous Functions

  1. Anonymous functions (lambdas) allow for concise and inline function definitions.
  2. They can capture variables from their surrounding scope by value, reference, or both.
  3. The mutable keyword allows modifying captured-by-value variables.
  4. They are useful in functional programming, callbacks, and algorithms like std::for_each.
  5. Lambdas support trailing return types and can be assigned to std::function for flexibility.