Skip to content

Instantly share code, notes, and snippets.

@theSamyak
Last active May 25, 2024 19:04
Show Gist options
  • Save theSamyak/35f27f6fbed12e50c4aeeca14a10abed to your computer and use it in GitHub Desktop.
Save theSamyak/35f27f6fbed12e50c4aeeca14a10abed to your computer and use it in GitHub Desktop.
[FCC First class functions and Closures Blog] A higher-order function that returns another function
def create_multiplier(n):
def multiplier(x):
return x * n
return multiplier
# Creating specific multiplier functions
multiply_by_2 = create_multiplier(2)
multiply_by_3 = create_multiplier(3)
# Using the returned functions
result1 = multiply_by_2(5) # Output: 10 (5 * 2)
result2 = multiply_by_3(4) # Output: 12 (4 * 3)
print(result1)
print(result2)

FIle name 2

# Using the returned functions
result1 = multiply_by_2(5) # Output: 10 (5 * 2)
result2 = multiply_by_3(4) # Output: 12 (4 * 3)
print(result1)
print(result2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment