Ad space (banner)
๐ŸŸฃJulia Lessons
Lesson 47 / 68

Returning several values from a function

This lesson covers returning several values from one function in Julia, with the aim of writing functions not limited to a single return value.

A Julia function returns several values as a tuple simply by separating them with commas, as in return a, b. Picture handing over one box containing two cards, the minimum and the maximum.

In the sample code, min_max() returns both with return minimum(nums), maximum(nums). The caller receives them together through the destructuring assignment smallest, largest = min_max(...).

A common early stumble is that this mechanism is in fact just returning a tuple. It looks like several values, but internally it is one tuple object.

In professional work, this simple form handles in Julia what other languages need an array or an object to express.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Julia directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)