Last active
May 25, 2024 19:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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