Functions

Identifiers

An identifier is the name of a function, variable, type, record field, etc.
An identifier may contain letters, numbers, _ and -, but it can't begin with a number, or begin or end with -.
Examples of valid identifiers: a, is-empty, float32, BIG_WORDS, עורב, 예리한.
Keen prefers kebab-case: lower-case words separated by -.
Keywords like if may not be used as identifiers.

Comments

Comments begin with #.

main void() log "I exist" # log "I don't"

Long comments begin and end with ###.

main void() ### A long comment ### log "Hello"

Prefix function calls

The most commont way to call functions in Keen is with a prefix syntax.

The first column below shows how to call a function f in Keen. The other column shows an equivalent in C.

ff af a, bf a, b, c

f alone calls a function with no arguments. Accessing a variable uses the same syntax.
Keen will access a variable if one is in scope, otherwise call a function.

Multiple function calls can nest without parentheses.
Each new function name is the final argument to the previous function.

f g af g a, bf g a, h bf g h a, b

Dot function calls

a.f is equivalent to (f a). This only works for a function with a single argument.
This often helps avoid parentheses.

f a.g, bf a.g.h, b

Infix function calls

'f calls f with infix syntax. This works with any number of arguments.

a 'f b, ca.f 'g b.h

Function definitions

Now to define a function and call it:

main void() log hello-world hello-world string() "Hello, world!"

log hello-world calls the function log with one argument hello-world, which is a 0-argument function call.

hello-world string() is the function signature. It takes no parameters and returns a string.
The function body always goes beneath the signature and indented.

Here's a modified version that takes a single parameter.

main void() log hello-world 3 hello-world string(times nat) join " ", n-of times, "Hello, world!"

This defines a function hello-world that takes a nat (natural number) and returns a string.
The argument 3 becomes the value of the parameter times, causing the string to repeat 3 times.

The below takes 2 parameters name and times and passes arguments "keen" and 3, respectively.

main void() log hello "Keen", 3 hello string(name string, times nat) join " ", n-of times, "Hello, {name}!"

Functions with many parameters can be written on multiple lines using the below syntax.
do is used to separate the parameters from the function body.

main void() log hello "keen", 3 hello string name string times nat do join " ", n-of times, "Hello, {name}!"