Skip to content

Functions

Functions are declared at the top level. All parameters require type annotations:

function greet(name: text): text {
return "Hello, " + name
}
function add(a: number, b: number): number {
return a + b
}
let message = greet("World") // "Hello, World"
let sum = add(1, 2) // 3

Functions must declare their return type:

function isEven(n: number): boolean {
return n % 2 == 0
}
function getItems(): text[] {
return ["a", "b", "c"]
}

Functions can contain AI expressions:

function summarize(content: text): text {
return do "Summarize this in one sentence: {content}"
}
function analyze(data: text): json {
return do "Analyze this data and return structured insights: {data}"
}

Chain AI calls within functions:

function analyzeAndSummarize(content: text): text {
let analysis = do "Analyze the key themes in: {content}"
return do "Summarize this analysis in one sentence: {analysis}"
}

Variables in scope are automatically visible to AI prompts:

function processArticle(url: text, style: text): text {
let content = fetch(url)
// AI sees both 'content' and 'style' variables
return do "Rewrite this article in a {style} style"
}

Share functions across files with export:

export function formatDate(date: text): text {
return do "Format this date nicely: {date}"
}

See Modules for more on imports and exports.