#221 · Functions · Beginner · 30 XP · about 4 min
A function can build another function and return it. The inner function remembers the variables of the function that made it, even after that function has finished. This is called a closure.
Write make_multiplier(n) that returns a new function. The new function takes one argument x and returns x * n.
double = make_multiplier(2) triple = make_multiplier(3) double(21) # returns 42 triple(5) # returns 15 make_multiplier(3)('ab') # returns 'ababab'
Each new function keeps its own n: making triple does not change double.
First solves only, ranked by stars and then time.