Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте 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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 4

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you show me an example of a lambda function with a capture clause?

What is the difference between capturing by value and by reference in lambda functions?

When should I use a capture clause in a lambda function?

Awesome!

Completion rate improved to 5

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

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 4. Розділ 4
some-alt