Next: , Up: Functions   [Contents][Index]

14.12.1 Function Declarations

A function is declared using the following syntax:

fun name = [(formal,…)] ret_type:
   … body …

where name is the name of the function, which uses the same namespace as variables and types and ret_type is the type of the value returned by the function. If the function returns no value then it is void.

Each formal argument has the form:

type name [= exp]

where type is the type of the formal, name its name, and exp is an optional expression that will be used to initialize the argument in case it is not specified when the function is called.

The last formal argument can take the form name, meaning the function is variadic. See Variadic Functions.

If the function takes no arguments, it is possible to omit the list of arguments entirely:

fun hello = void: { print "Hello!\n"; }

The return statement is used to return values in functions that return a value. Example:

fun gcd = (uint<64> a, uint<64> b) uint<64>:
   if (b == 0)
     return a;
     return gcd (b, a % b);

Note that reaching the end of a non-void function will trigger a run-time error.