Click here to Skip to main content
13,148,573 members (56,817 online)
Click here to Skip to main content
Add your own
alternative version


23 bookmarked
Posted 18 Oct 2012

Lambda Function in C++

, 21 Jun 2013
Rate this:
Please Sign up or sign in to vote.
Showcase for lambda function in C++


Lambda functions are quite popular in .net world and with standardization in C++11, its get included in the language standard and available with VS2010. These are type of function which can be declared anonymously and without having name.

Using the code   

Let go straight to lambda function declaration
[__strong__][1] (2) ->type {function body}(actual argument);


1 :is parent scope variable to be referred inside the lambda function, multiple variable can be separated by comma, this is quite different from .net world where we don’t need this thing
2 : is the place where you define the parameter list for the anonymous function
type : though this is not compulsory, but you can specify the return type of the lambda function from here
{functionbody} :actual anonymous function code goes here
(actual argument) :actual argument to be passed to this lambda function

Now let discuss it further with the example:-

Ex1 : Lambda function without parameter

This lambda function would return earth gravity as double type

double earthGravity = []()->double { return 9.81;}();

Ex2 : Lambda function with parameter

Say if you want to get area of rectangle by passing length and breath

int rectangleArea = [](int ilength,int iBreath)-> int
                    { return ilength*iBreath;}(10,20);

Here I create a lambda function with two parameter, return type int and passed the argument 10 and 20 and rectangleArea would contain the result 200,  once it's get executed

Ex3 : Lambda function with parent scope variable

Here I am creating two vector, first vector will contain the all the natural number from 1-10 and using lambda function we will add all the even number into the new vector which I going to pass by reference. I have explained the code in comments

#include <iostream>
#include <vector>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
	vector <int>vecInt;
	vector <int>evenNumberVec;

	//fill our main vector with dummy data
	for(int iCount =1;iCount<10;iCount++) 

	//get all even number from vector using anonymous function
	bool rFlag =
	[vecInt](vector<int> &vecTmp)->bool{
		vector<int>::const_iterator it = vecInt.begin();
		return true;

	// print them
	for(int iCount =0;iCount <evenNumberVec.size();iCount++)
	return 0;

Ex4 : Lambda function with parent scope variable (Pass By Reference)

thanks to Mr John Bandela [^]
bool rFlag =
        vector<int>::const_iterator it = vecInt.begin();
        return true;

Points of Interest  

Lambda’s looks good, hope we will hear more about it in coming days.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
India India
He used to have biography here Smile | :) , but now he will hire someone (for free offcourse Big Grin | :-D ), Who writes his biography on his behalf Smile | :)

He is Great Fan of Mr. Johan Rosengren (his idol),Lim Bio Liong, Nishant S and DavidCrow and Believes that, he will EXCEL in his life by following there steps!!!

For good 8 years he was Visual CPP MSMVP!

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
aydinsahin25-Jun-13 4:36
memberaydinsahin25-Jun-13 4:36 
AnswerRe: My vote of 5 Pin
ThatsAlok25-Jun-13 22:25
memberThatsAlok25-Jun-13 22:25 
GeneralMy vote of 4 Pin
Kishore Debnath21-Jun-13 21:44
memberKishore Debnath21-Jun-13 21:44 
GeneralRe: My vote of 4 Pin
ThatsAlok21-Jun-13 21:58
memberThatsAlok21-Jun-13 21:58 
Questionfeedback Pin
Dan page21-Jun-13 3:12
memberDan page21-Jun-13 3:12 
AnswerRe: feedback Pin
ThatsAlok21-Jun-13 6:02
memberThatsAlok21-Jun-13 6:02 
GeneralMy vote of 5 Pin
Espen Harlinn20-Oct-12 7:59
mvpEspen Harlinn20-Oct-12 7:59 
QuestionMy vote of 5 Pin
John Bandela19-Oct-12 5:54
memberJohn Bandela19-Oct-12 5:54 
AnswerRe: My vote of 5 Pin
ThatsAlok19-Oct-12 6:28
memberThatsAlok19-Oct-12 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 21 Jun 2013
Article Copyright 2012 by ThatsAlok
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid