4

What is the reason today's programming languages just allow one method return value?

I would like to know several reasons why e.g. Java does not allow to have a method

public String, Car, Driver exampleMethodName(String name, Car car, Driver driver);

and assign the return values e.g. like this:

String name;
Car ford;
Driver john;
name, ford, john = exampleMethodName(String name, Car car, Driver driver)

Using the same concept and syntax as the parameters during a method call.

It is not possible in the most languages, but why? There are may ways to solve this problem like using arrays, lists or custom objects.

Which concept speaks against having different (different types) multiple return values?

  • because you can use Beans! – donfuxx Mar 02 '14 at 23:19
  • Voting to close as primarily opinion-based. As an aside, Go is a counterexample. – Paul Bellora Mar 02 '14 at 23:20
  • @PaulBellora so is python – Jason Mar 02 '14 at 23:21
  • How would you assign the returned value to a variable? – tom Mar 02 '14 at 23:22
  • This might answer the question: http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from – justderb Mar 02 '14 at 23:22
  • @tom: Languages that allow multiple return values also typically allow multi-variable assignments, e.g. `foo, bar := baz(7)`. – ruakh Mar 02 '14 at 23:23
  • 1
    There doesn't need to be a reason for features *not* to exist. A feature doesn't exist until somebody thinks of it and actually makes it. The second part is the most important. An idea is a bit of ephemeral nothingness until somebody brings it to life. The default state of existence is non-existence. – John Kugelman Mar 02 '14 at 23:23
  • Lots of languages allow pass by reference (C/C++ for example) which allows you to change the contents of the values at their reference point, making the need to return multiple results redundant, this is also known as function side effect, which needs to be balanced with good documentation - an argument for another day. Since many languages use C/C++ as a bases, they tend to follow this paradigm of a single result from a function/method. I'm sure there are lots of other technical issues involved as well. I'd be more curious over the need to do this personally – MadProgrammer Mar 02 '14 at 23:23
  • 1
    How do you wan to use this method? String Car Driver variable = exampleMethodName(name, car, driver); – java-love Mar 02 '14 at 23:24
  • @justderb A different issue. Java *does* allow multiple return statements but not returning multiple values from one. – Martin Smith Mar 02 '14 at 23:26
  • Some languages have tuples, but that's not exactly the same. – Sotirios Delimanolis Mar 02 '14 at 23:27
  • so you need a method that returns a String, a Car and a Driver? Just create a bean with the fields: mName, mCar, mDriver and return it in your method – donfuxx Mar 02 '14 at 23:27
  • In Java, you can return multiple values inside a class object. class ReturnResult { String result1; int result2; } ReturnResult methodCall() { return new ReturnResult(...); } In C, C++, C#, and the like, you can pass in multiple arguments in an in/out manner using pointers (references), where you can modify the arguments inside the function body. // C void functionCall(int *a, char**b); // C++ void functionCall(int &a, string &b); // C# void functionCall(ref int a, ref string b); – stackoverflowuser2010 Mar 02 '14 at 23:30
  • 4
    why was this question put "on hold"? It's a perfectly good question. – stackoverflowuser2010 Mar 02 '14 at 23:31
  • 1
    @stackoverflowuser2010 Chances are it would have just got a bunch of fairly speculative answers. – Martin Smith Mar 02 '14 at 23:36
  • 1
    @stackoverflowuser2010 Because there isn't an answer. There doesn't even have to *be* an answer, as John Kugelman points out. – user207421 Mar 02 '14 at 23:39
  • Because, in the context of the standard algebraic statement notation, only one result can be utilized. To allow multiple results would require inventing a new notation, and a suitable one has not bubbled to the surface. – Hot Licks Mar 02 '14 at 23:43
  • Edited the question and answered to @java-love, to assign multiple variables. Removed "my opinion". – Dominik Feininger Mar 02 '14 at 23:46
  • @HotLicks There was a proposal in OOPSLA in the 1980s for multiple l-values, e.g. 'a,b,c = f(x,y,z);' It would also come in rather handy for swaps, e.g. 'a,b = b,a;' I'm not aware that anybody ever implemented it into anything. – user207421 Mar 02 '14 at 23:47
  • @EJP - Like I said, nothing's ever bubbled to the surface. Multa will have it, if I ever get around to it. But I have a play and a book to write first. – Hot Licks Mar 02 '14 at 23:51
  • As for why this was put on hold, I guess read [Good Subjective, Bad Subjective](http://blog.stackoverflow.com/2010/09/good-subjective-bad-subjective/). And as some have pointed out some languages do allow multiple return values so this morphs the question in to something more like "why do *some* languages ...?" in which case there is no 1 answer. – Radiodef Mar 02 '14 at 23:52
  • I believe this "feature" doesn't exist because the "=" operator would not be atomic anymore. And that would lead to quite some confusion. – EasterBunnyBugSmasher Mar 03 '14 at 00:37
  • @DirkHaase: I'm not sure what language you have in mind, but IME `=` is not usually atomic by default anyway. – ruakh Mar 03 '14 at 00:53
  • @Ruakh : You confuse me. "not usually atomic by default"? That sounds as if I could configure java so it is atomic or not. And I'm talking about java. "=" is atomic in java. You cannot assign half of a pointer. – EasterBunnyBugSmasher Mar 03 '14 at 01:02
  • @DirkHaase: By "by default", I meant, without (say) `volatile`. It's not a language-level configuration, but a variable-level configuration. :-) But, for your larger point -- it may be true that "you cannot assign half of a pointer", but not all assignments in Java are of pointers. See the SO question [long and double assignments are not atomic - How does it matter?](http://stackoverflow.com/q/17481153/978917). – ruakh Mar 03 '14 at 01:08

1 Answers1

1

Probably, the reason is that by definition a function is supposed to have exactly one output.

http://en.wikipedia.org/wiki/Function_(mathematics)

Andres
  • 10,561
  • 4
  • 45
  • 63
  • 1
    Of course, that same definition also states that a function is supposed to have exactly one *input*, which is not a requirement that most languages impose. (There are some languages with that property, especially functional languages such as Standard ML and Haskell, but that's clearly not the sort of language the OP has in mind.) – ruakh Mar 02 '14 at 23:30
  • 1
    This goes right until you go down there and find this section: [Functions with multiple inputs and outputs](http://en.wikipedia.org/wiki/Function_(mathematics)#Functions_with_multiple_inputs_and_outputs) – Luiggi Mendoza Mar 02 '14 at 23:31
  • @ruakh It says no such thing. It *specifically* refers to 'input variable(s)', and to 'tuple of inputs (if the function takes more than one input)'. – user207421 Mar 02 '14 at 23:36
  • @EJP: A function is defined as mapping from a single input (the preimage) to a single output (the image). The OP links to the Wikipedia article, which you seem to be quoting from, but you're quoting rather selectively; the very first sentence says that "each input is related to exactly one output". (The article is inconsistent about this, for the simple reason that mathematicians are flexible about the term "function" and let context imply the generalization being used; but the basic definition implies one input and one output, and both can be generalized.) – ruakh Mar 02 '14 at 23:46
  • That's simply a tautology. – Hot Licks Mar 02 '14 at 23:49
  • @ruakh I'm quoting what it says. You don't seem to have got past the first sentence yourself. I see no inconsistency. I'm a mathematician by training and I've never heard of a one-argument rule. There is a domain and a range and both are sets. – user207421 Mar 02 '14 at 23:49
  • @EJP: Re: "There is a domain and a range and both are sets": Yes, exactly. *A* domain, *a* range. The function maps each single input value in the domain to a single output value in the range. What are you not getting? – ruakh Mar 03 '14 at 00:43
  • Nothing in the mathematical definition of a function precludes the possibility of the thing which it returns being some form of aggregate. It's common in some fields, for example, to have functions that return vectors or matrices. – supercat Dec 23 '14 at 20:56