Menu

Sign in to track your progress and unlock all features.

Theme style

Log in
01. Build an Adder Function FactoryE02. Create a Multiplier FactoryE03. Make a Simple Call CounterE

Problem No 2

Create a Multiplier Factory

Easy

10 minute session

Summary

Build a closure that captures a multiplier and returns a function to multiply inputs by that value.

Problem statement

Write a function factory make_multiplier(m) that returns a new function. The returned function should accept a single numeric argument x and return x * m. Ensure the multiplier m is captured in the closure so that the returned function continues to use m even after make_multiplier returns. Avoid using global variables.

Task

Implement make_multiplier(m) which returns a function that multiplies its argument by m, reinforcing how closures preserve configuration values.

Examples

Basic usage

Input

make_multiplier(4)(3)

Output

12

Explanation

make_multiplier(4) returns a function that multiplies by 4. Calling it with 3 returns 3 * 4 = 12.

Input format

A single expression calling make_multiplier with a number and then calling the returned function with a numeric argument, e.g., make_multiplier(5)(3).

Output format

Return the numeric result of multiplying the argument by the captured multiplier. The test harness compares the string form of the return value.

Constraints

- Implement make_multiplier as a closure (inner function capturing m). - Do not use global variables. - Support integers (and floats) as inputs.

Samples

Sample input 0

make_multiplier(1)(999)

Sample output 0

999

Explanation 0

Multiplying by 1 returns the original value.

Code editor
Loading editor…

AI assistant

Ask me anything!

Need help? I can explain the core idea behind this problem, review your current code, and give targeted hints. Use “Teach Theory” for the concept, “Get AI hint” for a quick scaffold nudge, or ask a specific question below.

Chat history is temporary and will not be saved.

09:16 AM

Free preview includes 1 Teach Theory response and 1 AI hint on each of the first 3 lessons in this module.