Menu
Coddy logo textTech

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: 8

The 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: 28

This 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 icon

Challenge

Easy

Create 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 num1 and num2 representing the numbers for calculations
  • A character operation representing the mathematical operation to perform

Your program should:

  1. Create a lambda expression named add that takes two integer parameters and prints their sum in the format Sum: [result]
  2. Create a lambda expression named subtract that takes two integer parameters and prints their difference in the format Difference: [result]
  3. Create a lambda expression named multiply that takes two integer parameters and prints their product in the format Product: [result]
  4. Read the two numbers and the operation character from input
  5. Based on the operation character, call the appropriate lambda with the two numbers

The operation character will be one of:

  • + for addition (call the add lambda)
  • - for subtraction (call the subtract lambda)
  • * for multiplication (call the multiply lambda)

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: 8

Parameter 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: 28

Try 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow