views:

373

answers:

7

This always forces us to return a single parameter in case I need to return multiple, say a List and a String. This restriction is not there in function arguments.

+8  A: 

This problem is what functional languages such as F#, haskell etc. attempt to address. The problem is that in hardware, a function's return value was originally returned via a CPU register, so you could only return a single value. C syntax passed on this legacy and C++ (and C#, Java) also inherited this.

Spence
If it is possible with python, then I think hardware limitation may not be a problem. If it is there in Java/CPP it will be really useful.
Techmaddy
As I said, syntactical sugar. I can't see any benefit or usefulness, but yes I am seeing coding nightmare, bad practices, and lot of refactoring.
Adeel Ansari
This is for accumulator-based CPU architectures?
Tom Hawtin - tackline
Which is originally what C was written for on UNIX...Now we have much more knowledge regarding programming languages and make sacrifices in performance for gains in programmer efficacy and efficiency :)
Spence
+4  A: 

Hi Techmaddy,

In fact, some languages do what you exactly want. For example Python, look at this code. It returns 2 values: 1 string and another int.

>>> def myFunc():
...   string_val = "techmaddy"
...   int_val = 10
...   return string_val, int_val
...
>>>
>>> s, i = myFunc()
>>>
>>> print s
techmaddy
>>>
>>> print i
10

I hope it would help.

PS: Under the covers, I guess Python is boxing it in a list and un-boxing it. I don't know Python that well to tell you what exactly it does.

artknish
Syntactical sugar, I would say.
Adeel Ansari
@Vinegar: but oh-so-sweet syntactical sugar at that.
A. Rex
I love Python, wish I could code more in that :(
artknish
@Srikanth - Its game going on here.... I will ask this question, and then you come and answer, I will accept your answer.... How naive!! Please don't pollute.
Adeel Ansari
Python does box the return values in a tuple and unbox them. Technically, it's not even implicit (I think) because something like string_val, int_val creates a tuple even without parentheses.
David Zaslavsky
@Vinegar: Accepting an answer is upto the guy who asked the question, right? I just contributed my part by showing him an example. In fact, if I were to take an answer, I would take Spence's.
artknish
Ya Spence one is appropriate for C, C++, Java and C#. This one is more appropriate example, as I was expecting.
Techmaddy
@Techmaddy - Didn't you tag this question as Java, and not the python. And then you are accepting that the Spence's answer was more appropriate in terms of Java ad whatever. Just dont pollute, for your own sake.
Adeel Ansari
@Srikanth- Yeah, but he already know whose answer is to accept. Just be honest for your own sake. One will get the reputation but lose the soul.
Adeel Ansari
@Vinegar: I really don't know whether to laugh and leave it or to answer you, again. Losing my soul? What did I say? If I were to be asking this question, I would take Spence's answer. I voted Spence's answer up so that it stays at the top. What can I do if he takes my answer?
artknish
@Vinegar: Thanks for the time that you spent for this question. I agree the point that you mentioned that I have added this to Java tag. So, I am going with Spence's answer. Otherwise I am not trying to pollute this. The Python example was more appropriate.
Techmaddy
Thanks you Srikanth and Techmaddy. I really appreciate you both, that you guys clear my doubt. And +1 for the answer and the question.
Adeel Ansari
+3  A: 

or, return Object[] from your method, and do

return new Object[] { list, string};

If you need more parameterized return types, you could create Pair<X,Y> classes, or Triplet<Z,Y,Z> and return those.

John Gardner
Its a single product, so are all of your given options. I hope now it makes sense.
Adeel Ansari
Ya, this is a solution. And I was wondering why the language don't have the feature of parametrized return type. Here the Problem is the return type cannot be controlled. It is dynamic. So normally another way is to define a Object with List and String with setters and getters.
Techmaddy
Its a single return type, think of method declaration.
Adeel Ansari
The problem with returning an untyped list is you lose all type safety. And creating a return object whose sole purpose in life is to pass a return value from a function is ugly.Shame your not in C#, they've used out and ref parameters to try and deal with this.
Spence
Wow a downvote? That's harsh. It was a java question, these are java possible "workarounds". I see that the formatter stripped out my greaterthan/s, so that doesn't help. it was Pair<?,?>. Yes, they aren't the best solution, but they work. Your other option is to use a different language...
John Gardner
Java Tuples is worth looking: http://javatuple.com/
Camilo Díaz
+4  A: 

If you need to do this frequently enough, then create a class just to hold the result and return that instead.

This also allows you to DOCUMENT what your function returns in the same way that the parameters are documented.

Thorbjørn Ravn Andersen
A: 

On way to handle complex return behaviour is to pass an interface which the method calls. e.g.

public interface Results<R> {
    public void processing(String stage);
    public void success(String mesg, R result);
    public void successes(String mesg, List<R> result);
    public void thrown(Throwable t);
}

public void process(Results<R> results, String text, List<String> data);
Peter Lawrey
+1  A: 

Javascript, fortunately for you, is a dynamic language. This means you can construct any kind of object you want and return it. This effectively meets your requirement of having a "parameterized" return value, albeit in a rather un-typesafe way.

For example:

function stuff() {
 return {
  "foo": "a",
  "bar": "b"
 }
}

var theStuff = stuff();
alert(theStuff.foo + theStuff.bar); // should output "ab"
davogones
A: 

output parameters anyone??

ctrlalt3nd