Lambda Expressions/Functions in C++ Programming


A lambda function is a type of anonymous function that allows you to define and use a function in-place, without the need to declare it separately. In this program, a lambda function is created to calculate the sum of two integers, which are passed as parameters to the function.

The auto keyword is used to automatically deduce the return type of the lambda function. In this case, the lambda function returns an integer, which is the result of adding the two parameters.

The sum variable is initialized with the lambda function, and then called with arguments 5 and 6. The result of the function is then printed to the console using cout.

Source Code

//Lambda Expressions/Functions
#include <iostream>
using namespace std;
int main()
{
    auto sum =[](int a,int b)
    {
        return a+b;
    };
    cout <<"Total :"<< sum(5, 6) << endl;
    return 0;
}
 

Output

Total :11
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples