Created
May 25, 2024 13:40
-
-
Save theSamyak/75732a3fbcc797485b8cb609e5d3797e to your computer and use it in GitHub Desktop.
[FCC First class functions and Closures Blog] A higher-order function that takes a function as an argument
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 apply_operation(operation, x, y): | |
return operation(x, y) | |
# Functions to pass as arguments | |
def add(x, y): | |
return x + y | |
def multiply(x, y): | |
return x * y | |
# Using the higher-order function | |
result_add = apply_operation(add, 3, 4) | |
result_multiply = apply_operation(multiply, 3, 4) | |
print(result_add) # Output: 7 | |
print(result_multiply) # Output: 12 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment