Spec chapters

5. Functions

fn name(p1: T1, inout p2: T2, sink p3: T3, comptime T: type) R { ... }
  • A parameter is passed by value (the function reads it and cannot assign it; eggc passes one larger than 16 bytes as a pointer to it, which no program can tell apart), inout (section 12), sink or comptime (section 14). For an owned value (section 21) a plain parameter only borrows it, and sink takes it over; a sink parameter of a type that owns nothing is E145, except in a generic instance, where T may or may not own something.
  • A call must pass exactly as many arguments as there are parameters (E104), each of the declared type (E103). An inout argument must be a place of exactly that type: an i64 variable does not fit an inout ?i64, since the function writes a ?i64 into it.
  • A non-void function must return on every path (E107). A block returns on every path when at least one of its statements does, where:
    • return always returns;
    • if returns only when it has an else and every branch returns;
    • switch returns when every arm returns;
    • loops never count, even while (true).
  • A function returning void or !void may run off its end. return with no value is allowed only there, and a void function cannot return a value (E103).
  • Functions declared inside a struct or union are its methods (section 9).
// error: E107
uses io

fn sign(n: i64) i64 {
    if (n < 0) { return -1 }
}

fn main(io: Io) void {
    io.print(sign(1))
}