Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Higher-order function

Функция высшего порядка

A function is higher-order if it takes another function as an argument, returns a function, or both. For example, a filtering function can take a separate function that decides which items to keep.

The idea: pass behavior, not just data

Imagine sorting a list of tickets. The steps for putting items in order can stay the same whether you sort by priority or by creation date. A higher-order sorting function lets the caller supply a comparison function instead of building those choices into the sorting code. The sorting function controls the process; the comparison function defines the ordering.

What makes a function higher-order?

Look at its inputs and output. If any input is a function or the output is a function, it qualifies. Calling a helper by name inside the body isn't enough: a function that takes only numbers and returns a number is not higher-order just because it calls Math.round.

You'll often hear the term first-class functions in the same conversation. That describes what a language lets you do with functions, such as store them in variables, pass them as arguments, and return them. Higher-order describes a particular function that takes or returns one.

Example: pass a rule and return a new function

def keep(items, predicate):
    return [item for item in items if predicate(item)]

def longer_than(limit):
    return lambda text: len(text) > limit

is_long = longer_than(3)
keep(['cat', 'fox', 'otter'], is_long)  # ['otter']

keep is higher-order because it accepts a function. It calls that function once per item and keeps the original item if the result is truthy. longer_than is higher-order for a different reason: it returns a function. The returned function can still access limit after longer_than has finished. That's a closure. is_long is not itself higher-order: it takes a string and returns a boolean.

Where it shows up

Collection APIs often use this pattern: map takes a transformation, filter takes a test, and comparison-based sorts can take an ordering rule. Another use is wrapping an operation: a timing decorator can take a function and return a wrapper that measures how long the original function takes to run. Higher-order functions are common in functional programming, but they're not necessarily pure. A wrapper that logs timing information has a side effect.

Be careful about passing a function versus calling it. In the example, keep(items, is_long) hands over the test. With keep(items, is_long('otter')), Python evaluates the test first and passes True instead; keep then fails when it tries to call that boolean for an item. Also, a callback need not be asynchronous: the test in keep runs during the call to keep.

What interviewers look for

You might be asked to write a small map or filter, or to trace a function that returns another function. Explain who calls the supplied function, what arguments it receives, and what the caller does with its result. When tracing a closure, identify what the inner function can still access, rather than assuming every variable disappears when the outer call returns.