← Back to lessons

6 Multiple Return Values

Beginner

If you need to return multiple values from a function you can use tuples, and pattern matching.

Return type of this function is a tuple consisting of two Ints.

Using pattern matching we can decompose a tuple into separate variables of type Int64.

We can use the wildcard _ to match any value if we do not need to use that value.

multiple_return_values.cj
func vals(): (Int64, Int64) {
    return (3, 7)
}

main() {
    var (a, b) = vals()
    println(a)
    println(b)

    var (_, c) = vals()
    println(c)
}

// Output:
// 3
// 7
// 7