An example function might be declared like this:
int gcf ( int a, int b ) { int f = 1; for ( int i = 2; i <= abs ( a ) && i <= abs ( b ); ++i ) while ( ( a // f ) % i == 0 && ( b // f ) % i == 0 ) f *= i; return f; }
First comes the return type of the function, then the function name, then the list of arguments (with types), and finally the statement list in curly braces. If any types are left off, Nickle assumes poly. In any case, all typechecking is done at runtime.
A function can take any number of arguments. The final argument may be succeeded by an ellipses (...) to indicate an arbitrary, variable number of succeeding arguments, each of the type of the final argument; the last argument takes on a list value to store them.
... > print sum int sum (int a, int b ...) { for (int i = 0; i < dim (b); ++i) a += b[i]; return a; } > sum(1,2) 3 > sum(4) 4 > sum(1,2,4,6) 13
Functions are called as in C, with their names followed by argument values in parentheses:
Since they are first class, functions can be assigned:
See the section on Copy semantics for details on what functions may be assigned to each other.
Functions may also be declared and used anonymously:
Replacing the function name with the keyword func indicates its anonymity.