Multiple Return Values in Julia

Julia has built-in support for multiple return values. This feature is used often in idiomatic Julia, for example to return both result and error values from a function.

# The (Int, Int) in this function signature shows that
# the function returns 2 Ints.
function vals()
    return 3, 7
end

# Main function
function main()
    # Here we use the 2 different return values from the
    # call with multiple assignment.
    a, b = vals()
    println(a)
    println(b)

    # If you only want a subset of the returned values,
    # use the underscore _ as a placeholder.
    _, c = vals()
    println(c)
end

# Call the main function
main()

To run the program, save it as multiple_return_values.jl and use the Julia REPL or command line:

$ julia multiple_return_values.jl
3
7
7

Accepting a variable number of arguments is another nice feature of Julia functions; we’ll look at this next.