1

I am curious to know about this.

whenever I write a function which have to return multiple values, either I have to use pass by reference or create an array store values in it and pass them.

Why all the Object Orinented languages functions are not allowed to return multiple parameters as we pass them as input. Like is there anything inbuilt structure of the language which is restricting from doing this.

Dont you think it will be fun and easy if we are allowed to do so.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
rkb
  • 10,933
  • 22
  • 76
  • 103

7 Answers7

4

It's not true that all Object-Oriented languages follow this paradigm.

e.g. in Python (from here):

def quadcube (x):
    return x**2, x**3

a, b = quadcube(3)

a will be 9 and b will be 27.

Cowan
  • 37,227
  • 11
  • 66
  • 65
  • 2
    Is this really returning two integers? I was under the impression this returns a single object, a tuple (9,27), that gets automatically unpacked when assigned to 'a, b'. – Stephen Simmons Jul 30 '09 at 23:44
  • What is the effective difference in this case between returning multiple objects and returning a tuple? – Chuck Jul 30 '09 at 23:47
  • i don't think that will work in C++ since the language isn't designed that way. Python is designed to be interpreted while C++ is compiled. – Daniel A. White Jul 30 '09 at 23:48
  • @rkbang: This does not work in C++. Due to how the C comma operator works, this will merely return x**3. – Chuck Jul 30 '09 at 23:49
  • @Chuck the difference is that returning tuple will still be returning a single value - a pointer to the tuple. – Daniel A. White Jul 30 '09 at 23:49
  • 2
    I guess that's true, simmo, but as Chuck points out, is there any difference? I was looking at the Wikipedia article on Arity for another question, and as it points out for argument counts, any n-ary function can be looked at as a unary function with a complex input type. I guess the same replies for the return type -- they're basically equivalent. – Cowan Jul 30 '09 at 23:51
  • @Cowan, interesting - i guess its a bit of set theory with parameters. i like this! – Daniel A. White Jul 30 '09 at 23:53
  • @Daniel yes, I guess you can look at it as a multi-value-returning function being a tuple-returning function with convenient unpacking syntax, or a tuple-returning method being a multiple-value-returning method which gives you a convenient syntax to get a pointer to the aggregate set of results.... then your head explodes (well, mine does) – Cowan Jul 31 '09 at 00:00
  • Well I'm still finishing up my computer science degree so I think in terms of pointers a lot. – Daniel A. White Jul 31 '09 at 00:17
  • @Daniel, since Python hides that (possible) tuple from the user, it is a distinction without a difference, or possibly just an implementation detail. From the programmer's point of view, `quadcube` did return two values which were assigned to two variables. Lua does something very similar to the user, but internally it holds all values on a stack and the implementation tells the core how many elements at the top of the stack are return values so there are no tuples involved at all. – RBerteig Jul 31 '09 at 00:58
  • Price is the difference: instantiating a tuple on the fly costs more in terms of processor time and memory than would be used if it were possible to return multiple values into multiple return value registers. – intuited Jun 27 '10 at 15:03
3

The difference between the traditional

OutTypeA SomeFunction(out OutTypeB, TypeC someOtherInputParam)

and your

{ OutTypeA, OutTypeB } SomeFunction(TypeC someOtherInputParam)

is just syntactic sugar. Also, the tradition of returning one single parameter type allows writing in the easy readable natural language of result = SomeFunction(...). It's just convenience and ease of use.

And yes, as others said, you have tuples in some languages.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
2

This is likely because of the way processors have been designed and hence carried over to modern languages such as Java or C#. The processor can load multiple things (pointers) into parameter registers but only has one return value register that holds a pointer.

I do agree that not all OOP languages only support returning one value, but for the ones that "apparently" do, this I think is the reason why.

Also for returning a tuple, pair or struct for that matter in C/C++, essentially, the compiler is returning a pointer to that object.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1

First answer: They don't. many OOP languages allow you to return a tuple. This is true for instance in python, in C++ you have pair<> and in C++0x a fully fledged tuple<> is in TR1.

Second answer: Because that's the way it should be. A method should be short and do only one thing and thus can be argued, only need to return one thing.

shoosh
  • 76,898
  • 55
  • 205
  • 325
0

In PHP, it is like that because the only way you can receive a value is by assigning the function to a variable (or putting it in place of a variable). Although I know array_map allows you to do return something & something;

Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
0

To return multiple parameters, you return an single object that contains both of those parameters.

public MyResult GetResult(x)
{
    return new MyResult { Squared = Math.Pow(x,2), Cubed = Math.Pow(x,3) };
}

For some languages you can create anonymous types on the fly. For others you have to specify a return object as a concrete class. One observation with OO is you do end up with a lot of little classes.

The syntactic niceties of python (see @Cowan's answer) are up to the language designer. The compiler / runtime could creating an anonymous class to hold the result for you, even in a strongly typed environment like the .net CLR.

Yes it can be easier to read in some circumstances, and yes it would be nice. However, if you read Eric Lippert's blog, you'll often read dialogue's and hear him go on about how there are many nice features that could be implemented, but there's a lot of effort that goes into every feature, and some things just don't make the cut because in the end they can't be justified.

Robert Paulson
  • 17,603
  • 5
  • 34
  • 53
  • Also in C# there is a System.Collections.Generic.KeyValuePair, and .Net 4 is supposed to have Tuple<> generics. See SO question http://stackoverflow.com/questions/152019/tuples-in-c – Robert Paulson Jul 31 '09 at 00:03
0

It's not a restriction, it is just the architecture of the Object Oriented and Structured programming paradigms. I don't know if it would be more fun if functions returned more than one value, but it would be sure more messy and complicated. I think the designers of the above programming paradigms thought about it, and they probably had good reasons not to implement that "feature" -it is unnecessary, since you can already return multiple values by packing them in some kind of collection. Programming languages are designed to be compact, so usually unnecessary features are not implemented.