Documentation

Articles

Contribute

Functions

Whiley supports first-class functions similar those found in languages like Python and JavaScript, and to function pointers in C.  The following illustrates the syntax:

int f1(int x):
    return x + 1

int f2(int x):
    return x * 2

int g(int(int) func):
    return func(1234)

void System::main([string] args):
    out.println(str(g(&f1)))
    out.println(str(g(&f2)))

Here, we see that function g() accepts a parameter func of function type.  To generate values of function type, we use the “address-of” operator &.  The parameter func can be invoked inside g() as though it were a normal function.