Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Capture a Default Value with a Closure

Build a function factory that captures a default value and returns an adder using that default.

Python practice8 minClosures & Function FactoriesBeginnerLast updated April 13, 2026

Problem statement

Implement make_adder(default) which returns a function adder(x=None). The returned adder behaves as follows: - If called with no argument (or x is None), it returns the captured default value. - If called with a numeric argument x, it returns default + x. The default value should be captured by the closure so different adders keep different defaults.

Task

Use closures to capture and reuse a provided default value in a returned function.

Examples

Using the captured default

Input

(make_adder(5))()

Output

5

Calling the created adder without arguments returns the captured default (5).

Adding to the default

Input

(make_adder(5))(3)

Output

8

Calling the adder with 3 returns default + 3 = 8.

Input format

Your function make_adder(default) will be called; the returned function may be invoked with or without an argument in tests.

Output format

Return a value (the default or default + x) as described.

Constraints

Use a closure to capture the default value. The returned adder should handle None (or no argument) by returning the captured default.

Samples

Sample 1

Input

(lambda a,b: (a(), b(2)))(make_adder(1), make_adder(10))

Output

(1, 12)

Two different adders capture different defaults and behave independently.