typestar

Multiple returns in Odin

Boxes and leftovers in one call: named results and the bare return that sends them.

package main

import "core:fmt"

// Named results are declared in the signature; a bare return sends them.
divide :: proc(eggs, box: int) -> (boxes, left: int) {
    boxes = eggs / box
    left = eggs % box
    return
}

main :: proc() {
    full, spare := divide(157, 12)
    fmt.println(full, "boxes,", spare, "eggs in the basket")

    // Extra results can be dropped explicitly with _.
    just_boxes, _ := divide(60, 12)
    fmt.println("five dozen fills", just_boxes)
}

How it works

  1. -> (boxes, left: int) names both results in the signature.
  2. Assigning to them and writing bare return sends the pair back.
  3. _ discards a result the caller does not want — silently dropping is not allowed.

Keywords and builtins used here

The run, in numbers

Lines
19
Characters to type
443
Tokens
92
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 79 seconds.

Type this snippet

Step 2 of 3 in Procedures, step 8 of 27 in Language basics.

← Previous Next →

Multiple returns in other languages