Functions
Function Declaration
Section titled “Function Declaration”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}Calling Functions
Section titled “Calling Functions”let message = greet("World") // "Hello, World"let sum = add(1, 2) // 3Return Types
Section titled “Return Types”Functions must declare their return type:
function isEven(n: number): boolean { return n % 2 == 0}
function getItems(): text[] { return ["a", "b", "c"]}Functions with AI
Section titled “Functions with AI”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}"}Multi-Step Workflows
Section titled “Multi-Step Workflows”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}"}Context in Functions
Section titled “Context in Functions”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"}Exporting Functions
Section titled “Exporting Functions”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.