Functions

Functions live in fn blocks. A file may have several; they all belong to the same module.

functions.md
---
midad: "0.1"
module: functions
---

# functions

```fn
function square(n: number): number {
  return n * n;
}

function sum_to(n: number): number {
  if (n <= 0) {
    return 0;
  }
  return n + sum_to(n - 1);
}

function shout(text: string): string {
  return upper(text) + "!";
}
```

```exec
print(str(square(7)));
print(str(sum_to(100)));
print(shout("midad"));
```
output
49
5050
MIDAD!

Every parameter is typed and the return type is required. A function that can fall off its end is E0210, which is what keeps a route from answering nothing:

---
midad: "0.1"
module: missing
---

```fn
function classify(n: number): string {
  if (n > 0) {
    return "positive";
  }
}
```

A function with no return value is declared `: void` and may use a bare
`return;`.

## Builtins are functions too

The 72 builtins are called like any function: `len(s)`, `push(xs, x)`,
`json.parse(text)`, `db.query(conn, sql, params)`. The dot in `json.parse` is
part of the name, not a method call on a value; there are no methods in midad,
and `x.foo()` is a syntax error unless `x.foo` is a builtin.

The [builtin reference](../reference/builtins.md) lists every one with the
parameter sets it accepts.

## Functions as arguments

A parameter typed `fn` takes the **name** of a function, with no parentheses:

````midad run higher.md
---
midad: "0.1"
module: higher
---

# higher

```fn
function double(n: number): number {
  return n * 2;
}

function add_one(n: number): number {
  return n + 1;
}

function twice(f: fn, n: number): number {
  return f(f(n));
}
```

```exec
print(str(twice(double, 3)));
print(str(twice(add_one, 3)));
```
output
12
5

This is a compile-time feature, not a value. Before the checker runs, every call to a function with fn parameters is rewritten into a call of an ordinary copy made for the function names in those positions, so the interpreter, the C backend and Cranelift never see fn and cannot disagree about it.

The rules that follow from that:

  • The argument must be the name of a function, a native, an ai:intent or a builtin, or a fn parameter of the enclosing function (which forwards it). Anything else is E0342.
  • A function name anywhere else in an expression is E0341.
  • A function with a fn parameter that is never called is not checked at all, and W009 says so.
  • There are no closures, and nothing captures a variable. A function name cannot be stored in a list or a map, returned, bound with let, or computed at run time. There is no map(xs, f) over a list of functions.

fn has no signature: wrong argument types show up as ordinary type errors inside the copy, with a note saying which function the copy was made for.

Names

Functions and state are expected to be snake_case. A name that is not gets W005 (for a function) or W006 (for state); it is a warning, not an error. A module whose module: does not match its file name gets W004.

On this page