Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

1.5 Functions

Declaration

Functions are declared using lambda syntax assigned to a variable:

let add = (a:int b:int):>int => {
    return a + b
}

let greet = ():>void => {
    # do something
    return void
}
  • All parameters must have type annotations (e.g., a:int)
  • Return type annotation is required using :> syntax (e.g., :>int)
  • Parameters are space-separated (no commas)
  • All functions must explicitly return
  • Functions may only be declared at top level (no nested functions)

External Declarations

udewy supports top-level external declarations for functions and globals provided by linked artifacts:

let SDL_Init = (flags:int):>bool => extern
let SDL_Quit = ():>void => extern
let errno:int = extern

Extern declarations use the same syntax as ordinary top-level declarations, but replace the function body or initializer with the extern keyword.

Rules:

  • Extern declarations are only valid at top level
  • Local extern declarations are a compile-time error
  • Extern functions participate in normal forward-reference resolution by name
  • Extern globals and functions are resolved by the native linker, not by udewy source imports
  • Backends that do not support native external linking may reject extern

Forward References

Functions can be called before they are defined. Unknown identifiers during parsing are treated as forward function references and resolved at the end of compilation. If a forward reference remains undefined, compilation fails. Only functions may be forward-referenced; globals, constants, and ignored type declarations must be declared before use.

Calling Convention

Functions follow platform-specific calling conventions (see backend addendums). Arguments are passed in registers and/or on the stack depending on the backend.

No Closures

udewy does not support closures. Functions cannot capture variables from enclosing scopes. All functions operate only on their parameters, global variables, and locally declared variables.