Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Lambda Functions | Some Advanced Topics
C++ Functions

bookLambda Functions

Lambda functions, or anonymous functions, are a feature that allows you to create small, unnamed functions inline in your code. They are particularly useful when you need a simple function for a short period of time and don't want to define a separate named function.

Lambda functions are useful for several reasons:

Conciseness
expand arrow

Lambda functions allow you to write short and concise code. They are ideal for operations that can be expressed in a few lines.

Local Scope
expand arrow

They can capture variables from the surrounding scope, allowing you to use variables from the parent function within the lambda.

Flexibility
expand arrow

Lambdas can be passed as arguments to other functions, making them handy for functions like std::for_each, std::sort, etc.

How to create a lambda function?

You can use the following syntax to create lambda function.

lambda_function.h

lambda_function.h

copy
123
[capture_clause](parameter_list) -> return_type { // function body }

A capture clause in a lambda function allows you to specify which variables from the surrounding scope (outside the lambda function) can be accessed and used within the lambda function. There are 3 commonly types of capture clauses:

  • Capture Nothing []: the lambda function cannot access any variables from the surrounding scope;

  • Capture Specific Variables by Value [var1, var2, ...]: the lambda function can access specific variables from the surrounding scope by value;

  • Capture Specific Variables by Reference [&var1, &var2, ...]: the lambda function can access specific variables from the surrounding scope by reference.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int main() { int multiplier = 2; // Lambda function capturing `multiplier` by reference // With explicit return type (`int`) int result = [&multiplier](int num) -> int { return num * num * multiplier; }(5); // Invoking the lambda with argument 5 std::cout << result << std::endl; }

The function is constructed as follows:

  • The lambda function captures multiplier variable by reference [&multiplier];

  • The return type -> int specifies that the lambda function returns an integer;

  • The lambda is immediately invoked with the argument 5, and the result is stored in the result variable.

question mark

What is the purpose of a capture clause in a lambda function?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Awesome!

Completion rate improved to 5

bookLambda Functions

Deslize para mostrar o menu

Lambda functions, or anonymous functions, are a feature that allows you to create small, unnamed functions inline in your code. They are particularly useful when you need a simple function for a short period of time and don't want to define a separate named function.

Lambda functions are useful for several reasons:

Conciseness
expand arrow

Lambda functions allow you to write short and concise code. They are ideal for operations that can be expressed in a few lines.

Local Scope
expand arrow

They can capture variables from the surrounding scope, allowing you to use variables from the parent function within the lambda.

Flexibility
expand arrow

Lambdas can be passed as arguments to other functions, making them handy for functions like std::for_each, std::sort, etc.

How to create a lambda function?

You can use the following syntax to create lambda function.

lambda_function.h

lambda_function.h

copy
123
[capture_clause](parameter_list) -> return_type { // function body }

A capture clause in a lambda function allows you to specify which variables from the surrounding scope (outside the lambda function) can be accessed and used within the lambda function. There are 3 commonly types of capture clauses:

  • Capture Nothing []: the lambda function cannot access any variables from the surrounding scope;

  • Capture Specific Variables by Value [var1, var2, ...]: the lambda function can access specific variables from the surrounding scope by value;

  • Capture Specific Variables by Reference [&var1, &var2, ...]: the lambda function can access specific variables from the surrounding scope by reference.

main.cpp

main.cpp

copy
1234567891011121314
#include <iostream> int main() { int multiplier = 2; // Lambda function capturing `multiplier` by reference // With explicit return type (`int`) int result = [&multiplier](int num) -> int { return num * num * multiplier; }(5); // Invoking the lambda with argument 5 std::cout << result << std::endl; }

The function is constructed as follows:

  • The lambda function captures multiplier variable by reference [&multiplier];

  • The return type -> int specifies that the lambda function returns an integer;

  • The lambda is immediately invoked with the argument 5, and the result is stored in the result variable.

question mark

What is the purpose of a capture clause in a lambda function?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 4
some-alt