Most modern programming languages do not allow multiple return types (excluding scenarios like C#'s
out parameters or the newest use of
Tuples). This is because all languages implement the concept of mathematical functions, which for a given number of input parameters, produce only one result (by one I don't necessarily mean a single integer but a single output, even it's a set, a double or anything else).
We can overcome this issue with the use of
objects or in older languages (like C) with a
struct of multiple values. I have seen even arrays employed in certain cases.
But intuitively, we programmers are been discouraged on creating a method that will end up needing to return more than one value; this is considered a bad practice.
But with all the updates in modern languages, does only the aforementioned concept in mathematics, justify why methods should return only one value? Is this only for readability and avoiding common misunderstandings between programmers, as also as compiler optimizations?
In other words, is this just a technical limitation or not?
EDIT: I have no example to show. Personally, I strongly believe that functions should return only ONE value. But how do you explain C#'s 7.0 new features that allow such a concept? As I said to @DFord in the comments section, if we just reason this as "the correct thing" without any explanation, we are effectively excommunicating these features and calling those who created them, idiots.
EDIT 2: I am lost in the comment section. There are those who are for the concept of one return value and those who are against it. If, according to some of you, functions in maths can return multiple values, then why languages were designed to return one value and the concept we are discussing came more lately? I can break down my thoughts into two categories:
- Methods should only return one value, so using intermediate solutions like
structsjust solve a problem that should not be there in the first place
- Developers should return as many values as they want, so both the above as also C#'s
Tuplesare completely justified and not just syntactic sugar.
Note: It's entirely different for developers to find alternatives to implement something that is unsupported (e.g. return an object) and for programming languages to officially support this by providing ways to do it via their syntax -thus making it "legit".