---
title: Macros and functions
description: Define untyped SQL macros and typed typesql functions with bounded, static resolution.
species: reference
---
# Macros and functions

Use `create macro` for an untyped SQL helper. Use `create function` when callers need checked parameters and a checked return type.

```typesql
create function cents_to_dollars(cents: integer!) returns decimal(18, 2) as
  cast(cents as decimal(18, 2)) / 100.0;

select cents_to_dollars(amount)
from titan.stripe.charges
```

## SQL macros

`create macro name(parameters) as expression` follows the query dialect. Parameters may have defaults. Table macros use `as table` followed by a query. The checker treats these as untyped functions.

## Typed functions

A typed function declares each parameter and one scalar or table return. Bodies are expressions or queries, not procedural statements. Resolution is static, and recursion returns `TS0406`.

The checker verifies the body against the declared return. The compiled runtime statement contains supported SQL and no annotation syntax.

## Resolution order

Local declarations and imported package exports form one closed environment. Duplicate exports are errors rather than order-dependent overrides. Function calls are checked for argument count (`TS0404`) and argument type (`TS0405`).

Use packages to share a function across files. Keep a local declaration when its meaning belongs to one model and should be reviewed beside that model.
