Lambdas with Parameters
Part of the Logic & Flow section of Coddy's C++ journey — lesson 43 of 56.
Now that you understand basic lambda syntax, let's make lambdas more useful by adding parameters. Just like regular functions, lambdas can accept input values to work with.
To add parameters to a lambda, place them inside the parentheses after the capture clause:
auto addNumbers = [](int a, int b) {
std::cout << "Sum: " << (a + b) << std::endl;
};
addNumbers(5, 3); // Prints: Sum: 8The parameter syntax works exactly like regular function parameters - you specify the type followed by the parameter name. You can have multiple parameters separated by commas, just as shown above.
You can also call a lambda with parameters immediately without storing it:
[](int x, int y) {
std::cout << "Product: " << (x * y) << std::endl;
}(4, 7); // Prints: Product: 28This ability to accept parameters makes lambdas much more flexible and reusable, allowing you to create small functions that can process different input values each time they're called.
Challenge
EasyCreate a program that demonstrates lambda expressions with parameters by building a simple calculator that performs different mathematical operations. This challenge will test your understanding of how to define lambda expressions that accept input values and work with them.
The following inputs will be provided:
- Two integers
num1andnum2representing the numbers for calculations - A character
operationrepresenting the mathematical operation to perform
Your program should:
- Create a lambda expression named
addthat takes two integer parameters and prints their sum in the formatSum: [result] - Create a lambda expression named
subtractthat takes two integer parameters and prints their difference in the formatDifference: [result] - Create a lambda expression named
multiplythat takes two integer parameters and prints their product in the formatProduct: [result] - Read the two numbers and the operation character from input
- Based on the operation character, call the appropriate lambda with the two numbers
The operation character will be one of:
+for addition (call theaddlambda)-for subtraction (call thesubtractlambda)*for multiplication (call themultiplylambda)
Use the following exact output format based on the operation:
For addition:
Sum: [result]For subtraction:
Difference: [result]For multiplication:
Product: [result]Remember that lambda parameters are defined inside the parentheses after the capture clause, using the same syntax as regular function parameters. Store each lambda in a variable using the auto keyword, then call the appropriate lambda based on the input operation character using an if-else statement or switch statement.
Cheat sheet
Lambda expressions can accept parameters by placing them inside parentheses after the capture clause:
auto addNumbers = [](int a, int b) {
std::cout << "Sum: " << (a + b) << std::endl;
};
addNumbers(5, 3); // Prints: Sum: 8Parameter syntax works exactly like regular function parameters - specify the type followed by the parameter name. Multiple parameters are separated by commas.
You can also call a lambda with parameters immediately without storing it:
[](int x, int y) {
std::cout << "Product: " << (x * y) << std::endl;
}(4, 7); // Prints: Product: 28Try it yourself
#include <iostream>
using namespace std;
int main() {
// Read input
int num1, num2;
char operation;
cin >> num1 >> num2 >> operation;
// TODO: Create lambda expressions for add, subtract, and multiply operations
// TODO: Use if-else or switch statement to call the appropriate lambda based on operation
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice4Maps (Key-Value Pairs)
Introducing std::mapCreating a MapAccessing and Modifying ValuesChecking for KeysRemoving PairsIterating Over a MapRecap - Word Frequency7Advanced Functions
Pass by ReferenceIntro Lambda ExpressionsLambdas with ParametersLambdas with Return ValuesIntroduction to RecursionRecursive FactorialLambda Sort2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items