Lambda Expressions in c++

Lambda Expressions in c++


In this tutorial, we are going to learn about lambda expressions in C++.

Firstly, what is a lambda function?

The lambda expression is an anonymous function that provides a brief and functional syntax, which is used to write anonymous functions. The programming of a function is a body concept, which is used for creating expression tree types or delegates. In C++ 11 version, lambda expressions were introduced to allow us to write an inline function that can be used for short snippets of code. The syntax of the lambda expressions is as follows.

 This is the syntax of the generic lambda expressions or anonymous functions.

                     [  capture clause  ]  (  parameters  ) - > return type
                          //definition of the method

The return type in lambda expressions is not that mandatory as the return type can be evaluated by the compiler and there is no need to specify the return type. But in some complex cases, like in a conditional statement, the compiler can’t evaluate the return type and we need to specify the return type in those cases.


Let’s look at the advantages of anonymous functions over normal functions.

  • Lambdas are very cheap in memory and CPU usage. It never costs greater than or equivalent to the cost of a class or a function.
  • Lambda allocates on a stack not rather than on a heap. This may be advantageous in some cases.
  • We can return a lambda expression. It is just similar to std::function.
  • Captured variables in the lambda function are immutable by default.


Now let’s look at some of the disadvantages of the lambda expressions

  • Developers find the syntax of the lambda expressions confusing.
  • They also think that lambda expressions are more complex to use.
  • As it is advantageous sometimes as those lambda functions are allocated on a stack not rather than on a heap, it may be disadvantageous too in some cases.
  • The syntax of the lambda expressions is hard to remember.
See also  Features of C++ 11

Let’s look at a sample program implementing lambda expressions.

using namespace std;
int main() {
   vector<int> v {1,2,3,4,5};
   for_each(v.begin(), v.end(), [](int x) {
      cout <<"square of "<<x<< " is " <<x*x <<endl;


In the above program, we included the vector class which is used to create a vector, and we also included algorithm header which contains the for_each loop.

In the above program, we created a vector and a the for_each loop is iterated over the given vector by passing the starting and ending indices of the vector.

Let’s see some more examples based on lambda expressions.

#include <bits/stdc++.h>
using namespace std;
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int f = accumulate(arr, arr + 10, 1, [](int i, int j)
        return i * j;

The output of the program is.

In the above program, accumulate is a kind of lambda function which accumulates the container based on the function provided as the third argument. The third argument is representing two integers traversing through the array list which returns the product of the two integers.

#include <bits/stdc++.h>
using namespace std;
    auto square = [](int i)
        return i * i;
    cout << "Square of 5 is : " << square(5) << endl;

The output of the program is.

The above program is just a sample program based on the lambda expressions which returns the square of the number.

Lambda expressions can have more power than an ordinary function by having variables from the enclosing scope. We can capture the enclosing scope in three ways

  • Capturing by reference [&]
  • Capturing by value [=]
  • Capturing by mixed capture [a,&b]
See also  Operator Overloading in c++


That’s it from this tutorial. Hope you guys found it interesting. We have learned about lambda expressions and different kinds of lambda expressions with examples. If you have any queries, leave a comment below the article. Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.