Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create a function factory returning customized functions

Build a function factory that returns specialized functions capturing parameters from their creation scope.

Python practice15 minFunctions & ScopeIntermediateLast updated March 17, 2026

Problem statement

Write a function make_power(exponent) that returns a new function. The returned function should accept a single numeric argument x and return x ** exponent. This demonstrates how a factory function captures and reuses configuration (exponent) via closures.

Task

Implement make_power(exponent) that returns a function which raises its input to the given exponent.

Examples

Square and cube factories

Input

make_power(2)(3)

Output

9

make_power(2) returns a function that squares its input. Calling it with 3 returns 9.

Zero exponent

Input

make_power(0)(5)

Output

1

Any non-zero number to the power 0 is 1.

Input format

Call make_power with a numeric exponent and immediately call the returned function with a numeric value, e.g., make_power(3)(2).

Output format

Return the numeric result of raising the input to the stored exponent (as Python would display it).

Constraints

- exponent may be any integer (positive, zero, negative). - The returned function should work with integers and floats. - Use closures to capture the exponent value.

Samples

Sample 1

Input

make_power(-1)(2)

Output

0.5

A negative exponent inverts the base (2 ** -1 == 0.5).