Main menu

Pages

C++ Lambda expression - Learn CPP, In the C++ programming language, a new programming method has been added that can be used to reduce the size of the code when defining new functions, and this method is called (Lambda Expressions), (Closures), (Literals Function) or (Lambdas), and usually What this programming approach is to take advantage of the concept of encapsulation, encapsulating a few lines of code passed to algorithms or asynchronous functions, has been used starting with C++11.

The name lambda expression came from the science of (lambda calculus), a mathematical form invented in the thirties by (Alonzo Church) that came to answer some questions related to logic, and (lambda expression) also formed the foundations of the programming language (LISP). 


C++ Lambda expression - Learn CPP



Lambda Expression Definition General Form


The following code shows the general layout of Lambda Expression:

[captures](parameters)-> return Types Declaration { lambda Statements;};


Place of the word (captures)


Values ​​for variables defined outside the function or their addresses are placed only in order to be accessed and handled from within the function, and two empty squares ([]) must be placed if no parameters are set in them.


word place (parameters)


Parameters are set to the function, indicating that the programmer can not put the parentheses () at all if he does not want to put (parameters) in it.


Return Types Declaration


The type of value that will be returned is passed when the execution process is completed, as the Compiler can most likely know what the function will return automatically if it contains only one command, but it is always preferable to specify the return type by the programmer.


Place of the word (lambda Statements)


The commands that the programmer wants to be executed are placed when the function is called.


The basics of working with Lambda Expressions in the C++ programming language


A regular function in C++ is given by the programmer with a name when it is defined, and later when the code is executed, it is called by typing its name and this is how the Compiler will understand that it should be executed. A function defined in Lambda Expressions is not given a name but is assigned to a variable of the same type so that we can call it from it.