v0.4.0

return

return ends the function it is in and hands a value back to the caller.

return-statement = "return" [expression] ";"
func Sign(n: int32) -> int32 {
    if n < 0 {
        return -1;
    }
    match n {
        0 => return 0,
        _ => return 1
    }
}

With and without a value

A function with a return type returns a value of that type; one without a return type returns with a bare return;, or simply by reaching the end of its body:

func Log(message: char8[..]) {
    if message.length == 0 {
        return;
    }
    PrintLine("{}", message);
}
MistakeError
a value of the wrong type'return' value must have type 'int', but found 'char8[..]'
a value from a function with no return type'return' cannot have a value in a function with no return type
no value from a function with a return type'return' requires a value of type 'int'

The value takes its type from the function the way an annotated binding does: an unsuffixed literal takes the return type, and a narrower integer widens to it.

In a function that declares throws E, return value; completes with the success and throw error; leaves with an error. A function that throws and has no return type completes with () when it returns or falls off its end.

Every path returns

A function with a return type must return on every path through its body. The compiler follows each branch, and one that can reach the closing brace is an error:

func Positive(x: int) -> int {
    if x > 0 {
        return 1;
    }
}
error: function 'Positive' must return a value of type 'int' on every control-flow path

A path that cannot continue counts as finished: a match whose every arm returns, a loop with no break, a call to Panic or to any function marked #NoReturn:

func Day(n: uint) -> char8[..] {
    if n < 7 {
        return "weekday";
    }
    Panic("no such day");
}

Returning a move-only value

return name; copies name into the result, like any other use by value, and only plain data copies. A type that owns resources or is marked #NoCopy is handed out with move:

func Pass(token: Token) -> Token {
    return move token;
}

Without the move, for a #NoCopy Token:

error: cannot copy 'Token', which prohibits copying
  note: a named value used by value in return position is copied, and only plain data copies
  help: write 'move token' to transfer it

For a type that owns resources the message is cannot copy 'String', which owns resources, and the help also offers name.Clone(). A freshly made value, such as a structure literal or the result of a call, needs no move. See Copy and move.

return and defer

return value; evaluates value exactly once, then runs the function's deferred statements, newest first, and only then leaves. An error leaving through the function's throws channel runs them the same way, after the error has been captured. Changes a deferred statement makes to a local after that point do not alter the value already captured. See defer.

Inside expressions

return, with or without a value, may also stand where an expression would leave rather than produce one: as a whole match or catch arm, or as the fallback of ??:

func First(values: int32[..]) -> int32 {
    let found: int32? = if values.length > 0 { .Some(values[0]) } else { none };
    let v = found ?? return -1;
    return v;
}

Anywhere else it is error: 'return' cannot be used as a value here.

See also

  • Functions — return types
  • main — what Main returns to the operating system
  • Errors — throw, the other way out of a function that throws
  • defer — what runs on the way out
  • Learn: Return