FUNCTIONS


    -- The simplest possible function
    main ||:
        io("Hello from a function")
    ;
    

The function signature

Function names come first, followed by parameters inside vertical pipes. The arrow symbol defines return slots. Functions can return multiple values.



    get_doubled |value Int| -> Int:
        return value * 2
    ;

    pair || -> String, Int:
        return "Ana", 2
    ;

    doubled = get_doubled(21)
    name, count = pair()
    

Arguments

Positional arguments come before named arguments. Mutable access to an existing place must be explicit at the call site.


    update |value ~Int|:
        value += 1
    ;

    value ~= 1
    update(~value)

    -- Fresh values can satisfy mutable parameters without '~'.
    update(1)
    

Error-returning functions

A function can mark one return slot with `!`. Error-returning calls must be handled with propagation, fallback values, or a named handler.


    parse_number |text String| -> Int, Error!:

        if text.is_empty():
            return! Error("Parse", "int.empty", "Missing number")
        ;

        return 42
    ;

    with_fallback || -> Int:
        value = parse_number("") ! 0
        return value
    ;

    bubble_up |text String| -> Int, Error!:
        value = parse_number(text)!
        return value
    ;

    recover |text String| -> Int:
        value = parse_number(text) err! 0:
            io(err.message)
        ;

        return value
    ;
    

Closures

Closures and `Fn(...)` function-value types are deferred for Alpha. Do not use closure examples as current compiler support yet.