I have a question that I cannot answer myself, but it seems like a fundamentally good question to clear up:
Why do some languages restrict the data returned from a function to a single item?
Is this serving some benefit? Or is it a practice brought over from Maths?
An example being (in Scala):
def login(username: String, password: String): User
If I wanted to return multiple items I cannot say it in the same manner as I just did for the input arguments (now entering imaginary Scala land)
def login(username: String, password: String): (User, Context, String)
Or even with named data returned:
def login(username: String, password: String): (user: User, context: Context, serverMessage: String)
There is no relationship: as observed, an arbitrary number of values can be returned, even if they must be “packaged” into a single value.
Imagine a language that can only accept a single tuple and can only return a single tuple from a function (the tuples can be any size). These functions then resemble math function transforming a vector from one space to another.
However, some reasons why it might be so:
Even while “returning only one value”, programming languages already have different ways of dealing with it. As noted in the post, some languages allow decomposition (the tuple returned as “decomposed” into it’s two values during an assignment):
Additionally, other languages like C# which lacks decomposition, allow pass-by-reference (or emulate it in with mutation of an object):
And, of course… 😉
There are likely more methods I am not aware of.
Happy coding.