Higher-Order Function
- In Turkish
- Yüksek dereceli fonksiyon
In short
A higher-order function is a function that takes another function as an argument, returns a function, or both, so behavior can be passed around like data.
What is a higher-order function?
A higher-order function is any function that works with other functions as values: it accepts a function as a parameter, returns a new function, or does both. This is possible in languages where functions are first-class values, which means they can be stored in variables, passed as arguments and returned just like numbers or strings. JavaScript, Python, Kotlin, Swift, Rust and almost every other modern language support this.
The best-known examples are array helpers such as map, filter and reduce. Each one handles the looping for you and asks only for a small function describing what to do with each item: how to transform it, whether to keep it, or how to combine it. Functions that return functions are also common, such as a withLogging(fn) wrapper that returns a version of fn that logs every call, or a factory that builds customized validators.
A higher-order function is like a food processor with swappable blades: the machine provides the motor and the bowl, and the blade you attach decides whether it slices, grates or blends. This makes code shorter and more reusable, because the general mechanics are written once and only the part that varies is passed in.
Higher-order functions are often mixed up with callbacks. A callback is the function that gets passed in, while the higher-order function is the one that receives it, so in items.map(double) the map method is higher-order and double is the callback. Functions returned from a higher-order function are usually closures, because they remember variables from the function that created them.
Key takeaways
- A higher-order function takes a function as input, returns a function, or both.
- It requires first-class functions, which most modern languages support.
map,filterandreduceare the classic examples.- The function passed in is the callback; the function receiving it is higher-order.
Example
const prices = [12, 45, 7, 30];
// filter and map are higher-order: they take functions as arguments
const doubled = prices
.filter((p) => p > 10)
.map((p) => p * 2);
// A higher-order function that returns a new function
function multiplier(factor) {
return (n) => n * factor;
}
const triple = multiplier(3);
console.log(doubled, triple(5)); // [24, 90, 60] 15Readers ask
Is a callback a higher-order function?
Not usually. The callback is the function being passed in, and the function that accepts it is the higher-order one. A callback is only higher-order itself if it also takes or returns functions.
Which languages support higher-order functions?
Any language with first-class functions, including JavaScript, TypeScript, Python, Kotlin, Swift, C#, Rust, Go and all functional languages. Java added support through lambdas and functional interfaces in Java 8.
Why use higher-order functions instead of loops?
They hide repetitive looping code and let you describe what should happen to each item, which is often shorter and easier to read. Plain loops are still fine, and sometimes clearer when the logic has many steps or needs to stop early.
See also
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- CallbackProgramming Fundamentals, p. 7A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- Functional ProgrammingProgramming Fundamentals, p. 22Functional programming is a style of building software from pure functions that avoid changing shared data, making code more predictable and easier to test.
- Lambda FunctionProgramming Fundamentals, p. 33A lambda function is a small function written inline without a name, usually passed straight to another function such as a sorting or filtering helper.
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
Spotted a mistake or something missing on this page?Suggest an edit