First-Class Function vs Higher-Order Function
With first-class functions, a function is a value you can store and pass. A higher-order function is one that takes in or hands back a function.
First-Class Function
A function treated as a value. You can store it, pass it or return it.
A language has first-class functions when a function is treated like any other value. You can store one in a variable. You can pass one into another function, and a function can hand one back. JavaScript, Python and Kotlin all work this way. It is what makes callbacks and higher-order functions possible.
Higher-Order Function
A function that takes another function as input, or gives one back. map is one.
A higher-order function takes another function as input, or gives one back. map is the best-known one: you hand it a function and a list, and it runs that function on every item. sorted with a key, and filter, work the same way. They let you reuse the looping part and swap only the small action. Most modern languages have them built in.
In one line
- First-Class Function
- A function treated as a value. You can store it, pass it or return it.
- Higher-Order Function
- A function that takes another function as input, or gives one back. map is one.