Skip to content

12 Final-expression returns ​

A function with a result type may return its final expression without return.

norm
Integer square(Integer value) {
  value * value
}

Integer sumOfSquares(Integer first, Integer second) {
  square(first) + square(second)
}

main() {
  printLine(square(5))
  printLine(sumOfSquares(first: 3, second: 4))
}

Output:

text
25
25

square returns value * value. sumOfSquares composes two calls and returns their sum. Both function signatures still state Integer; the expression at the end must have a compatible type. This is a return rule, not a change to how arguments are passed.

Try it: Change the final expression of square to a string and inspect the type error.

Precise rules: Language Reference.

Norm 0.25