# Functions Reference

This section documents the keywords used to define functions and computations in L4. Functions represent individual rules.

## Overview

L4 functions are defined using a combination of keywords that specify parameters, return types, and the function body. The typical structure is:

```l4
GIVEN parameter IS A Type      -- Parameters (optional)
GIVETH A ReturnType            -- Return type (optional)
DECIDE name MEANS expression   -- Definition
```

## Keywords

| Keyword                                  | Purpose                                     | Required                   |
| ---------------------------------------- | ------------------------------------------- | -------------------------- |
| [GIVEN](/l4/reference/functions/GIVEN.md)                        | Declares function parameters with types     | For functions with inputs  |
| [GIVETH](/l4/reference/functions/GIVETH.md) / [GIVES](/l4/reference/functions/GIVETH.md) | Specifies the return type                   | Optional (can be inferred) |
| [DECIDE](/l4/reference/functions/DECIDE.md)                      | Defines a named value or function           | Yes (or use bare MEANS)    |
| [MEANS](/l4/reference/functions/MEANS.md)                        | Connects the function name to its body      | Yes                        |
| [YIELD](/l4/reference/functions/YIELD.md)                        | Creates anonymous functions (lambdas)       | For inline functions       |
| [WHERE](/l4/reference/functions/WHERE.md)                        | Adds local definitions after an expression  | Optional                   |
| [LET](/l4/reference/functions/LET.md)                            | Adds local definitions before an expression | Optional                   |
| [AKA](/l4/reference/functions/AKA.md)                            | Creates aliases for definitions             | Optional                   |

## Quick Examples

### Simple Function

```l4
GIVEN x IS A NUMBER
double x MEANS x TIMES 2
```

### Fully Annotated Function

```l4
GIVEN n IS A NUMBER
GIVETH A NUMBER
DECIDE factorial n MEANS
  IF n EQUALS 0
  THEN 1
  ELSE n TIMES factorial (n MINUS 1)
```

### With Local Definitions

```l4
circleArea radius MEANS
  pi TIMES radius TIMES radius
  WHERE
    pi MEANS 3.14159
```

### With Aliases

```l4
`total cost` AKA `final price` MEANS
  basePrice PLUS tax
```

### Anonymous Functions (Lambdas)

Use `GIVEN ... YIELD` for inline functions:

```l4
GIVEN numbers IS A LIST OF NUMBER
`all positive` MEANS all (GIVEN n YIELD n > 0) numbers
```

## Function Definition Patterns

### Named Constants

No parameters needed:

```l4
DECIDE pi IS 3.14159
DECIDE greeting IS "Hello, World!"
```

### Single Parameter

```l4
GIVEN x IS A NUMBER
square x MEANS x TIMES x
```

### Multiple Parameters

```l4
GIVEN a IS A NUMBER
      b IS A NUMBER
add a b MEANS a PLUS b
```

### Boolean Decisions

Use `DECIDE ... IF` for boolean-returning functions:

```l4
GIVEN age IS A NUMBER
DECIDE `is adult` IF age >= 18
```

## Mixfix Function Definitions

L4 functions do not have to put the function name first. In a **mixfix** definition, the parameters declared with GIVEN are interleaved with the identifier words of the function name, so calls read like natural language:

```l4
GIVEN mom IS A STRING
      dad IS A STRING
      kid IS A STRING
GIVES A STRING
mom and dad `have a baby named` kid MEANS
    CONCAT "mother: ", mom, ", father: ", dad, ", child: ", kid
```

The call site uses the same word order:

```l4
#EVAL "alice" and "bob" `have a baby named` "carol"
```

Key points:

- Parameters may appear before, between, or after the function's identifier words — enabling infix (`x plus y`) and postfix (`n squared`) conventions as well.
- Backtick-quoted identifiers may contain spaces, so the "name words" can be multi-word legal phrases (`` `is entitled to` ``).
- The words of the pattern that are not parameters act as **mixfix keywords**. The typechecker records each mixfix definition — its written pattern and checked type signature — in a **mixfix registry**, which it uses to resolve call sites where arguments and keywords are interleaved.
- Tooling reuses the registry: the document renderer derives prose **headings** for mixfix functions from the registered pattern, rendering each parameter slot in the author's original word order (so even functions that are never called elsewhere get proper headings).

For when to reach for mixfix and more worked examples, see [Advanced Patterns: Mixfix Function Definitions](/l4/reference/patterns.md#mixfix-function-definitions).

## Local Bindings

Two styles for introducing local definitions:

### WHERE (definitions after)

```l4
result MEANS x PLUS y
  WHERE
    x MEANS 10
    y MEANS 20
```

### LET (definitions before)

```l4
result MEANS
  LET x IS 10, y IS 20
  IN x PLUS y
```

## See Also

- **[Types Reference](/l4/reference/types.md)** - Available types for parameters and returns
- **[Operators Reference](/l4/reference/operators.md)** - Operators for expressions
- **[Control Flow Reference](/l4/reference/control-flow.md)** - IF, CONSIDER, and other constructs
