Lambdas Basics
Lambda expressions are a core feature of C++ that enable you to create small, unnamed function objects directly within your code. They are especially useful for functional programming patterns, such as passing behavior to algorithms, creating concise callbacks, or defining quick, one-off operations. With lambda expressions, you can write more expressive and compact code by embedding logic where it is needed, rather than defining separate named functions. This makes them a powerful tool for writing modern, readable C++.
main.cpp
12345678910111213#include <iostream> int main() { // Define a lambda that adds two integers and prints the result auto add_and_print = [](int a, int b) { std::cout << (a + b) << std::endl; }; add_and_print(3, 5); // Output: 8 return 0; }
The syntax for a lambda expression in C++ consists of several parts. It starts with square brackets [ ], known as the capture list, which will be discussed in detail later. Next comes the parameter list ( ), just like in a regular function, where you specify the arguments the lambda accepts. After the parameter list, you can optionally specify the return type using ->, but in most cases, C++ can deduce the return type automatically based on the expression in the body. The body of the lambda is enclosed in curly braces { }, where you write the logic to execute. For example, in the lambda [](int a, int b) { std::cout << (a + b) << std::endl; }, there is no explicit return type, so C++ deduces it as void since there is no return statement. This concise syntax allows you to define functions inline, making your code more readable and maintainable.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 4.55
Lambdas Basics
Swipe to show menu
Lambda expressions are a core feature of C++ that enable you to create small, unnamed function objects directly within your code. They are especially useful for functional programming patterns, such as passing behavior to algorithms, creating concise callbacks, or defining quick, one-off operations. With lambda expressions, you can write more expressive and compact code by embedding logic where it is needed, rather than defining separate named functions. This makes them a powerful tool for writing modern, readable C++.
main.cpp
12345678910111213#include <iostream> int main() { // Define a lambda that adds two integers and prints the result auto add_and_print = [](int a, int b) { std::cout << (a + b) << std::endl; }; add_and_print(3, 5); // Output: 8 return 0; }
The syntax for a lambda expression in C++ consists of several parts. It starts with square brackets [ ], known as the capture list, which will be discussed in detail later. Next comes the parameter list ( ), just like in a regular function, where you specify the arguments the lambda accepts. After the parameter list, you can optionally specify the return type using ->, but in most cases, C++ can deduce the return type automatically based on the expression in the body. The body of the lambda is enclosed in curly braces { }, where you write the logic to execute. For example, in the lambda [](int a, int b) { std::cout << (a + b) << std::endl; }, there is no explicit return type, so C++ deduces it as void since there is no return statement. This concise syntax allows you to define functions inline, making your code more readable and maintainable.
Thanks for your feedback!