<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Higher-order_function</id>
	<title>Higher-order function - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sarg.dev/index.php?action=history&amp;feed=atom&amp;title=Higher-order_function"/>
	<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Higher-order_function&amp;action=history"/>
	<updated>2026-06-24T06:13:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://wiki.sarg.dev/index.php?title=Higher-order_function&amp;diff=165735&amp;oldid=prev</id>
		<title>50.101.202.93: /* General examples */</title>
		<link rel="alternate" type="text/html" href="https://wiki.sarg.dev/index.php?title=Higher-order_function&amp;diff=165735&amp;oldid=prev"/>
		<updated>2025-09-29T04:35:14Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;General examples&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Function that takes one or more functions as an input or that outputs a function}}{{More sources|date=November 2024}}{{Distinguish|Functor{{!}}Functor (category theory)}}In [[mathematics]] and [[computer science]], a &amp;#039;&amp;#039;&amp;#039;higher-order function&amp;#039;&amp;#039;&amp;#039; (&amp;#039;&amp;#039;&amp;#039;HOF&amp;#039;&amp;#039;&amp;#039;) is a [[function (mathematics)|function]] that does at least one of the following:&lt;br /&gt;
* takes one or more functions as arguments (i.e. a [[procedural parameter]], which is a [[Parameter (computer science)|parameter]] of a [[Subroutine|procedure]] that is itself a procedure),&lt;br /&gt;
* returns a function as its result.&lt;br /&gt;
All other functions are &amp;#039;&amp;#039;first-order functions&amp;#039;&amp;#039;. In mathematics higher-order functions are also termed &amp;#039;&amp;#039;[[operator (mathematics)|operators]]&amp;#039;&amp;#039; or &amp;#039;&amp;#039;[[functional (mathematics)|functionals]]&amp;#039;&amp;#039;. The [[differential operator]] in [[calculus]] is a common example, since it maps a function to its [[derivative]], also a function. Higher-order functions should not be confused with other uses of the word &amp;quot;functor&amp;quot; throughout mathematics, see [[Functor (disambiguation)]].&lt;br /&gt;
&lt;br /&gt;
In the untyped [[lambda calculus]], all functions are higher-order; in a [[typed lambda calculus]], from which most [[functional programming]] languages are derived, higher-order functions that take one function as argument are values with types of the form &amp;lt;math&amp;gt;(\tau_1\to\tau_2)\to\tau_3&amp;lt;/math&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==General examples==&lt;br /&gt;
* {{mono|[[map (higher-order function)|map]]}} function, found in many functional programming languages, is one example of a higher-order function. It takes arguments as a function &amp;#039;&amp;#039;f&amp;#039;&amp;#039; and a collection of elements, and as the result, returns a new collection with &amp;#039;&amp;#039;f&amp;#039;&amp;#039; applied to each element from the collection.&lt;br /&gt;
* {{mono|[[Sorting algorithm|sort]]}}, which take a comparison function as a parameter, allowing the programmer to separate the sorting algorithm from the comparisons of the items being sorted. The [[C (programming language)|C]] standard [[function (computer science)|function]] &amp;lt;code&amp;gt;qsort&amp;lt;/code&amp;gt; is an example of this.&lt;br /&gt;
* {{mono|[[Filter (higher-order function)|filter]]}}&lt;br /&gt;
* {{mono|[[fold (higher-order function)|fold]]}} (including {{mono|foldl}} and {{mono|foldr}})&lt;br /&gt;
* {{mono|[[Prefix sum|scan]]}}&lt;br /&gt;
* {{mono|[[apply]]}}&lt;br /&gt;
* [[Function composition (computer science)|Function composition]]&lt;br /&gt;
* [[Integral|Integration]]&lt;br /&gt;
* [[Callback (computer programming)|Callback]]&lt;br /&gt;
* [[Tree traversal]]&lt;br /&gt;
* [[Montague grammar]], a semantic theory of natural language, uses higher-order functions&lt;br /&gt;
&lt;br /&gt;
==Support in programming languages==&amp;lt;!-- see also Category:Programming language comparisons --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Direct support===&lt;br /&gt;
&amp;#039;&amp;#039;The examples are not intended to compare and contrast programming languages, but to serve as examples of higher-order function syntax&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
In the following examples, the higher-order function {{code|twice}} takes a function, and applies the function to some value twice. If {{code|twice}} has to be applied several times for the same {{code|f}} it preferably should return a function rather than a value. This is in line with the &amp;quot;[[don&amp;#039;t repeat yourself]]&amp;quot; principle.&lt;br /&gt;
&lt;br /&gt;
====APL====&lt;br /&gt;
{{further information|APL (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;apl&amp;quot;&amp;gt;&lt;br /&gt;
      twice←{⍺⍺ ⍺⍺ ⍵}&lt;br /&gt;
&lt;br /&gt;
      plusthree←{⍵+3}&lt;br /&gt;
&lt;br /&gt;
      g←{plusthree twice ⍵}&lt;br /&gt;
    &lt;br /&gt;
      g 7&lt;br /&gt;
13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or in a tacit manner:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;apl&amp;quot;&amp;gt;&lt;br /&gt;
      twice←⍣2&lt;br /&gt;
&lt;br /&gt;
      plusthree←+∘3&lt;br /&gt;
&lt;br /&gt;
      g←plusthree twice&lt;br /&gt;
    &lt;br /&gt;
      g 7&lt;br /&gt;
13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====C++====&lt;br /&gt;
{{further information|C++}}&lt;br /&gt;
&lt;br /&gt;
Using {{code|std::function}} in [[C++11]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
auto twice = [](const std::function&amp;lt;int(int)&amp;gt;&amp;amp; f) -&amp;gt; auto {&lt;br /&gt;
    return [f](int x) -&amp;gt; int {&lt;br /&gt;
        return f(f(x));&lt;br /&gt;
    };&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
auto plusThree = [](int i) -&amp;gt; int {&lt;br /&gt;
    return i + 3;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    auto g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
    std::println(&amp;quot;{}&amp;quot;, g(7)); // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with generic lambdas provided by C++14:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c++&amp;quot;&amp;gt;&lt;br /&gt;
import std;&lt;br /&gt;
&lt;br /&gt;
auto twice = [](const auto&amp;amp; f) -&amp;gt; auto {&lt;br /&gt;
    return [f](int x) -&amp;gt; int {&lt;br /&gt;
        return f(f(x));&lt;br /&gt;
    };&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
auto plusThree = [](int i) -&amp;gt; int {&lt;br /&gt;
    return i + 3;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
    auto g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
    std::println(&amp;quot;{}&amp;quot;, g(7)); // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====C#====&lt;br /&gt;
{{further information|C Sharp (programming language)}}&lt;br /&gt;
&lt;br /&gt;
Using just delegates:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    public static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        Func&amp;lt;Func&amp;lt;int, int&amp;gt;, Func&amp;lt;int, int&amp;gt;&amp;gt; twice = f =&amp;gt; x =&amp;gt; f(f(x));&lt;br /&gt;
&lt;br /&gt;
        Func&amp;lt;int, int&amp;gt; plusThree = i =&amp;gt; i + 3;&lt;br /&gt;
&lt;br /&gt;
        var g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
        Console.WriteLine(g(7)); // 13&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or equivalently, with static methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;csharp&amp;quot;&amp;gt;&lt;br /&gt;
using System;&lt;br /&gt;
&lt;br /&gt;
public class Program&lt;br /&gt;
{&lt;br /&gt;
    private static Func&amp;lt;int, int&amp;gt; Twice(Func&amp;lt;int, int&amp;gt; f)&lt;br /&gt;
    {&lt;br /&gt;
        return x =&amp;gt; f(f(x));&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    private static int PlusThree(int i) =&amp;gt; i + 3;&lt;br /&gt;
&lt;br /&gt;
    public static void Main(string[] args)&lt;br /&gt;
    {&lt;br /&gt;
        var g = Twice(PlusThree);&lt;br /&gt;
&lt;br /&gt;
        Console.WriteLine(g(7)); // 13&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Clojure====&lt;br /&gt;
{{further information|Clojure}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;clojure&amp;quot;&amp;gt;&lt;br /&gt;
(defn twice [f]&lt;br /&gt;
  (fn [x] (f (f x))))&lt;br /&gt;
&lt;br /&gt;
(defn plus-three [i]&lt;br /&gt;
  (+ i 3))&lt;br /&gt;
&lt;br /&gt;
(def g (twice plus-three))&lt;br /&gt;
&lt;br /&gt;
(println (g 7)) ; 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====ColdFusion Markup Language (CFML)====&lt;br /&gt;
{{further information|ColdFusion Markup Language}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cfs&amp;quot;&amp;gt;&lt;br /&gt;
twice = function(f) {&lt;br /&gt;
    return function(x) {&lt;br /&gt;
        return f(f(x));&lt;br /&gt;
    };&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
plusThree = function(i) {&lt;br /&gt;
    return i + 3;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
writeOutput(g(7)); // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Common Lisp====&lt;br /&gt;
{{further information|Common Lisp}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lisp&amp;quot;&amp;gt;&lt;br /&gt;
(defun twice (f)                                                                &lt;br /&gt;
  (lambda (x) (funcall f (funcall f x))))                                       &lt;br /&gt;
                                                                                &lt;br /&gt;
(defun plus-three (i)                                                           &lt;br /&gt;
  (+ i 3))                                                                      &lt;br /&gt;
                                                                                &lt;br /&gt;
(defvar g (twice #&amp;#039;plus-three))                                                 &lt;br /&gt;
                                                                                &lt;br /&gt;
(print (funcall g 7))                                                           &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====D====&lt;br /&gt;
{{further information|D (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;d&amp;quot;&amp;gt;&lt;br /&gt;
import std.stdio : writeln;&lt;br /&gt;
&lt;br /&gt;
alias twice = (f) =&amp;gt; (int x) =&amp;gt; f(f(x));&lt;br /&gt;
&lt;br /&gt;
alias plusThree = (int i) =&amp;gt; i + 3;&lt;br /&gt;
&lt;br /&gt;
void main()&lt;br /&gt;
{&lt;br /&gt;
    auto g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
    writeln(g(7)); // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Dart====&lt;br /&gt;
{{further information|Dart (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;dart&amp;quot;&amp;gt;&lt;br /&gt;
int Function(int) twice(int Function(int) f) {&lt;br /&gt;
    return (x) {&lt;br /&gt;
        return f(f(x));&lt;br /&gt;
    };&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int plusThree(int i) {&lt;br /&gt;
    return i + 3;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void main() {&lt;br /&gt;
    final g = twice(plusThree);&lt;br /&gt;
    &lt;br /&gt;
    print(g(7)); // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Elixir====&lt;br /&gt;
{{further information|Elixir (programming language)}}&lt;br /&gt;
&lt;br /&gt;
In Elixir, you can mix module definitions and [[anonymous function]]s&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;elixir&amp;quot;&amp;gt;&lt;br /&gt;
defmodule Hof do&lt;br /&gt;
    def twice(f) do&lt;br /&gt;
        fn(x) -&amp;gt; f.(f.(x)) end&lt;br /&gt;
    end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plus_three = fn(i) -&amp;gt; i + 3 end&lt;br /&gt;
&lt;br /&gt;
g = Hof.twice(plus_three)&lt;br /&gt;
&lt;br /&gt;
IO.puts g.(7) # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Alternatively, we can also compose using pure anonymous functions.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;elixir&amp;quot;&amp;gt;&lt;br /&gt;
twice = fn(f) -&amp;gt;&lt;br /&gt;
    fn(x) -&amp;gt; f.(f.(x)) end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plus_three = fn(i) -&amp;gt; i + 3 end&lt;br /&gt;
&lt;br /&gt;
g = twice.(plus_three)&lt;br /&gt;
&lt;br /&gt;
IO.puts g.(7) # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Erlang====&lt;br /&gt;
{{further information|Erlang (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;erlang&amp;quot;&amp;gt;&lt;br /&gt;
or_else([], _) -&amp;gt; false;&lt;br /&gt;
or_else([F | Fs], X) -&amp;gt; or_else(Fs, X, F(X)).&lt;br /&gt;
&lt;br /&gt;
or_else(Fs, X, false) -&amp;gt; or_else(Fs, X);&lt;br /&gt;
or_else(Fs, _, {false, Y}) -&amp;gt; or_else(Fs, Y);&lt;br /&gt;
or_else(_, _, R) -&amp;gt; R.&lt;br /&gt;
&lt;br /&gt;
or_else([fun erlang:is_integer/1, fun erlang:is_atom/1, fun erlang:is_list/1], 3.23).&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this Erlang example, the higher-order function {{code|or_else/2}} takes a list of functions ({{code|Fs}}) and argument ({{code|X}}). It evaluates the function {{code|F}} with the argument {{code|X}} as argument. If the function {{code|F}} returns false then the next function in {{code|Fs}} will be evaluated. If the function {{code|F}} returns {{code|{false, Y} }} then the next function in {{code|Fs}} with argument {{code|Y}} will be evaluated. If the function {{code|F}} returns {{code|R}} the higher-order function {{code|or_else/2}} will return {{code|R}}. Note that {{code|X}}, {{code|Y}}, and {{code|R}} can be functions. The example returns {{code|false}}.&lt;br /&gt;
&lt;br /&gt;
====F#====&lt;br /&gt;
{{further information|F Sharp (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;fsharp&amp;quot;&amp;gt;&lt;br /&gt;
let twice f = f &amp;gt;&amp;gt; f&lt;br /&gt;
&lt;br /&gt;
let plus_three = (+) 3&lt;br /&gt;
&lt;br /&gt;
let g = twice plus_three&lt;br /&gt;
&lt;br /&gt;
g 7 |&amp;gt; printf &amp;quot;%A&amp;quot; // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Go====&lt;br /&gt;
{{further information|Go (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;go&amp;quot;&amp;gt;&lt;br /&gt;
package main&lt;br /&gt;
&lt;br /&gt;
import &amp;quot;fmt&amp;quot;&lt;br /&gt;
&lt;br /&gt;
func twice(f func(int) int) func(int) int {&lt;br /&gt;
	return func(x int) int {&lt;br /&gt;
		return f(f(x))&lt;br /&gt;
	}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
func main() {&lt;br /&gt;
	plusThree := func(i int) int {&lt;br /&gt;
		return i + 3&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	g := twice(plusThree)&lt;br /&gt;
&lt;br /&gt;
	fmt.Println(g(7)) // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice a function literal can be defined either with an identifier ({{code|twice}}) or anonymously (assigned to variable {{code|plusThree}}).&lt;br /&gt;
&lt;br /&gt;
====Groovy====&lt;br /&gt;
{{further information|Groovy (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;groovy&amp;quot;&amp;gt;def twice = { f, x -&amp;gt; f(f(x)) }&lt;br /&gt;
def plusThree = { it + 3 }&lt;br /&gt;
def g = twice.curry(plusThree) &lt;br /&gt;
println g(7) // 13&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Haskell====&lt;br /&gt;
{{further information|Haskell}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;br /&gt;
twice :: (Int -&amp;gt; Int) -&amp;gt; (Int -&amp;gt; Int)&lt;br /&gt;
twice f = f . f&lt;br /&gt;
&lt;br /&gt;
plusThree :: Int -&amp;gt; Int&lt;br /&gt;
plusThree = (+3)&lt;br /&gt;
&lt;br /&gt;
main :: IO ()&lt;br /&gt;
main = print (g 7) -- 13&lt;br /&gt;
  where&lt;br /&gt;
    g = twice plusThree&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====J====&lt;br /&gt;
{{further information|J (programming language)}}&lt;br /&gt;
&lt;br /&gt;
Explicitly,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;J&amp;quot;&amp;gt;&lt;br /&gt;
   twice=.     adverb : &amp;#039;u u y&amp;#039;&lt;br /&gt;
&lt;br /&gt;
   plusthree=. verb   : &amp;#039;y + 3&amp;#039;&lt;br /&gt;
   &lt;br /&gt;
   g=. plusthree twice&lt;br /&gt;
   &lt;br /&gt;
   g 7&lt;br /&gt;
13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or tacitly,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;J&amp;quot;&amp;gt;&lt;br /&gt;
   twice=. ^:2&lt;br /&gt;
&lt;br /&gt;
   plusthree=. +&amp;amp;3&lt;br /&gt;
   &lt;br /&gt;
   g=. plusthree twice&lt;br /&gt;
   &lt;br /&gt;
   g 7&lt;br /&gt;
13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Java (1.8+)====&lt;br /&gt;
{{further information|Java (programming language)|Java version history}}&lt;br /&gt;
&lt;br /&gt;
Using just functional interfaces:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.function.*;&lt;br /&gt;
&lt;br /&gt;
class Main {&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        Function&amp;lt;IntUnaryOperator, IntUnaryOperator&amp;gt; twice = f -&amp;gt; f.andThen(f);&lt;br /&gt;
&lt;br /&gt;
        IntUnaryOperator plusThree = i -&amp;gt; i + 3;&lt;br /&gt;
&lt;br /&gt;
        var g = twice.apply(plusThree);&lt;br /&gt;
&lt;br /&gt;
        System.out.println(g.applyAsInt(7)); // 13&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or equivalently, with static methods:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.util.function.*;&lt;br /&gt;
&lt;br /&gt;
class Main {&lt;br /&gt;
    private static IntUnaryOperator twice(IntUnaryOperator f) {&lt;br /&gt;
        return f.andThen(f);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    private static int plusThree(int i) {&lt;br /&gt;
        return i + 3;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        var g = twice(Main::plusThree);&lt;br /&gt;
&lt;br /&gt;
        System.out.println(g.applyAsInt(7)); // 13&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====JavaScript====&lt;br /&gt;
{{further information|JavaScript}}&lt;br /&gt;
&lt;br /&gt;
With arrow functions:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;use strict&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
const twice = f =&amp;gt; x =&amp;gt; f(f(x));&lt;br /&gt;
&lt;br /&gt;
const plusThree = i =&amp;gt; i + 3;&lt;br /&gt;
&lt;br /&gt;
const g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
console.log(g(7)); // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or with classical syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
&amp;quot;use strict&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
function twice(f) {&lt;br /&gt;
  return function (x) {&lt;br /&gt;
    return f(f(x));&lt;br /&gt;
  };&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function plusThree(i) {&lt;br /&gt;
  return i + 3;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const g = twice(plusThree);&lt;br /&gt;
&lt;br /&gt;
console.log(g(7)); // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Julia====&lt;br /&gt;
{{further information|Julia (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;jlcon&amp;quot;&amp;gt;&lt;br /&gt;
julia&amp;gt; function twice(f)&lt;br /&gt;
           function result(x)&lt;br /&gt;
               return f(f(x))&lt;br /&gt;
           end&lt;br /&gt;
           return result&lt;br /&gt;
       end&lt;br /&gt;
twice (generic function with 1 method)&lt;br /&gt;
&lt;br /&gt;
julia&amp;gt; plusthree(i) = i + 3&lt;br /&gt;
plusthree (generic function with 1 method)&lt;br /&gt;
&lt;br /&gt;
julia&amp;gt; g = twice(plusthree)&lt;br /&gt;
(::var&amp;quot;#result#3&amp;quot;{typeof(plusthree)}) (generic function with 1 method)&lt;br /&gt;
&lt;br /&gt;
julia&amp;gt; g(7)&lt;br /&gt;
13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Kotlin====&lt;br /&gt;
{{further information|Kotlin (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;kotlin&amp;quot;&amp;gt;&lt;br /&gt;
fun twice(f: (Int) -&amp;gt; Int): (Int) -&amp;gt; Int {&lt;br /&gt;
    return { f(f(it)) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fun plusThree(i: Int) = i + 3&lt;br /&gt;
&lt;br /&gt;
fun main() {&lt;br /&gt;
    val g = twice(::plusThree)&lt;br /&gt;
&lt;br /&gt;
    println(g(7)) // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== Lua ====&lt;br /&gt;
{{further information|Lua (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;lua&amp;quot;&amp;gt;&lt;br /&gt;
function twice(f)&lt;br /&gt;
  return function (x)&lt;br /&gt;
    return f(f(x))&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function plusThree(i)&lt;br /&gt;
  return i + 3&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
local g = twice(plusThree)&lt;br /&gt;
&lt;br /&gt;
print(g(7)) -- 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== MATLAB ====&lt;br /&gt;
{{further information|MATLAB}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;matlab&amp;quot;&amp;gt;&lt;br /&gt;
function result = twice(f)&lt;br /&gt;
result = @(x) f(f(x));&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plusthree = @(i) i + 3;&lt;br /&gt;
&lt;br /&gt;
g = twice(plusthree)&lt;br /&gt;
&lt;br /&gt;
disp(g(7)); % 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== OCaml ====&lt;br /&gt;
{{further information|OCaml}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ocaml&amp;quot; start=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
let twice f x =&lt;br /&gt;
  f (f x)&lt;br /&gt;
&lt;br /&gt;
let plus_three =&lt;br /&gt;
  (+) 3&lt;br /&gt;
&lt;br /&gt;
let () =&lt;br /&gt;
  let g = twice plus_three in&lt;br /&gt;
&lt;br /&gt;
  print_int (g 7); (* 13 *)&lt;br /&gt;
  print_newline ()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====PHP====&lt;br /&gt;
{{further information|PHP}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
declare(strict_types=1);&lt;br /&gt;
&lt;br /&gt;
function twice(callable $f): Closure {&lt;br /&gt;
    return function (int $x) use ($f): int {&lt;br /&gt;
        return $f($f($x));&lt;br /&gt;
    };&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
function plusThree(int $i): int {&lt;br /&gt;
    return $i + 3;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
$g = twice(&amp;#039;plusThree&amp;#039;);&lt;br /&gt;
&lt;br /&gt;
echo $g(7), &amp;quot;\n&amp;quot;; // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or with all functions in variables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
&lt;br /&gt;
declare(strict_types=1);&lt;br /&gt;
&lt;br /&gt;
$twice = fn(callable $f): Closure =&amp;gt; fn(int $x): int =&amp;gt; $f($f($x));&lt;br /&gt;
&lt;br /&gt;
$plusThree = fn(int $i): int =&amp;gt; $i + 3;&lt;br /&gt;
&lt;br /&gt;
$g = $twice($plusThree);&lt;br /&gt;
&lt;br /&gt;
echo $g(7), &amp;quot;\n&amp;quot;; // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note that arrow functions implicitly capture any variables that come from the parent scope,&amp;lt;ref&amp;gt;{{Cite web|title=PHP: Arrow Functions - Manual|url=https://www.php.net/manual/en/functions.arrow.php|access-date=2021-03-01|website=www.php.net}}&amp;lt;/ref&amp;gt; whereas anonymous functions require the {{code|use}} keyword to do the same.&lt;br /&gt;
&lt;br /&gt;
====Perl====&lt;br /&gt;
{{further information|Perl}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
use strict;&lt;br /&gt;
use warnings;&lt;br /&gt;
&lt;br /&gt;
sub twice {&lt;br /&gt;
    my ($f) = @_;&lt;br /&gt;
    sub {&lt;br /&gt;
        $f-&amp;gt;($f-&amp;gt;(@_));&lt;br /&gt;
    };&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
sub plusThree {&lt;br /&gt;
    my ($i) = @_;&lt;br /&gt;
    $i + 3;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
my $g = twice(\&amp;amp;plusThree);&lt;br /&gt;
&lt;br /&gt;
print $g-&amp;gt;(7), &amp;quot;\n&amp;quot;; # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
or with all functions in variables:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;perl&amp;quot;&amp;gt;&lt;br /&gt;
use strict;&lt;br /&gt;
use warnings;&lt;br /&gt;
&lt;br /&gt;
my $twice = sub {&lt;br /&gt;
    my ($f) = @_;&lt;br /&gt;
    sub {&lt;br /&gt;
        $f-&amp;gt;($f-&amp;gt;(@_));&lt;br /&gt;
    };&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
my $plusThree = sub {&lt;br /&gt;
    my ($i) = @_;&lt;br /&gt;
    $i + 3;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
my $g = $twice-&amp;gt;($plusThree);&lt;br /&gt;
&lt;br /&gt;
print $g-&amp;gt;(7), &amp;quot;\n&amp;quot;; # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Python====&lt;br /&gt;
{{further information|Python (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
def twice(f: Callable[Any]) -&amp;gt; Any:&lt;br /&gt;
    def result(x: Any) -&amp;gt; Any:&lt;br /&gt;
        return f(f(x))&lt;br /&gt;
    return result&lt;br /&gt;
&lt;br /&gt;
plus_three: Callable[int] = lambda i: i + 3&lt;br /&gt;
&lt;br /&gt;
g: int = twice(plus_three)&lt;br /&gt;
    &lt;br /&gt;
print(g(7))&lt;br /&gt;
# prints 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Python decorator syntax is often used to replace a function with the result of passing that function through a higher-order function. E.g., the function {{code|g}} could be implemented equivalently:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
@twice&lt;br /&gt;
def g(i: int) -&amp;gt; int:&lt;br /&gt;
    return i + 3&lt;br /&gt;
&lt;br /&gt;
print(g(7))&lt;br /&gt;
# prints 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====R====&lt;br /&gt;
{{further information|R (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;R&amp;quot;&amp;gt;&lt;br /&gt;
twice &amp;lt;- \(f) \(x) f(f(x))&lt;br /&gt;
&lt;br /&gt;
plusThree &amp;lt;- function(i) i + 3&lt;br /&gt;
&lt;br /&gt;
g &amp;lt;- twice(plusThree)&lt;br /&gt;
&lt;br /&gt;
&amp;gt; g(7)&lt;br /&gt;
[1] 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Raku====&lt;br /&gt;
{{further information|Raku (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;raku&amp;quot;&amp;gt;&lt;br /&gt;
sub twice(Callable:D $f) {&lt;br /&gt;
    return sub { $f($f($^x)) };&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
sub plusThree(Int:D $i) {&lt;br /&gt;
    return $i + 3;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
my $g = twice(&amp;amp;plusThree);&lt;br /&gt;
&lt;br /&gt;
say $g(7); # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In Raku, all code objects are closures and therefore can reference inner &amp;quot;lexical&amp;quot; variables from an outer scope because the lexical variable is &amp;quot;closed&amp;quot; inside of the function. Raku also supports &amp;quot;pointy block&amp;quot; syntax for lambda expressions which can be assigned to a variable or invoked anonymously.&lt;br /&gt;
&lt;br /&gt;
====Ruby====&lt;br /&gt;
{{further information|Ruby (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;ruby&amp;quot;&amp;gt;&lt;br /&gt;
def twice(f)&lt;br /&gt;
  -&amp;gt;(x) { f.call(f.call(x)) }&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
plus_three = -&amp;gt;(i) { i + 3 }&lt;br /&gt;
&lt;br /&gt;
g = twice(plus_three)&lt;br /&gt;
&lt;br /&gt;
puts g.call(7) # 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Rust====&lt;br /&gt;
{{further information|Rust (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;rust&amp;quot;&amp;gt;&lt;br /&gt;
fn twice(f: impl Fn(i32) -&amp;gt; i32) -&amp;gt; impl Fn(i32) -&amp;gt; i32 {&lt;br /&gt;
    move |x| f(f(x))&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fn plus_three(i: i32) -&amp;gt; i32 {&lt;br /&gt;
    i + 3&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
fn main() {&lt;br /&gt;
    let g = twice(plus_three);&lt;br /&gt;
&lt;br /&gt;
    println!(&amp;quot;{}&amp;quot;, g(7)) // 13&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Scala====&lt;br /&gt;
{{further information|Scala (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;scala&amp;quot;&amp;gt;&lt;br /&gt;
object Main {&lt;br /&gt;
  def twice(f: Int =&amp;gt; Int): Int =&amp;gt; Int =&lt;br /&gt;
    f compose f&lt;br /&gt;
&lt;br /&gt;
  def plusThree(i: Int): Int =&lt;br /&gt;
    i + 3&lt;br /&gt;
&lt;br /&gt;
  def main(args: Array[String]): Unit = {&lt;br /&gt;
    val g = twice(plusThree)&lt;br /&gt;
&lt;br /&gt;
    print(g(7)) // 13&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Scheme====&lt;br /&gt;
{{further information|Scheme (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;scheme&amp;quot;&amp;gt;&lt;br /&gt;
(define (compose f g) &lt;br /&gt;
  (lambda (x) (f (g x))))&lt;br /&gt;
&lt;br /&gt;
(define (twice f) &lt;br /&gt;
  (compose f f))&lt;br /&gt;
&lt;br /&gt;
(define (plus-three i)&lt;br /&gt;
  (+ i 3))&lt;br /&gt;
&lt;br /&gt;
(define g (twice plus-three))&lt;br /&gt;
&lt;br /&gt;
(display (g 7)) ; 13&lt;br /&gt;
(display &amp;quot;\n&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Swift====&lt;br /&gt;
{{further information|Swift (programming language)}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;swift&amp;quot;&amp;gt;&lt;br /&gt;
func twice(_ f: @escaping (Int) -&amp;gt; Int) -&amp;gt; (Int) -&amp;gt; Int {&lt;br /&gt;
    return { f(f($0)) }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
let plusThree = { $0 + 3 }&lt;br /&gt;
&lt;br /&gt;
let g = twice(plusThree)&lt;br /&gt;
&lt;br /&gt;
print(g(7)) // 13&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Tcl====&lt;br /&gt;
{{further information|Tcl}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;tcl&amp;quot;&amp;gt;&lt;br /&gt;
set twice {{f x} {apply $f [apply $f $x]}}&lt;br /&gt;
set plusThree {{i} {return [expr $i + 3]}}&lt;br /&gt;
&lt;br /&gt;
# result: 13&lt;br /&gt;
puts [apply $twice $plusThree 7]&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Tcl uses apply command to apply an anonymous function (since 8.6).&lt;br /&gt;
&lt;br /&gt;
====XACML====&lt;br /&gt;
{{further information|XACML}}&lt;br /&gt;
&lt;br /&gt;
The XACML standard defines higher-order functions in the standard to apply a function to multiple values of attribute bags.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xquery&amp;quot;&amp;gt;&lt;br /&gt;
rule allowEntry{&lt;br /&gt;
    permit&lt;br /&gt;
    condition anyOfAny(function[stringEqual], citizenships, allowedCitizenships)&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The list of higher-order functions in XACML can be found [[XACML#Higher order functions|here]].&lt;br /&gt;
&lt;br /&gt;
====XQuery====&lt;br /&gt;
{{further information|XQuery}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;xquery&amp;quot;&amp;gt;&lt;br /&gt;
declare function local:twice($f, $x) {&lt;br /&gt;
  $f($f($x))&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
declare function local:plusthree($i) {&lt;br /&gt;
  $i + 3&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
local:twice(local:plusthree#1, 7) (: 13 :)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Alternatives ===&lt;br /&gt;
====Function pointers====&lt;br /&gt;
[[Function pointer]]s in languages such as [[C (programming language)|C]], [[C++]], [[Fortran]], and [[Pascal (programming language)|Pascal]] allow programmers to pass around references to functions. The following C code computes an approximation of the integral of an arbitrary function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double square(double x)&lt;br /&gt;
{&lt;br /&gt;
    return x * x;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
double cube(double x)&lt;br /&gt;
{&lt;br /&gt;
    return x * x * x;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/* Compute the integral of f() within the interval [a,b] */&lt;br /&gt;
double integral(double f(double x), double a, double b, int n)&lt;br /&gt;
{&lt;br /&gt;
    int i;&lt;br /&gt;
    double sum = 0;&lt;br /&gt;
    double dt = (b - a) / n;&lt;br /&gt;
    for (i = 0;  i &amp;lt; n;  ++i) {&lt;br /&gt;
        sum += f(a + (i + 0.5) * dt);&lt;br /&gt;
    }&lt;br /&gt;
    return sum * dt;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    printf(&amp;quot;%g\n&amp;quot;, integral(square, 0, 1, 100));&lt;br /&gt;
    printf(&amp;quot;%g\n&amp;quot;, integral(cube, 0, 1, 100));&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The [[qsort]] function from the C standard library uses a function pointer to emulate the behavior of a higher-order function.&lt;br /&gt;
&lt;br /&gt;
====Macros====&lt;br /&gt;
[[Macro (computer science)|Macros]] can also be used to achieve some of the effects of higher-order functions. However, macros cannot easily avoid the problem of variable capture; they may also result in large amounts of duplicated code, which can be more difficult for a compiler to optimize. Macros are generally not strongly typed, although they may produce strongly typed code.&lt;br /&gt;
&lt;br /&gt;
====Dynamic code evaluation====&lt;br /&gt;
In other [[imperative programming]] languages, it is possible to achieve some of the same algorithmic results as are obtained via higher-order functions by dynamically executing code (sometimes called &amp;#039;&amp;#039;Eval&amp;#039;&amp;#039; or &amp;#039;&amp;#039;Execute&amp;#039;&amp;#039; operations) in the scope of evaluation. There can be significant drawbacks to this approach:&lt;br /&gt;
*The argument code to be executed is usually not [[type system#Static typing|statically typed]]; these languages generally rely on [[type system#Dynamic typing|dynamic typing]] to determine the well-formedness and safety of the code to be executed.&lt;br /&gt;
*The argument is usually provided as a string, the value of which may not be known until run-time. This string must either be compiled during program execution (using [[just-in-time compilation]]) or evaluated by [[interpreter (computing)|interpretation]], causing some added overhead at run-time, and usually generating less efficient code.&lt;br /&gt;
&lt;br /&gt;
====Objects====&lt;br /&gt;
In [[object-oriented programming]] languages that do not support higher-order functions, [[object (computer science)|objects]] can be an effective substitute. An object&amp;#039;s [[method (computer science)|methods]] act in essence like functions, and a method may accept objects as parameters and produce objects as return values. Objects often carry added run-time overhead compared to pure functions, however, and added [[boilerplate code]] for defining and instantiating an object and its method(s). Languages that permit [[stack-based memory allocation|stack]]-based (versus [[dynamic memory allocation|heap]]-based) objects or [[Record (computer science)|structs]] can provide more flexibility with this method.&lt;br /&gt;
&lt;br /&gt;
An example of using a simple stack based record in [[Free Pascal]] with a function that returns a function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pascal&amp;quot;&amp;gt;&lt;br /&gt;
program example;&lt;br /&gt;
&lt;br /&gt;
type &lt;br /&gt;
  int = integer;&lt;br /&gt;
  Txy = record x, y: int; end;&lt;br /&gt;
  Tf = function (xy: Txy): int;&lt;br /&gt;
     &lt;br /&gt;
function f(xy: Txy): int; &lt;br /&gt;
begin &lt;br /&gt;
  Result := xy.y + xy.x; &lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
function g(func: Tf): Tf; &lt;br /&gt;
begin &lt;br /&gt;
  result := func; &lt;br /&gt;
end;&lt;br /&gt;
&lt;br /&gt;
var &lt;br /&gt;
  a: Tf;&lt;br /&gt;
  xy: Txy = (x: 3; y: 7);&lt;br /&gt;
&lt;br /&gt;
begin  &lt;br /&gt;
  a := g(@f);     // return a function to &amp;quot;a&amp;quot;&lt;br /&gt;
  writeln(a(xy)); // prints 10&lt;br /&gt;
end.&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function &amp;lt;code&amp;gt;a()&amp;lt;/code&amp;gt; takes a &amp;lt;code&amp;gt;Txy&amp;lt;/code&amp;gt; record as input and returns the integer value of the sum of the record&amp;#039;s &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;y&amp;lt;/code&amp;gt; fields (3 + 7).&lt;br /&gt;
&lt;br /&gt;
====Defunctionalization====&lt;br /&gt;
[[Defunctionalization]] can be used to implement higher-order functions in languages that lack  [[first class function|first-class functions]]:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;cpp&amp;quot;&amp;gt;&lt;br /&gt;
// Defunctionalized function data structures&lt;br /&gt;
template&amp;lt;typename T&amp;gt; struct Add { T value; };&lt;br /&gt;
template&amp;lt;typename T&amp;gt; struct DivBy { T value; };&lt;br /&gt;
template&amp;lt;typename F, typename G&amp;gt; struct Composition { F f; G g; };&lt;br /&gt;
&lt;br /&gt;
// Defunctionalized function application implementations&lt;br /&gt;
template&amp;lt;typename F, typename G, typename X&amp;gt;&lt;br /&gt;
auto apply(Composition&amp;lt;F, G&amp;gt; f, X arg) {&lt;br /&gt;
    return apply(f.f, apply(f.g, arg));&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
template&amp;lt;typename T, typename X&amp;gt;&lt;br /&gt;
auto apply(Add&amp;lt;T&amp;gt; f, X arg) {&lt;br /&gt;
    return arg  + f.value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
template&amp;lt;typename T, typename X&amp;gt;&lt;br /&gt;
auto apply(DivBy&amp;lt;T&amp;gt; f, X arg) {&lt;br /&gt;
    return arg / f.value;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Higher-order compose function&lt;br /&gt;
template&amp;lt;typename F, typename G&amp;gt;&lt;br /&gt;
Composition&amp;lt;F, G&amp;gt; compose(F f, G g) {&lt;br /&gt;
    return Composition&amp;lt;F, G&amp;gt; {f, g};&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(int argc, const char* argv[]) {&lt;br /&gt;
    auto f = compose(DivBy&amp;lt;float&amp;gt;{ 2.0f }, Add&amp;lt;int&amp;gt;{ 5 });&lt;br /&gt;
    apply(f, 3); // 4.0f&lt;br /&gt;
    apply(f, 9); // 7.0f&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, different types are used to trigger different functions via [[function overloading]]. The overloaded function in this example has the signature &amp;lt;code&amp;gt;auto apply&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==See also==&lt;br /&gt;
*[[First-class function]]&lt;br /&gt;
*[[Combinatory logic]]&lt;br /&gt;
*[[Function-level programming]]&lt;br /&gt;
*[[Functional programming]]&lt;br /&gt;
*[[Kappa calculus]] - a formalism for functions which &amp;#039;&amp;#039;excludes&amp;#039;&amp;#039; higher-order functions&lt;br /&gt;
*[[Strategy pattern]]&lt;br /&gt;
*[[Higher order message]]s&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
{{Reflist}}&lt;br /&gt;
{{Functions navbox}}&lt;br /&gt;
&lt;br /&gt;
[[Category:Functional programming]]&lt;br /&gt;
[[Category:Lambda calculus]]&lt;br /&gt;
[[Category:Higher-order functions| ]]&lt;br /&gt;
[[Category:Subroutines]]&lt;br /&gt;
[[Category:Articles with example C code]]&lt;br /&gt;
[[Category:Articles with example C++ code]]&lt;br /&gt;
[[Category:Articles with example D code]]&lt;br /&gt;
[[Category:Articles with example Haskell code]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Articles with example JavaScript code]]&lt;br /&gt;
[[Category:Articles with example Julia code]]&lt;br /&gt;
[[Category:Articles with example Lisp (programming language) code]]&lt;br /&gt;
[[Category:Articles with example MATLAB/Octave code]]&lt;br /&gt;
[[Category:Articles with example Pascal code]]&lt;br /&gt;
[[Category:Articles with example Perl code]]&lt;br /&gt;
[[Category:Articles with example PHP code]]&lt;br /&gt;
[[Category:Articles with example Python (programming language) code]]&lt;br /&gt;
[[Category:Articles with example R code]]&lt;br /&gt;
[[Category:Articles with example Scala code]]&lt;br /&gt;
[[Category:Articles with example Scheme (programming language) code]]&lt;br /&gt;
[[Category:Articles with example Tcl code]]&lt;br /&gt;
[[Category:Articles with example Swift code]]&lt;/div&gt;</summary>
		<author><name>50.101.202.93</name></author>
	</entry>
</feed>