Understanding Lambda Functions in C++ with Examples

Hello everyone today we’re going to learn about Lambda functions.Lambda functions, introduced in C++11, provide a concise way to define anonymous functions in your code. These functions are sometimes referred to as “lambda expressions” and offer a more compact syntax compared to traditional function declarations. Lambda functions are particularly useful in situations where you need a short, throwaway function for a specific task. In this article, we’ll explore the syntax and usage of lambda functions in C++ through examples.

Basic Syntax of Lambda Functions

The basic syntax of a lambda function looks like this:

cpp
[capture](parameters) -> return_type {
    // function body
}

Let’s break down the components:

  • Capture Clause ([capture]): This optional part allows you to capture variables from the surrounding scope. The capture clause specifies which external variables the lambda function can access.
  • Parameters ((parameters)): Similar to regular functions, lambda functions can take parameters.
  • Return Type (-> return_type): Specifies the return type of the lambda function. This part is optional if the return type can be inferred.
  • Function Body ({ /* function body */ }): Contains the actual implementation of the lambda function.

Examples of Lambda Functions

Example 1: Basic Lambda Function

#include <iostream>

int main() {
    // Lambda function without parameters and return type
    auto greet = [] {
        std::cout << "Hello, lambda!" << std::endl;
    };

    // Call the lambda function
    greet();

    return 0;
}

In this example, we define a lambda function called greet that takes no parameters and has no return type. The lambda function is then invoked, printing “Hello, lambda!” to the console.

Example 2: Lambda Function with Parameters

#include <iostream>

int main() {
    // Lambda function with parameters
    auto add = [](int a, int b) -> int {
        return a + b;
    };

    // Call the lambda function
    int result = add(5, 3);
    std::cout << "Sum: " << result << std::endl;

    return 0;
}

In this example, we create a lambda function named add that takes two integer parameters and returns their sum. The lambda function is then called with arguments 5 and 3, and the result is printed to the console.

Example 3: Lambda Function with Capture Clause

#include <iostream>

int main() {
    // Capture external variable in lambda function
    int x = 10;
    auto multiply = [x](int y) -> int {
        return x * y;
    };

    // Call the lambda function
    int result = multiply(3);
    std::cout << "Result: " << result << std::endl;

    return 0;
}

In this example, we introduce the capture clause by capturing the external variable x in the lambda function multiply. The lambda function multiplies the captured variable x by its parameter y.

Lambda functions provide a concise and expressive way to write inline functions in C++. They are especially useful in scenarios where a short-lived function is needed, such as in algorithms or functional-style programming. By understanding the syntax and usage of lambda functions, we can leverage this feature to write more readable and efficient code in C++.

That’s all for now. In the future we are going to go a bit deeper into Lambda functions and see it in action with few more examples. Thanks for reading.

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE

Related :

Follow :