Higher-order functions

From Rosetta Code
Task
Higher-order functions
You are encouraged to solve this task according to the task description, using any language you may know.
Task

Pass a function as an argument to another function.


C.f.



Contents

8th[edit]

 
: pass-me
"I was passed\n" . ;
: passer
w:exec ;
\ pass 'pass-me' to 'passer'
' pass-me passer
 
Output:

I was passed

ActionScript[edit]

package {
public class MyClass {
 
public function first(func:Function):String {
return func.call();
}
 
public function second():String {
return "second";
}
 
public static function main():void {
var result:String = first(second);
trace(result);
result = first(function() { return "third"; });
trace(result);
}
}
}

Ada[edit]

Simple Example[edit]

with Ada.Text_Io; use Ada.Text_Io;
 
procedure Subprogram_As_Argument is
type Proc_Access is access procedure;
 
procedure Second is
begin
Put_Line("Second Procedure");
end Second;
 
procedure First(Proc : Proc_Access) is
begin
Proc.all;
end First;
begin
First(Second'Access);
end Subprogram_As_Argument;

Complex Example[edit]

with Ada.Text_Io; use Ada.Text_Io;
 
procedure Subprogram_As_Argument_2 is
 
-- Definition of an access to long_float
 
type Lf_Access is access Long_Float;
 
-- Definition of a function returning Lf_Access taking an
-- integer as a parameter
 
function Func_To_Be_Passed(Item : Integer) return Lf_Access is
Result : Lf_Access := new Long_Float;
begin
Result.All := 3.14159 * Long_Float(Item);
return Result;
end Func_To_Be_Passed;
 
-- Definition of an access to function type matching the function
-- signature above
 
type Func_Access is access function(Item : Integer) return Lf_Access;
 
-- Definition of an integer access type
 
type Int_Access is access Integer;
 
-- Define a function taking an instance of Func_Access as its
-- parameter and returning an integer access type
 
function Complex_Func(Item : Func_Access; Parm2 : Integer) return Int_Access is
Result : Int_Access := new Integer;
begin
Result.All := Integer(Item(Parm2).all / 3.14149);
return Result;
end Complex_Func;
 
-- Declare an access variable to hold the access to the function
 
F_Ptr : Func_Access := Func_To_Be_Passed'access;
 
-- Declare an access to integer variable to hold the result
 
Int_Ptr : Int_Access;
 
begin
 
-- Call the function using the access variable
 
Int_Ptr := Complex_Func(F_Ptr, 3);
Put_Line(Integer'Image(Int_Ptr.All));
end Subprogram_As_Argument_2;

Aime[edit]

integer
average(integer p, integer q)
{
return (p + q) / 2;
}
 
 
void
out(integer p, integer q, integer (*f) (integer, integer))
{
o_integer(f(p, q));
o_byte('\n');
}
 
 
integer
main(void)
{
# display the minimum, the maximum and the average of 117 and 319
out(117, 319, min);
out(117, 319, max);
out(117, 319, average);
 
return 0;
}

ALGOL 68[edit]

Works with: ALGOL 68 version Revision 1 - no extensions to language used
Works with: ALGOL 68G version Any - tested with release 1.18.0-9h.tiny
PROC first = (PROC(LONG REAL)LONG REAL f) LONG REAL:
(
f(1) + 2
);
 
PROC second = (LONG REAL x)LONG REAL:
(
x/2
);
 
main: (
printf(($xg(5,2)l$,first(second)))
)

Output:

+2.50

AmigaE[edit]

The {} takes the pointer to an object (code/functions, variables and so on); Amiga E does not distinguish nor check anything, so it is up to the programmer to use the pointer properly. For this reason, a warning is always raised when a variable (func, holding a pointer to a real function in our case) is used like a function.

PROC compute(func, val)
DEF s[10] : STRING
WriteF('\s\n', RealF(s,func(val),4))
ENDPROC
 
PROC sin_wrap(val) IS Fsin(val)
PROC cos_wrap(val) IS Fcos(val)
 
PROC main()
compute({sin_wrap}, 0.0)
compute({cos_wrap}, 3.1415)
ENDPROC

AppleScript[edit]

-- This handler takes a script object (singer)
-- with another handler (call).
on sing about topic by singer
call of singer for "Of " & topic & " I sing"
end sing
 
-- Define a handler in a script object,
-- then pass the script object.
script cellos
on call for what
say what using "Cellos"
end call
end script
sing about "functional programming" by cellos
 
-- Pass a different handler. This one is a closure
-- that uses a variable (voice) from its context.
on hire for voice
script
on call for what
say what using voice
end call
end script
end hire
sing about "closures" by (hire for "Pipe Organ")

As we can see above, AppleScript functions (referred to as 'handlers' in Apple's documentation) are not, in themselves, first class objects. They can only be applied within other functions, when passed as arguments, if wrapped in Script objects. If we abstract out this lifting of functions into objects by writing an mReturn or mInject function, we can then use it to write some higher-order functions which directly accept unadorned AppleScript handlers as arguments.

We could, for example, write map, fold/reduce and filter functions for AppleScript as follows:

-- Returns a new list consisting of the results of applying the 
-- provided function to each element of the first list
-- [a] -> (a -> b) -> [b]
on map(xs, f)
set mf to mReturn(f)
 
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set end of lst to mf's call(item i of xs, i, xs)
end repeat
return lst
end map
 
-- Applies a function against an accumulator and
-- each list element (from left-to-right) to reduce it
-- to a single return value
-- Arguments: list, function, initial value of accumulator
-- [a] -> (b -> b) -> b
on reduce(xs, f, a)
set mf to mReturn(f)
 
set v to a
repeat with i from 1 to length of xs
set v to mf's call(v, item i of xs, i, xs)
end repeat
return v
end reduce
 
-- Sublist of those elements for which the predicate
-- function returns true
-- [a] -> (a -> Bool) -> [a]
on filtered(xs, f)
set mf to mReturn(f)
 
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if mf's call(v, i, xs) then
set end of lst to v
end if
end repeat
return lst
end filtered
 
-- An ordinary AppleScript handler function
-- lifted into a script which is a first-class object
on mReturn(f)
script
property call : f
end script
end mReturn
 
-- HANDLER FUNCTIONS TO BE PASSED AS ARGUMENTS
on squared(x)
return x * x
end squared
 
on summed(a, b)
return a + b
end summed
 
on isEven(x)
return x mod 2 = 0
end isEven
 
-- PASSING FUNCTIONS AS ARGUMENTS TO
-- MAP REDUCE AND FILTER ACROSS A LIST
set lstRange to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
 
map(lstRange, squared)
--> {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
 
reduce(map(lstRange, squared), summed, 0)
--> 385
 
filtered(lstRange, isEven)
--> {0, 2, 4, 6, 8, 10}

ATS[edit]

 
#include
"share/atspre_staload.hats"
 
fun app_to_0 (f: (int) -> int): int = f (0)
 
implement
main0 () =
{
//
val () = assertloc (app_to_0(lam(x) => x+1) = 1)
val () = assertloc (app_to_0(lam(x) => 10*(x+1)) = 10)
//
} (* end of [main0] *)
 

AutoHotkey[edit]

 
f(x) {
return x
}
g(x, y) {
msgbox %x%
msgbox %y%
}
g(f("RC Function as an Argument AHK implementation"), "Non-function argument")
return
 

BBC BASIC[edit]

      REM Test passing a function to a function:
PRINT FNtwo(FNone(), 10, 11)
END
 
REM Function to be passed:
DEF FNone(x, y) = (x + y) ^ 2
 
REM Function taking a function as an argument:
DEF FNtwo(RETURN f%, x, y) = FN(^f%)(x, y)

Output:

       441

Bracmat[edit]

( (plus=a b.!arg:(?a.?b)&!a+!b)
& ( print
= text a b func
.  !arg:(?a.?b.(=?func).?text)
& out$(str$(!text "(" !a "," !b ")=" func$(!a.!b)))
)
& print$(3.7.'$plus.add)
& print
$ ( 3
. 7
. (=a b.!arg:(?a.?b)&!a*!b)
. multiply
)
);

Output:

add(3,7)=10
multiply(3,7)=21

Brat[edit]

add = { a, b | a + b }
 
doit = { f, a, b | f a, b }
 
p doit ->add 1 2 #prints 3

Burlesque[edit]

Burlesque doesn't have functions in the usual sense. One can think of blocks in Burlesque as anonymous functions. The function "m[" (map) takes a block (a 'function') as it's argument. Add 5 to every element in a list (like map (+5) [1,2,3,4] in haskell):

 
blsq ) {1 2 3 4}{5.+}m[
{6 7 8 9}
 


C[edit]

Simple example

The pointer to the function to be passed as an argument is the only involved pointer.

Definition of a function whose only parameter is a pointer to a function with no parameters and no return value:

void myFuncSimple( void (*funcParameter)(void) )
{
/* ... */
 
(*funcParameter)(); /* Call the passed function. */
funcParameter(); /* Same as above with slight different syntax. */
 
/* ... */
}

Note that you can't call the passed function by " *funcParameter() ", since that would mean "call funcParameter and then apply the * operator on the returned value".

Call:

void funcToBePassed(void);
 
/* ... */
 
myFuncSimple(&funcToBePassed);

Complex example

Definition of a function whose return value is a pointer to int and whose only parameter is a pointer to a function, whose (in turn) return value is a pointer to double and whose only parameter is a pointer to long.

int* myFuncComplex( double* (*funcParameter)(long* parameter) )
{
long inLong;
double* outDouble;
long *inLong2 = &inLong;
 
/* ... */
 
outDouble = (*funcParameter)(&inLong); /* Call the passed function and store returned pointer. */
outDouble = funcParameter(inLong2); /* Same as above with slight different syntax. */
 
/* ... */
}

Call:

double* funcToBePassed(long* parameter);
 
/* ... */
 
int* outInt;
 
outInt = myFuncComplex(&funcToBePassed);

Pointer

Finally, declaration of a pointer variable of the proper type to hold such a function as myFunc:

int* (*funcPointer)( double* (*funcParameter)(long* parameter) );
 
/* ... */
 
funcPointer = &myFuncComplex;

Of course, in a real project you shouldn't write such a convoluted code, but use some typedef instead, in order to break complexity into steps.

C++[edit]

Function Pointer[edit]

Works with: g++ version 3.4.2 (mingw-special)

C++ can pass function pointers in the same manner as C.

Function class template[edit]

Using the std::tr1::function class template allows more powerful usage. function<> can be used to pass around arbitrary function objects. This permits them to be used as closures.

For C++11 this is now std::function.

Works with: gcc version 4.4
 
// Use <functional> for C++11
#include <tr1/functional>
#include <iostream>
 
using namespace std;
using namespace std::tr1;
 
void first(function<void()> f)
{
f();
}
 
void second()
{
cout << "second\n";
}
 
int main()
{
first(second);
}
 

Template and Inheritance[edit]

Works with: Visual C++ version 2005
#include <iostream>
#include <functional>
 
template<class Func>
typename Func::result_type first(Func func, typename Func::argument_type arg)
{
return func(arg);
}
 
class second : public std::unary_function<int, int>
{
public:
result_type operator()(argument_type arg) const
{
return arg * arg;
}
};
 
int main()
{
std::cout << first(second(), 2) << std::endl;
return 0;
}

C#[edit]

Each example below does the same thing and has the same output:

f=Add, f(6, 2) = 8
f=Mul, f(6, 2) = 12
f=Div, f(6, 2) = 3

Delegates: Named methods[edit]

This works for all standard versions of C#.

using System;
delegate int Func2(int a, int b);
class Program
{
static int Add(int a, int b)
{
return a + b;
}
 
static int Mul(int a, int b)
{
return a * b;
}
 
static int Div(int a, int b)
{
return a / b;
}
 
static int Call(Func2 f, int a, int b)
{
return f(a, b);
}
 
static void Main()
{
int a = 6;
int b = 2;
 
Func2 add = new Func2(Add);
Func2 mul = new Func2(Mul);
Func2 div = new Func2(Div);
 
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call(add, a, b));
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call(mul, a, b));
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call(div, a, b));
}
}

Delegates: Anonymous functions[edit]

Anonymous functions added closures to C#.

Works with: C# version 2+
using System;
delegate int Func2(int a, int b);
class Program
{
static int Call(Func2 f, int a, int b)
{
return f(a, b);
}
 
static void Main()
{
int a = 6;
int b = 2;
 
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call(delegate(int x, int y) { return x + y; }, a, b));
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call(delegate(int x, int y) { return x * y; }, a, b));
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call(delegate(int x, int y) { return x / y; }, a, b));
}
}

Lambdas[edit]

Lambda functions are syntactic sugar for anonymous functions. The System namespace also gained some common delegates, such as Func<T0, T1, T2>, which refers to a function that returns a value of type T2 and has two parameters of types T0 and T1.

Works with: C# version 3+
using System;
class Program
{
static int Call(Func<int, int, int> f, int a, int b)
{
return f(a, b);
}
 
static void Main()
{
int a = 6;
int b = 2;
 
Console.WriteLine("f=Add, f({0}, {1}) = {2}", a, b, Call((x, y) => x + y, a, b));
Console.WriteLine("f=Mul, f({0}, {1}) = {2}", a, b, Call((x, y) => x * y, a, b));
Console.WriteLine("f=Div, f({0}, {1}) = {2}", a, b, Call((x, y) => x / y, a, b));
}
}

Clean[edit]

Take a function as an argument and apply it to all elements in a list:

map f [x:xs] = [f x:map f xs]
map f [] = []

Pass a function as an argument:

incr x = x + 1
 
Start = map incr [1..10]

Do the same using a anonymous function:

Start = map (\x -> x + 1) [1..10]

Do the same using currying:

Start = map ((+) 1) [1..10]

Clojure[edit]

 
(defn append-hello [s]
(str "Hello " s))
 
(defn modify-string [f s]
(f s))
 
(println (modify-string append-hello "World!"))
 

CoffeeScript[edit]

Passing an anonymous function to built-in map/reduce functions:

double = [1,2,3].map (x) -> x*2

Using a function stored in a variable:

fn = -> return 8
sum = (a, b) -> a() + b()
sum(fn, fn) # => 16
 

List comprehension with a function argument:

bowl = ["Cheese", "Tomato"]
 
smash = (ingredient) ->
return "Smashed #{ingredient}"
 
contents = smash ingredient for ingredient in bowl
# => ["Smashed Cheese", "Smashed Tomato"]
 

Nested function passing:

double = (x) -> x*2
triple = (x) -> x*3
addOne = (x) -> x+1
 
addOne triple double 2 # same as addOne(triple(double(2)))

A function that returns a function that returns a function that returns a function that returns 2, immediately executed:

(-> -> -> -> 2 )()()()() # => 2

A function that takes a function that takes a function argument:

((x)->
2 + x(-> 5)
)((y) -> y()+3)
# result: 10

Common Lisp[edit]

In Common Lisp, functions are first class objects, so you can pass function objects as arguments to other functions:

CL-USER> (defun add (a b) (+ a b))
ADD
CL-USER> (add 1 2)
3
CL-USER> (defun call-it (fn x y)
(funcall fn x y))
CALL-IT
CL-USER> (call-it #'add 1 2)
3

The Common Lisp library makes extensive use of higher-order functions:

CL-USER> (funcall #'+ 1 2 3)
6
CL-USER> (apply #'+ (list 1 2 3))
6
CL-USER> (sort (string-downcase "Common Lisp will bend your mind!") #'string<)
"     !bcddeiiilllmmmnnnoooprsuwy"
CL-USER> (reduce #'/ '(1 2 3 4 5))
1/120
CL-USER> (mapcar #'(lambda (n) (expt 2 n)) '(0 1 2 3 4 5))
(1 2 4 8 16 32)
CL-USER> 

D[edit]

int hof(int a, int b, int delegate(int, int) f) {
return f(a, b);
}
 
void main() {
import std.stdio;
writeln("Add: ", hof(2, 3, (a, b) => a + b));
writeln("Multiply: ", hof(2, 3, (a, b) => a * b));
}
Output:
Add: 5
Multiply: 6

This longer and more systematic example shows D functions/delegates by passing each type of function/delegate to _test_ as argument.

import std.stdio;
 
// Test the function argument.
string test(U)(string scopes, U func) {
string typeStr = typeid(typeof(func)).toString();
 
string isFunc = (typeStr[$ - 1] == '*') ? "function" : "delegate";
writefln("Hi, %-13s : scope: %-8s (%s) : %s",
func(), scopes, isFunc, typeStr );
return scopes;
}
 
// Normal module level function.
string aFunction() { return "Function"; }
 
// Implicit-Function-Template-Instantiation (IFTI) Function.
T tmpFunc(T)() { return "IFTI.function"; }
 
// Member in a template.
template tmpGroup(T) {
T t0(){ return "Tmp.member.0"; }
T t1(){ return "Tmp.member.1"; }
T t2(){ return "Tmp.member.2"; }
}
 
// Used for implementing member function at class & struct.
template Impl() {
static string aStatic() { return "Static Method"; }
string aMethod() { return "Method"; }
}
 
class C { mixin Impl!(); }
struct S { mixin Impl!(); }
 
void main() {
// Nested function.
string aNested() {
return "Nested";
}
 
// Bind to a variable.
auto variableF = function string() { return "variable.F"; };
auto variableD = delegate string() { return "variable.D"; };
 
C c = new C;
S s;
 
"Global".test(&aFunction);
"Nested".test(&aNested);
"Class".test(&C.aStatic)
.test(&c.aMethod);
"Struct".test(&S.aStatic)
.test(&s.aMethod);
"Template".test(&tmpFunc!(string))
.test(&tmpGroup!(string).t2);
"Binding".test(variableF)
.test(variableD);
// Literal function/delegate.
"Literal".test(function string() { return "literal.F"; })
.test(delegate string() { return "literal.D"; });
}
Output:
}
Hi, Function      : scope: Global   (function) : immutable(char)[]()*
Hi, Nested        : scope: Nested   (delegate) : immutable(char)[] delegate()
Hi, Static Method : scope: Class    (function) : immutable(char)[]()*
Hi, Method        : scope: Class    (delegate) : immutable(char)[] delegate()
Hi, Static Method : scope: Struct   (function) : immutable(char)[]()*
Hi, Method        : scope: Struct   (delegate) : immutable(char)[] delegate()
Hi, IFTI.function : scope: Template (function) : immutable(char)[]()*
Hi, Tmp.member.2  : scope: Template (function) : immutable(char)[]()*
Hi, variable.F    : scope: Binding  (function) : immutable(char)[]()*
Hi, variable.D    : scope: Binding  (delegate) : immutable(char)[] delegate()
Hi, literal.F     : scope: Literal  (function) : immutable(char)[]()*
Hi, literal.D     : scope: Literal  (delegate) : immutable(char)[] delegate()

Delphi[edit]

See Pascal

Déjà Vu[edit]

map f lst:
]
for item in lst:
f item
[
 
twice:
* 2
 
!. map @twice [ 1 2 5 ]
Output:
[ 2 4 10 ]

DWScript[edit]

type TFnType = function(x : Float) : Float;
 
function First(f : TFnType) : Float;
begin
Result := f(1) + 2;
end;
 
function Second(f : Float) : Float;
begin
Result := f/2;
end;
 
PrintLn(First(Second));

E[edit]

def map(f, list) {
var out := []
for x in list {
out with= f(x)
}
return out
}
 
? map(fn x { x + x }, [1, "two"])
# value: [2, "twotwo"]
 
? map(1.add, [5, 10, 20])
# value: [6, 11, 21]
 
? def foo(x) { return -(x.size()) }
> map(foo, ["", "a", "bc"])
# value: [0, -1, -2]


ECL[edit]

//a Function prototype:
INTEGER actionPrototype(INTEGER v1, INTEGER v2) := 0;
 
INTEGER aveValues(INTEGER v1, INTEGER v2) := (v1 + v2) DIV 2;
INTEGER addValues(INTEGER v1, INTEGER v2) := v1 + v2;
INTEGER multiValues(INTEGER v1, INTEGER v2) := v1 * v2;
 
//a Function prototype using a function prototype:
INTEGER applyPrototype(INTEGER v1, actionPrototype actionFunc) := 0;
 
//using the Function prototype and a default value:
INTEGER applyValue2(INTEGER v1,
actionPrototype actionFunc = aveValues) :=
actionFunc(v1, v1+1)*2;
 
//Defining the Function parameter inline, witha default value:
INTEGER applyValue4(INTEGER v1,
INTEGER actionFunc(INTEGER v1,INTEGER v2) = aveValues)
 := actionFunc(v1, v1+1)*4;
INTEGER doApplyValue(INTEGER v1,
INTEGER actionFunc(INTEGER v1, INTEGER v2))
 := applyValue2(v1+1, actionFunc);
 
//producing simple results:
OUTPUT(applyValue2(1)); // 2
OUTPUT(applyValue2(2)); // 4
OUTPUT(applyValue2(1, addValues)); // 6
OUTPUT(applyValue2(2, addValues)); // 10
OUTPUT(applyValue2(1, multiValues)); // 4
OUTPUT(applyValue2(2, multiValues)); // 12
OUTPUT(doApplyValue(1, multiValues)); // 12
OUTPUT(doApplyValue(2, multiValues)); // 24
 
 
 
//A definition taking function parameters which themselves
//have parameters that are functions...
 
STRING doMany(INTEGER v1,
INTEGER firstAction(INTEGER v1,
INTEGER actionFunc(INTEGER v1,INTEGER v2)),
INTEGER secondAction(INTEGER v1,
INTEGER actionFunc(INTEGER v1,INTEGER v2)),
INTEGER actionFunc(INTEGER v1,INTEGER v2))
 := (STRING)firstAction(v1, actionFunc) + ':' + (STRING)secondaction(v1, actionFunc);
 
OUTPUT(doMany(1, applyValue2, applyValue4, addValues));
// produces "6:12"
 
OUTPUT(doMany(2, applyValue4, applyValue2,multiValues));
// produces "24:12"

Efene[edit]

first = fn (F) {
F()
}
 
second = fn () {
io.format("hello~n")
}
 
@public
run = fn () {
# passing the function specifying the name and arity
# arity: the number of arguments it accepts
first(fn second:0)
 
first(fn () { io.format("hello~n") })
 
# holding a reference to the function in a variable
F1 = fn second:0
F2 = fn () { io.format("hello~n") }
 
first(F1)
first(F2)
}
 

Elixir[edit]

iex(1)> defmodule RC do
...(1)> def first(f), do: f.()
...(1)> def second, do: :hello
...(1)> end
{:module, RC,
<<70, 79, 82, 49, 0, 0, 4, 224, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 142,
131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95
, 118, 49, 108, 0, 0, 0, 2, 104, 2, ...>>,
{:second, 0}}
iex(2)> RC.first(fn -> RC.second end)
:hello
iex(3)> RC.first(&RC.second/0) # Another expression
:hello
iex(4)> f = fn -> :world end # Anonymous function
#Function<20.54118792/0 in :erl_eval.expr/5>
iex(5)> RC.first(f)
:world

Erlang[edit]

Erlang functions are atoms, and they're considered different functions if their arity (the number of arguments they take) is different. As such, an Erlang function must be passed as fun Function/Arity, but can be used as any other variable:

-module(test).
-export([first/1, second/0]).
 
first(F) -> F().
second() -> hello.

Testing it:

1> c(tests).
{ok, tests}
2> tests:first(fun tests:second/0).
hello
3> tests:first(fun() -> anonymous_function end).
anonymous_function

ERRE[edit]

ERRE function are limited to one-line FUNCTION, but you can write:

 
PROGRAM FUNC_PASS
 
FUNCTION ONE(X,Y)
ONE=(X+Y)^2
END FUNCTION
 
FUNCTION TWO(X,Y)
TWO=ONE(X,Y)+1
END FUNCTION
 
BEGIN
PRINT(TWO(10,11))
END PROGRAM
 

Answer is 442

Euler Math Toolbox[edit]

 
>function f(x,a) := x^a-a^x
>function dof (f$:string,x) := f$(x,args());
>dof("f",1:5;2)
[ -1 0 1 0 -7 ]
>plot2d("f",1,5;2):
 

Euphoria[edit]

procedure use(integer fi, integer a, integer b)
print(1,call_func(fi,{a,b}))
end procedure
 
function add(integer a, integer b)
return a + b
end function
 
use(routine_id("add"),23,45)

Factor[edit]

Using words (factor's functions) :

USING: io ;
IN: rosetacode
: argument-function1 ( -- ) "Hello World!" print ;
: argument-function2 ( -- ) "Goodbye World!" print ;
 
! normal words have to know the stack effect of the input parameters they execute
: calling-function1 ( another-function -- ) execute( -- ) ;
 
! unlike normal words, inline words do not have to know the stack effect.
: calling-function2 ( another-function -- ) execute ; inline
 
! Stack effect has to be written for runtime computed values :
: calling-function3 ( bool -- ) \ argument-function1 \ argument-function2 ? execute( -- ) ;
 
   ( scratchpad )
   \ argument-function1 calling-function1
   \ argument-function1 calling-function2
   t calling-function3
   f calling-function3
   Hello World!
   Hello World!
   Hello World!
   Goodbye World!

FALSE[edit]

Anonymous code blocks are the basis of FALSE control flow and function definition. These blocks may be passed on the stack as with any other parameter.

[f:[$0>][@@\f;!\1-]#%]r:   { reduce n stack items using the given basis and binary function }
 
1 2 3 4 0 4[+]r;!." " { 10 }
1 2 3 4 1 4[*]r;!." " { 24 }
1 2 3 4 0 4[$*+]r;!. { 30 }

Fantom[edit]

 
class Main
{
// apply given function to two arguments
static Int performOp (Int arg1, Int arg2, |Int, Int -> Int| fn)
{
fn (arg1, arg2)
}
 
public static Void main ()
{
echo (performOp (2, 5, |Int a, Int b -> Int| { a + b }))
echo (performOp (2, 5, |Int a, Int b -> Int| { a * b }))
}
}
 

Forth[edit]

Forth words can be referenced on the stack via their execution token or XT. An XT is obtained from a word via the quote operator, and invoked via EXECUTE. Anonymous functions may be defined via :NONAME (returning an XT) instead of a standard colon definition.

: square  dup * ;
: cube dup dup * * ;
: map. ( xt addr len -- )
0 do 2dup i cells + @ swap execute . loop 2drop ;
 
create array 1 , 2 , 3 , 4 , 5 ,
' square array 5 map. cr \ 1 4 9 16 25
' cube array 5 map. cr \ 1 8 27 64 125
:noname 2* 1+ ; array 5 map. cr \ 3 5 7 9 11

Fortran[edit]

Works with: Fortran version 90 and later

use the EXTERNAL attribute to show the dummy argument is another function rather than a data object. i.e.

FUNCTION FUNC3(FUNC1, FUNC2, x, y)
REAL, EXTERNAL :: FUNC1, FUNC2
REAL :: FUNC3
REAL :: x, y
 
FUNC3 = FUNC1(x) * FUNC2(y)
END FUNCTION FUNC3

Another way is to put the functions you want to pass in a module:

module FuncContainer
implicit none
contains
 
function func1(x)
real :: func1
real, intent(in) :: x
 
func1 = x**2.0
end function func1
 
function func2(x)
real :: func2
real, intent(in) :: x
 
func2 = x**2.05
end function func2
 
end module FuncContainer
 
program FuncArg
use FuncContainer
implicit none
 
print *, "Func1"
call asubroutine(func1)
 
print *, "Func2"
call asubroutine(func2)
 
contains
 
subroutine asubroutine(f)
! the following interface is redundant: can be omitted
interface
function f(x)
real, intent(in) :: x
real :: f
end function f
end interface
real :: px
 
px = 0.0
do while( px < 10.0 )
print *, px, f(px)
px = px + 1.0
end do
end subroutine asubroutine
 
end program FuncArg

Frink[edit]

The following defines an anonymous function and passes it to another function. In this case, the anonymous function is a comparison function that sorts by string length.

 
cmpFunc = {|a,b| length[a] <=> length[b]}
 
a = ["tree", "apple", "bee", "monkey", "z"]
sort[a, cmpFunc]
 

You can also look up functions by name and number of arguments. The following is equivalent to the previous example.

 
lengthCompare[a,b] := length[a] <=> length[b]
 
func = getFunction["lengthCompare", 2]
a = ["tree", "apple", "bee", "monkey", "z"]
sort[a, func]
 

F#[edit]

We define a function that takes another function f as an argument and applies that function twice to the argument x:

> let twice f x = f (f x);;
 
val twice : ('a -> 'a) -> 'a -> 'a
 
> twice System.Math.Sqrt 81.0;;
val it : float = 3.0

Another example, using an operator as a function:

> List.map2 (+) [1;2;3] [3;2;1];;
val it : int list = [4; 4; 4]

GAP[edit]

Eval := function(f, x)
return f(x);
end;
 
Eval(x -> x^3, 7);
# 343

Go[edit]

package main
import "fmt"
 
func func1(f func(string) string) string { return f("a string") }
func func2(s string) string { return "func2 called with " + s }
func main() { fmt.Println(func1(func2)) }

Groovy[edit]

As closures:

first = { func -> func() }
second = { println "second" }
 
first(second)

As functions:

def first(func) { func() }
def second() { println "second" }
 
first(this.&second)

Haskell[edit]

Works with: GHCi version 6.6

A function is just a value that wants arguments:

func1 f = f "a string"
func2 s = "func2 called with " ++ s
 
main = putStrLn $ func1 func2

Or, with an anonymous function:

func f = f 1 2 
 
main = print $ func (\x y -> x+y)
-- output: 3

Note that func (\x y -> x+y) is equivalent to func (+). (Operators are functions too.)

Icon and Unicon[edit]

 procedure main()
local lst
lst := [10, 20, 30, 40]
myfun(callback, lst)
end
 
procedure myfun(fun, lst)
every fun(!lst)
end
 
procedure callback(arg)
write("->", arg)
end

Inform 6[edit]

As in C, functions in Inform 6 are not first-class, but pointers to functions can be used.

[ func;
print "Hello^";
];
 
[ call_func x;
x();
];
 
[ Main;
call_func(func);
];

Inform 7[edit]

Phrases usually aren't defined with names, only with invocation syntax. A phrase must be given a name (here, "addition" and "multiplication") in order to be passed as a phrase value.

Higher Order Functions is a room.
 
To decide which number is (N - number) added to (M - number) (this is addition):
decide on N + M.
 
To decide which number is multiply (N - number) by (M - number) (this is multiplication):
decide on N * M.
 
To demonstrate (P - phrase (number, number) -> number) as (title - text):
say "[title]: [P applied to 12 and 34]."
 
When play begins:
demonstrate addition as "Add";
demonstrate multiplication as "Mul";
end the story.

J[edit]

Adverbs take a single verb or noun argument and conjunctions take two. For example, / (insert) \ (prefix) and \. (suffix) are adverbs and @ (atop), & (bond or compose) and ^: (power) are conjunctions. The following expressions illustrate their workings.

   + / 3 1 4 1 5 9   NB. sum
23
>./ 3 1 4 1 5 9 NB. max
9
*./ 3 1 4 1 5 9 NB. lcm
180
 
+/\ 3 1 4 1 5 9 NB. sum prefix (partial sums)
3 4 8 9 14 23
 
+/\. 3 1 4 1 5 9 NB. sum suffix
23 20 19 15 14 9
 
2&% 1 2 3 NB. divide 2 by
2 1 0.666667
 
 %&2 (1 2 3) NB. divide by 2 (need parenthesis to break up list formation)
0.5 1 1.5
-: 1 2 3 NB. but divide by 2 happens a lot so it's a primitive
0.5 1 1.5
 
f=: -:@(+ 2&%) NB. one Newton iteration
f 1
1.5
f f 1
1.41667
 
f^:(i.5) 1 NB. first 5 Newton iterations
1 1.5 1.41667 1.41422 1.41421
f^:(i.5) 1x NB. rational approximations to sqrt 2
1 3r2 17r12 577r408 665857r470832

Adverbs and conjunctions may also be user defined

   + conjunction def 'u' -
+
+ conjunction def 'v' -
-
* adverb def '10 u y' 11
110
^ conjunction def '10 v 2 u y' * 11
20480

Java[edit]

There is no real callback in Java like in C or C++, but we can do the same as swing does for executing an event. We need to create an interface that has the method we want to call or create one that will call the method we want to call. The following example uses the second way.

public class NewClass {
 
public NewClass() {
first(new AnEventOrCallback() {
public void call() {
second();
}
});
}
 
public void first(AnEventOrCallback obj) {
obj.call();
}
 
public void second() {
System.out.println("Second");
}
 
public static void main(String[] args) {
new NewClass();
}
}
 
interface AnEventOrCallback {
public void call();
}

From Java 8, lambda expressions may be used. Example (from Oracle):

public class ListenerTest {
public static void main(String[] args) {
JButton testButton = new JButton("Test Button");
testButton.addActionListener(new ActionListener(){
@Override public void actionPerformed(ActionEvent ae){
System.out.println("Click Detected by Anon Class");
}
});
 
testButton.addActionListener(e -> System.out.println("Click Detected by Lambda Listner"));
 
// Swing stuff
JFrame frame = new JFrame("Listener Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(testButton, BorderLayout.CENTER);
frame.pack();
frame.setVisible(true);
}
}

JavaScript[edit]

function first (func) {
return func();
}
 
function second () {
return "second";
}
 
var result = first(second);
result = first(function () { return "third"; });

An example with anonymous functions and uses in the core library

Works with: Firefox version 1.5
for methods filter and map.
>>> var array = [2, 4, 5, 13, 18, 24, 34, 97];
>>> array
[2, 4, 5, 13, 18, 24, 34, 97]
 
// return all elements less than 10
>>> array.filter(function (x) { return x < 10 });
[2, 4, 5]
 
// return all elements less than 30
>>> array.filter(function (x) { return x < 30 });
[2, 4, 5, 13, 18, 24]
 
// return all elements less than 100
>>> array.filter(function (x) { return x < 100 });
[2, 4, 5, 13, 18, 24, 34, 97]
 
// multiply each element by 2 and return the new array
>>> array.map(function (x) { return x * 2 });
[4, 8, 10, 26, 36, 48, 68, 194]
 
// sort the array from smallest to largest
>>> array.sort(function (a, b) { return a > b });
[2, 4, 5, 13, 18, 24, 34, 97]
 
// sort the array from largest to smallest
>>> array.sort(function (a, b) { return a < b });
[97, 34, 24, 18, 13, 5, 4, 2]

Joy[edit]

This example is taken from V. Define first as multiplying two numbers on the stack.

DEFINE first == *.

There will be a warning about overwriting builtin first. Define second as interpreting the passed quotation on the stack.

DEFINE second == i.

Pass first enclosed in quotes to second which applies it on the stack.

2 3 [first] second.

The program prints 6.

jq[edit]

The examples given in this section closely follow the exposition in the Julia section of this page.

To understand these examples, it is helpful to remember that:

  • jq functions are filters that can participate in a left-to-right pipeline, just as in most modern command shells;
  • "." on the right of a pipe ("|") refers to the output from the filter on the left.

Example 1: "hello blue world"[edit]

def foo( filter ):
("world" | filter) as $str
| "hello \($str)" ;
 
# blue is defined here as a filter that adds blue to its input:
def blue: "blue \(.)";
 
foo( blue ) # prints "hello blue world"
 

Example 2: g(add; 2; 3)[edit]

 
def g(f; x; y): [x,y] | f;
 
g(add; 2; 3) # => 5

Example: Built-in higher-order functions[edit]

In the following sequence of interactions, we pass the function *is_even/0* to some built-in higher order functions. *is_even/0* is defined as follows:

def is_even:
if floor == . then (. % 2) == 0
else error("is_even expects its input to be an integer")
end;
 
# Are all integers between 1 and 5 even?
# For this example, we will use all/2 even
# though it requires a release of jq after jq 1.4;
# we do so to highlight the fact that all/2
# terminates the generator once the condition is satisfied:
all( range(1;6); is_even )
false
 
# Display the even integers in the given range:
range(1;6) | select(is_even)
2
4
 
# Evaluate is_even for each integer in an array
[range(1;6)] | map(is_even)
[false, true, false, true, false]
 
# Note that in jq, there is actually no need to call
# a higher-order function in cases like this.
# For example one can simply write:
range(1;6) | is_even
false
true
false
true
false

Julia[edit]

 
function foo(x)
str = x("world")
println("hello $(str)!")
end
foo(y -> "blue $y") # prints "hello blue world"
 

The above code snippet defines a named function, foo, which takes a single argument, which is a Function. foo calls this function on the string literal "world", and then interpolates the result into the "hello ___!" string literal, and prints it. In the final line, foo is called with an anonymous function that takes a string, and returns a that string with "blue " preppended to it.

 
function g(x,y,z)
x(y,z)
end
println(g(+,2,3)) # prints 5
 

This code snippet defines a named function g that takes three arguments: x is a function to call, and y and z are the values to call x on. We then call g on the + function. Operators in Julia are just special names for functions.

In the following interactive session, we pass the function iseven to a few higher order functions. The function iseven returns true if its argument is an even integer, false if it is an odd integer, and throws an error otherwise. The second argument to the functions is a range of integers, specifically the five integers between 1 and 5 included.

julia> all(iseven, 1:5)              # not all integers between 1 and 5 are even.
false
 
julia> findfirst(iseven, 1:5) # the first even integer is at index 2 in the range.
2
 
julia> count(iseven, 1:5) # there are two even integers between 1 and 5.
2
 
julia> filter(iseven, 1:5) # here are the even integers in the given range.
2-element Array{Int64,1}:
2
4
 
julia> map(iseven, 1:5) # we apply our function to all integers in range.
5-element Array{Bool,1}:
false
true
false
true
false
 

Kotlin[edit]

Kotlin is a functional language. Example showing how the builtin map function is used to get the average value of a transformed list of numbers:

fun main(args: Array<String>) {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
var a = list.map({ x -> x + 2 }).average()
var h = list.map({ x -> x * x }).average()
var g = list.map({ x -> x * x * x }).average()
println("A = %f G = %f H = %f".format(a, g, h))
}

[edit]

You can pass the quoted symbol for the function and invoke it with RUN.

to printstuff
print "stuff
end
to runstuff :proc
run :proc
end
runstuff "printstuff  ; stuff
runstuff [print [also stuff]]  ; also stuff

Lily[edit]

A simple function that can take another function can be defined like this:

define f(x: integer, y: integer)
{
return x + y
}
 
define simpleHigherOrder(x: integer, y: integer, f: function(integer, integer => integer) => integer)
{
return f(x, y)
}
 
# First, using another function.
simpleHigherOrder(1, 2, f) # Result: 3
 
# Again, but using a lambda this time.
simpleHigherOrder(1, 2, {|a, b| a + b}) # Result: 3

Here's something more interesting, using generics:

define genericHigherOrder[A](x: A, y: A, f: function(A, A => A) => A)
{
return f(x, y)
}
 
# x is of type 'A', so A is inferred to be type integer within the lambda.
genericHigherOrder(1.5, 2.5, {|a, b| a + b}) # Result: 4.0
 
# The string class contains a method called concat, which joins two strings.
# Since A = string in every case, this is also allowed.
genericHigherOrder("1", "2", string::concat) # Result: "12"

Lua[edit]

Lua functions are first-class:

a = function() return 1 end
b = function(r) print( r() ) end
b(a)

Luck[edit]

Higher-order functions can be used to implement conditional expressions:

function lambda_true(x: 'a)(y: 'a): 'a = x;;
function lambda_false(x: 'a)(y: 'a): 'a = y;;
function lambda_if(c:'a -> 'a -> 'a )(t: 'a)(f: 'a): 'a = c(t)(f);;
 
print( lambda_if(lambda_true)("condition was true")("condition was false") );;

Mathematica / Wolfram Language[edit]

Passing 3 arguments and a value (could be a number, variable, graphic or a function as well, actually it could be anything), and composing them in an unusual way:

PassFunc[f_, g_, h_, x_] := f[g[x]*h[x]]
PassFunc[Tan, Cos, Sin, x]
% /. x -> 0.12
PassFunc[Tan, Cos, Sin, 0.12]

gives back:

Tan[Cos[x] Sin[x]]
0.119414
0.119414

MATLAB / Octave[edit]

   F1=@sin;	% F1 refers to function sin()
F2=@cos; % F2 refers to function cos()
 
% varios ways to call the refered function
F1(pi/4)
F2(pi/4)
feval(@sin,pi/4)
feval(@cos,pi/4)
feval(F1,pi/4)
feval(F2,pi/4)
 
% named functions, stored as strings
feval('sin',pi/4)
feval('cos',pi/4)
F3 = 'sin';
F4 = 'cos';
feval(F3,pi/4)
feval(F4,pi/4)

Maxima[edit]

callee(n) := (print(sconcat("called with ", n)), n + 1)$
caller(f, n) := sum(f(i), i, 1, n)$
caller(callee, 3);
"called with 1"
"called with 2"
"called with 3"

MAXScript[edit]

fn second =
(
print "Second"
)
 
fn first func =
(
func()
)
 
first second

Metafont[edit]

We can simulate this by using scantokens, which digests a string as if it would be a source input.

def calcit(expr v, s) = scantokens(s & decimal v) enddef;
 
t := calcit(100.4, "sind");
show t;
end

МК-61/52[edit]

6	ПП	04
П7 КПП7 В/О
1 В/О

Note: as the receiver of argument used register Р7; the result is "1" on the indicator.

Modula-3[edit]

MODULE Proc EXPORTS Main;
 
IMPORT IO;
 
TYPE Proc = PROCEDURE();
 
PROCEDURE Second() =
BEGIN
IO.Put("Second procedure.\n");
END Second;
 
PROCEDURE First(proc: Proc) =
BEGIN
proc();
END First;
 
BEGIN
First(Second);
END Proc.

Morfa[edit]

Translation of: D
 
func g(a: int, b: int, f: func(int,int): int): int
{
return f(a, b);
}
 
import morfa.base;
 
func main(): void
{
println("Add: ", g(2, 3, func(a: int, b: int) { return a + b; }));
println("Multiply: ", g(2, 3, func(a: int, b: int) { return a * b; }));
}
 

Nemerle[edit]

Functions must declare the types of their parameters in Nemerle. Function types in Nemerle are written params type -> return type, as seen in the simple example below.

Twice[T] (f : T -> T, x : T) : T { f(f(x)) }

NewLISP[edit]

> (define (my-multiply a b) (* a b))
(lambda (a b) (* a b))
> (define (call-it f x y) (f x y))
(lambda (f x y) (f x y))
> (call-it my-multiply 2 3)
6
 

Nim[edit]

proc first(fn: proc): auto =
return fn()
 
proc second(): string =
return "second"
 
echo first(second)

Oberon-2[edit]

Works with oo2c version 2

 
MODULE HOFuns;
IMPORT
NPCT:Tools,
Out;
TYPE
Formatter = PROCEDURE (s: STRING; len: LONGINT): STRING;
VAR
words: ARRAY 8 OF STRING;
 
PROCEDURE PrintWords(w: ARRAY OF STRING; format: Formatter);
VAR
i: INTEGER;
BEGIN
i := 0;
WHILE (i < LEN(words)) DO
Out.Object(format(words[i],16));
INC(i)
END;
Out.Ln
END PrintWords;
BEGIN
words[0] := "Al-Andalus";
words[1] := "contributed";
words[2] := "significantly";
words[3] := "to";
words[4] := "the";
words[5] := "field";
words[6] := "of";
words[7] := "medicine";
 
PrintWords(words,Tools.AdjustLeft);
PrintWords(words,Tools.AdjustCenter);
PrintWords(words,Tools.AdjustRight)
END HOFuns.
 

Objeck[edit]

 
bundle Default {
class HighOrder {
function : Main(args : String[]) ~ Nil {
f := GetSize(String) ~ Int;
Print(f);
}
 
function : GetSize(s : String) ~ Int {
return s->Size();
}
 
function : Print(func : (String)~Int) ~ Nil {
func("Hello World!")->PrintLine();
}
}
}
 

OCaml[edit]

A function is just a value that wants arguments:

# let func1 f = f "a string";;
val func1 : (string -> 'a) -> 'a = <fun>
# let func2 s = "func2 called with " ^ s;;
val func2 : string -> string = <fun>
 
# print_endline (func1 func2);;
func2 called with a string
- : unit = ()

Or, with an anonymous function:

# let func f = f 1 2;;
val func : (int -> int -> 'a) -> 'a = <fun>
 
# Printf.printf "%d\n" (func (fun x y -> x + y));;
3
- : unit = ()

Note that func (fun x y -> x + y) is equivalent to func (+). (Operators are functions too.)

Octave[edit]

We can pass a function handle (@function_name)

function r = computeit(f, g, v)
r = f(g(v));
endfunction
 
computeit(@exp, @sin, pi/3)
computeit(@log, @cos, pi/6)

Or pass the string name of the function and use the feval primitive.

function r = computeit2(f, g, v)
r = f(feval(g, v));
endfunction
 
computeit2(@exp, "sin", pi/3)

Oforth[edit]

If you add # before a function or method name you push the function object on the stack (instead of performing the function). This allows to pass functions to other functions, as for any other object. Here we pass #1+ to map :

[1, 2, 3, 4, 5 ] map(#1+)

ooRexx[edit]

routines are first class ooRexx objects that can be passed to other routines or methods and invoked.

 
say callit(.routines~fib, 10)
say callit(.routines~fact, 6)
say callit(.routines~square, 13)
say callit(.routines~cube, 3)
say callit(.routines~reverse, 721)
say callit(.routines~sumit, 1, 2)
say callit(.routines~sumit, 2, 4, 6, 8)
 
-- call the provided routine object with the provided variable number of arguments
::routine callit
use arg function
args = arg(2, 'a') -- get all arguments after the first to pass along
return function~callWith(args) -- and pass along the call
 
::routine cube
use arg n
return n**3
 
::routine square
use arg n
return n**2
 
::routine reverse
use arg n
return reverse(n)
 
::routine fact
use arg n
accum = 1
loop j = 2 to n
accum = accum * j
end
return accum
 
::routine sumit
use arg n
accum = 0
do i over arg(1, 'a') -- iterate over the array of args
accum += i
end
return accum
 
::routine fib
use arg n
if n == 0 then
return n
if n == 1 then
return n
last = 0
next = 1
loop j = 2 to n;
current = last + next
last = next
next = current
end
return current
 
 

Order[edit]

Functions in Order can accept any other named function, local variable, or anonymous function as arguments:

 
#include <order/interpreter.h>
 
#define ORDER_PP_DEF_8func1 ORDER_PP_FN ( \
8fn(8F, \
8ap(8F, 8("a string")) ))

 
#define ORDER_PP_DEF_8func2 ORDER_PP_FN ( \
8fn(8S, \
8adjoin(8("func2 called with "), 8S ) ))

 
ORDER_PP(
8func1(8func2)
)
// -> "func2 called with ""a string"
 
#define ORDER_PP_DEF_8func3 ORDER_PP_FN ( \
8fn(8F, \
8ap(8F, 1, 2) ))

 
ORDER_PP(
8func3(8plus)
)
// -> 3
 
ORDER_PP(
8ap( 8fn(8X, 8Y, 8mul(8add(8X, 8Y), 8sub(8X, 8Y))), 5, 3)
)
// -> 16
 

The only difference between toplevel function definitions, and variables or literals, is that variables and anonymous functions must be called using the 8ap syntactic form rather than direct argument application syntax. This is a limitation of the C preprocessor.

OxygenBasic[edit]

 
'FUNCTION TO BE PASSED
'=====================
 
function f(double d,e) as double
return (d+e)*2
end function
 
 
'FUNCTION TAKING A FUNCTION AS AN ARGUMENT
'=========================================
 
function g(sys p) as string
 
declare function x(double d,e) as double at p
 
return x(10,11)
 
end function
 
 
'TEST: PASSING ADDRESS OF FUNCTION f
'===================================
 
'the name 'f' is combined with the prototype signature '#double#double'
'@' signifies the address of the function is being passed
 
print g(@f#double#double) 'result '42'
 
 

Oz[edit]

Functions are just regular values in Oz.

declare
fun {Twice Function X}
{Function {Function X}}
end
in
{Show {Twice Sqrt 81.0}} %% prints 3.0

PARI/GP[edit]

Works with: PARI/GP version 2.4.2 and above
secant_root(ff,a,b)={
e = eps() * 2;
aval=ff(a);
bval=ff(b);
while (abs(bval) > e,
oldb = b;
b = b - (b - a)/(bval - aval) * bval;
aval = bval;
bval = ff(b);
a = oldb
);
b
};
addhelp(secant_root, "secant_root(ff,a,b): Finds a root of ff between a and b using the secant method.");
 
eps()={
precision(2. >> (32 * ceil(default(realprecision) * 38539962 / 371253907)), 9)
};
addhelp(eps,"Returns machine epsilon for the current precision.");

Pascal[edit]

Standard Pascal (will not work with Turbo Pascal):

program example(output);
 
function first(function f(x: real): real): real;
begin
first := f(1.0) + 2.0;
end;
 
function second(x: real): real;
begin
second := x/2.0;
end;
 
begin
writeln(first(second));
end.

Turbo Pascal (will not work with Standard Pascal):

program example;
 
type
FnType = function(x: real): real;
 
function first(f: FnType): real;
begin
first := f(1.0) + 2.0;
end;
 
{$F+}
function second(x: real): real;
begin
second := x/2.0;
end;
{$F-}
 
begin
writeln(first(second));
end.

Perl[edit]

sub another {
# take a function and a value
my $func = shift;
my $val = shift;
 
# call the function with the value as argument
return $func->($val);
};
 
sub reverser {
return scalar reverse shift;
};
 
# pass named coderef
print another \&reverser, 'data';
# pass anonymous coderef
print another sub {return scalar reverse shift}, 'data';
 
# if all you have is a string and you want to act on that,
# set up a dispatch table
my %dispatch = (
square => sub {return shift() ** 2},
cube => sub {return shift() ** 3},
rev => \&reverser,
);
 
print another $dispatch{$_}, 123 for qw(square cube rev);
sub apply (&@) {            # use & as the first item in a prototype to take bare blocks like map and grep
my ($sub, @ret) = @_; # this function applies a function that is expected to modify $_ to a list
$sub->() for @ret; # it allows for simple inline application of the s/// and tr/// constructs
@ret
}
 
print join ", " => apply {tr/aeiou/AEIOU/} qw/one two three four/;
# OnE, twO, thrEE, fOUr
sub first {shift->()}
 
sub second {'second'}
 
print first \&second;
 
print first sub{'sub'};

Perl 6[edit]

The best type to use for the parameter of a higher-order function is Callable (implied by the & sigil), a role common to all function-like objects. For an example of defining and calling a second-order function, see Functional Composition.

Convenient syntax is provided for anonymous functions, either a bare block, or a parameterized block introduced with ->, which serves as a "lambda":

sub twice(&todo) {
todo(); todo(); # declaring &todo also defines bare function
}
twice { say "Boing!" }
# output:
# Boing!
# Boing!
 
sub twice-with-param(&todo) {
todo(0); todo(1);
}
twice-with-param -> $time {
say "{$time+1}: Hello!"
}
# output:
# 1: Hello!
# 2: Hello!

Phix[edit]

Copy of Euphoria

procedure use(integer fi, integer a, integer b)
print(1,call_func(fi,{a,b}))
end procedure
 
function add(integer a, integer b)
return a + b
end function
 
use(routine_id("add"),23,45)
Output:
68

PHP[edit]

function first($func) {
return $func();
}
 
function second() {
return 'second';
}
 
$result = first('second');

Or, with an anonymous function in PHP 5.3+:

function first($func) {
return $func();
}
 
$result = first(function() { return 'second'; });

PicoLisp[edit]

: (de first (Fun)
(Fun) )
-> first
 
: (de second ()
"second" )
-> second
 
: (first second)
-> "second"
 
: (de add (A B)
(+ A B) )
-> add
 
: (add 1 2)
-> 3
 
: (de call-it (Fun X Y)
(Fun X Y) )
-> call-it
 
: (call-it add 1 2)
-> 3
 
: (mapcar inc (1 2 3 4 5))
-> (2 3 4 5 6)
 
: (mapcar + (1 2 3) (4 5 6))
-> (5 7 9)
 
: (mapcar add (1 2 3) (4 5 6))
-> (5 7 9)


PL/I[edit]

 
f: procedure (g) returns (float);
declare g entry (float);
 
get (x);
put (g(x));
end f;
 
x = f(p); /* where "p" is the name of a function. */
 

Pop11[edit]

;;; Define a function
define x_times_three_minus_1(x);
return(3*x-1);
enddefine;
 
;;; Pass it as argument to built-in function map and print the result
mapdata({0 1 2 3 4}, x_times_three_minus_1) =>

PostScript[edit]

Library: initlib
 
/x_times_3_sub_1 {3 * 1 sub}.
[0 1 2 3 4] {x_times_3_sub_1} map
 

PowerShell[edit]

Works with: PowerShell version 4.0
 
function f ($y) {
$y*$y
}
function g (${function:f}, $y) {
(f $y)
}
 

You can implement a function inside a function.

 
function g2($y) {
function f2($y) {
$y*$y
}
(f2 $y)
}
 

Calling:

 
g f 5
g2 9
 

Output:

25
81

Prolog[edit]

 
first(Predicate) :- call(Predicate).
second(Argument) :- write(Argument).
 
:-first(second('Hello World!')).
 

PureBasic[edit]

Prototype.d func(*text$)
 
Procedure NumberTwo(arg$)
Debug arg$
EndProcedure
 
Procedure NumberOne(*p, text$)
Define MyFunc.func=*p
MyFunc(@text$)
EndProcedure
 
NumberOne(@NumberTwo(),"Hello Worldy!")

Python[edit]

Works with: Python version 2.5
def first(function):
return function()
 
def second():
return "second"
 
result = first(second)

or

  result = first(lambda: "second")

Functions are first class objects in Python. They can be bound to names ("assigned" to "variables"), associated with keys in dictionaries, and passed around like any other object.

Q[edit]

Its helpful to remember that in Q, when parameters aren't named in the function declaration, x is assumed to be the first parameter.

 
q)sayHi:{-1"Hello ",x;}
q)callFuncWithParam:{x["Peter"]}
q)callFuncWithParam sayHi
Hello Peter
q)callFuncWithParam[sayHi]
Hello Peter

R[edit]

f <- function(f0) f0(pi) # calc. the function in pi
tf <- function(x) x^pi # a func. just to test
 
print(f(sin))
print(f(cos))
print(f(tf))

Racket[edit]

 
#lang racket/base
(define (add f g x)
(+ (f x) (g x)))
(add sin cos 10)
 

Raven[edit]

This is not strictly passing a function, but the string representing the function name.

define doit use $v1
"doit called with " print $v1 print "\n" print
 
define callit use $v2
"callit called with " print $v2 print "\n" print
$v2 call
 
23.54 "doit" callit
Output:
callit called with doit
doit called with 23.54

REBOL[edit]

rebol [
Title: "Function Argument"
Author: oofoe
Date: 2009-12-19
URL: http://rosettacode.org/wiki/Function_as_an_Argument
]

 
map: func [
"Apply function to contents of list, return new list."
f [function!] "Function to apply to list."
data [block! list!] "List to transform."
/local result i
][
result: copy [] repeat i data [append result f i] result]
 
square: func [
"Calculate x^2."
x [number!]
][x * x]
 
cube: func [
"Calculate x^3."
x [number!]
][x * x * x]
 
; Testing:
 
x: [1 2 3 4 5]
print ["Data: " mold x]
print ["Squared:" mold map :square x]
print ["Cubed: " mold map :cube x]
print ["Unnamed:" mold map func [i][i * 2 + 1] x]

Output:

Data:    [1 2 3 4 5]
Squared: [1 4 9 16 25]
Cubed:   [1 8 27 64 125]
Unnamed: [3 5 7 9 11]

Retro[edit]

: disp ( nq- )
do putn ;
 
31 [ ( n-n ) 100 * ] disp
 

REXX[edit]

/*REXX program demonstrates passing a function as a name to another function. */
n=3735928559
funcName = 'fib'  ; q= 10; call someFunk funcName, q; call tell
funcName = 'fact'  ; q= 6; call someFunk funcName, q; call tell
funcName = 'square'  ; q= 13; call someFunk funcName, q; call tell
funcName = 'cube'  ; q= 3; call someFunk funcName, q; call tell
q=721; call someFunk 'reverse',q; call tell
say copies('═', 30) /*display a nice separator fence */
say 'done as' d2x(n)"." /*prove that variable N is still intact*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────one─liner subroutines─────────────────────*/
cube: return n**3
fact:  !=1; do j=2 to n;  !=!*j; end; return !
reverse: return 'REVERSE'(n)
someFunk: procedure; arg ?,n; signal value (?); say result 'result'; return
square: return n**2
tell: say right(funcName'('q") = ",20) result; return
/*────────────────────────────────────────────────────────────────────────────*/
fib: if n==0 | n==1 then return n; _=0; a=0; b=1
do j=2 to n; _=a+b; a=b; b=_; end; return _

output

          fib(10) =  55
          fact(6) =  720
       square(13) =  169
          cube(3) =  27
        cube(721) =  127
══════════════════════════════
done as DEADBEEF.

Ruby[edit]

With a proc (procedure):

succ = proc{|x| x+1}
def to2(&f)
f[2]
end
 
to2(&succ) #=> 3
to2{|x| x+1} #=> 3

With a method:

def succ(n)
n+1
end
def to2(m)
m[2]
end
 
meth = method(:succ)
to2(meth) #=> 3


Rust[edit]

Functions are first class values and identified in the type system by implementing the FnOnce, FnMut or the Fn trait which happens implicitly for functions and closures.

fn execute_with_10<F: Fn(u64) -> u64> (f: F) -> u64 {
f(10)
}
 
fn square(n: u64) -> u64 {
n*n
}
 
fn main() {
println!("{}", execute_with_10(|n| n*n )); // closure
println!("{}", execute_with_10(square)); // function
}
Output:
100
100

Scala[edit]

def functionWithAFunctionArgument(x : int, y : int, f : (int, int) => int) = f(x,y)

Call:

functionWithAFunctionArgument(3, 5, {(x, y) => x + y}) // returns 8

Scheme[edit]

A function is just a value that wants arguments:

> (define (func1 f) (f "a string"))
> (define (func2 s) (string-append "func2 called with " s))
> (begin (display (func1 func2)) (newline))
func2 called with a string

Or, with an anonymous function:

> (define (func f) (f 1 2))
> (begin (display (func (lambda (x y) (+ x y)))) (newline))
3

Note that (func (lambda (x y) (+ x y))) is equivalent to (func +). (Operators are functions too.)

Sidef[edit]

func first(f) {
return f();
}
 
func second {
return "second";
}
 
say first(second); # => "second"
say first(func { "third" }); # => "third"

Slate[edit]

Methods and blocks can both be passed as arguments to functions (other methods and blocks):

define: #function -> [| :x | x * 3 - 1].
#(1 1 2 3 5 8) collect: function.

Smalltalk[edit]

first := [ :f | f value ].
second := [ 'second' ].
Transcript show: (first value: second).
function := [:x | x * 3 - 1].
#(1 1 2 3 5 8) collect: function.

Sparkling[edit]

function call_me(func, arg) {
return func(arg);
}
 
let answer = call_me(function(x) { return 6 * x; }, 7);
print(answer);

Standard ML[edit]

- fun func1 f = f "a string";
val func1 = fn : (string -> 'a) -> 'a
- fun func2 s = "func2 called with " ^ s;
val func2 = fn : string -> string
 
- print (func1 func2 ^ "\n");
func2 called with a string
val it = () : unit

Or, with an anonymous function:

- fun func f = f (1, 2);
val func = fn : (int * int -> 'a) -> 'a
 
- print (Int.toString (func (fn (x, y) => x + y)) ^ "\n");
3
val it = () : unit

Note that func (fn (x, y) => x + y) is equivalent to func op+. (Operators are functions too.)

Swift[edit]

func func1(f: String->String) -> String { return f("a string") }
func func2(s: String) -> String { return "func2 called with " + s }
println(func1(func2)) // prints "func2 called with a string"

Or, with an anonymous function:

func func3<T>(f: (Int,Int)->T) -> T { return f(1, 2) }
println(func3 {(x, y) in x + y}) // prints "3"

Note that {(x, y) in x + y} can also be written as {$0 + $1} or just +.

Tcl[edit]

# this procedure executes its argument:
proc demo {function} {
$function
}
# for example:
demo bell

It is more common to pass not just a function, but a command fragment or entire script. When used with the built-in list command (which introduces a very useful degree of quoting) this makes for a very common set of techniques when doing advanced Tcl programming.

# This procedure executes its argument with an extra argument of "2"
proc demoFrag {fragment} {
{*}$fragment 2
}
# This procedure executes its argument in the context of its caller, which is
# useful for scripts so they get the right variable resolution context
proc demoScript {script} {
uplevel 1 $script
}
 
# Examples...
set chan stderr
demoFrag [list puts $chan]
demoFrag {
apply {x {puts [string repeat ? $x]}}
}
demoScript {
parray tcl_platform
}

TI-89 BASIC[edit]

TI-89 BASIC does not have first-class functions; while function definitions as stored in variables are fully dynamic, it is not possible to extract a function value from a variable rather than calling it. In this case, we use the indirection operator #, which takes a string and returns the value of the named variable, to use the name of the function as something to be passed.

The function name passed cannot be that of a local function, because the local function map does not see the local variables of the enclosing function.

Local map
Define map(f,l)=Func
Return seq(#f(l[i]),i,1,dim(l))
EndFunc
Disp map("sin", {0, π/6, π/4, π/3, π/2})

Toka[edit]

Toka allows obtaining a function pointer via the ` (backtick) word. The pointers are passed on the stack, just like all other data.

[ ." First\n" ] is first
[ invoke ] is second
` first second

Trith[edit]

Due to the homoiconic program representation and the concatenative nature of the language, higher-order functions are as simple as:

: twice 2 times ;
: hello "Hello, world!" print ;
[hello] twice

TXR[edit]

lambda passed to mapcar with environment capture:

@(bind a @(let ((counter 0))
(mapcar (lambda (x y) (list (inc counter) x y))
'(a b c) '(t r s))))
@(output)
@ (repeat)
@ (rep)@a:@(last)@a@(end)
@ (end)
@(end)
1:a:t
2:b:r
3:c:s

Ursala[edit]

Autocomposition is a user defined function that takes a function as an argument, and returns a function equivalent to the given functon composed with itself.

(autocomposition "f") "x" = "f" "f" "x"

test program:

#import flo
#cast %e
 
example = autocomposition(sqrt) 16.0

output:

2.000000e+00


V[edit]

Define first as multiplying two numbers on stack

[first *].

Define second as applying the passed quote on stack

[second i].

Pass the first enclosed in quote to second which applies it on stack.

2 3 [first] second
=6

Visual Prolog[edit]

 
domains
intFunction = (integer In) -> integer Out procedure (i).
 
class predicates
addone : intFunction.
doTwice : (intFunction, integer) -> integer procedure (i, i).
 
clauses
doTwice(Pred,X) = Y :- Y = Pred(Pred(X)).
 
addone(X) = Y := Y = X + 1.
 
run():-
init(),
write(dotwice(addone,2)),
succeed().
 

zkl[edit]

Everything is a first class object so

fcn f(g){g()} fcn g{"Hello World!".println()}
Output:
f(g)
"Hello World!"

or

fcn f(g){g()}
f( fcn{"Hello World!".println()} )