Skip to content

Instantly share code, notes, and snippets.

@theSamyak
Created May 25, 2024 13:40
Show Gist options
  • Save theSamyak/75732a3fbcc797485b8cb609e5d3797e to your computer and use it in GitHub Desktop.
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
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