views:

254

answers:

8

Hello all,

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.

A: 

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;

Chacha102
+2  A: 

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
Yea but why cant it use multiple parameters instead of assigning multiple values to same parameter.
rkb
What do you mean by multiple values/parameters?
Daniel A. White
I can explain in an made up machine and a C-like language, if you would like.
Daniel A. White
+4  A: 

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
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'.
hey this is even new to me, does this works in c++ also.
rkb
What is the effective difference in this case between returning multiple objects and returning a tuple?
Chuck
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
@rkbang: This does not work in C++. Due to how the C comma operator works, this will merely return x**3.
Chuck
@Chuck the difference is that returning tuple will still be returning a single value - a pointer to the tuple.
Daniel A. White
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
@Cowan, interesting - i guess its a bit of set theory with parameters. i like this!
Daniel A. White
@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
Well I'm still finishing up my computer science degree so I think in terms of pointers a lot.
Daniel A. White
@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
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
+1  A: 

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
But returning a tuple/pair in c++ is returning a pointer to the value, not the values themselves.
Daniel A. White
Not true. returning a pointer to `pair<>` makes no sense. You always return it by value.
shoosh
+3  A: 

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
A: 

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
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
A: 

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.

+1  A: 

It's not really specific to object-oriented languages since:

  • some OO languages allow multiple return values, such as Python.
  • some non-OO languages only allow a single return value, such as C.

The question is more about static vs. dynamic typing. In a statically typed language, a function can only return one value because that is its static return type. Note that this return type can actually be a container of multiple values, such as an array, but it's still one object.

By contrast, a Python method can return a different number of values, because its return type is determined at run-time. So for instance you can have:

def foo(a):
    if a > 10:
        return 0
    else:
        return 1, False

number, boolean = foo(10)
print num, boolean

...which doesn't make any practical sense, but demonstrate that a function can have a varying number of return values. When you look under the hood, however, this turns out to be just a syntactic shortcut: what actually happens when you return multiple values is that Python creates a tuple on-the-fly and returns that single object. So the following code is also valid:

returnValues = foo(10)
print returnValues[0], returnValues[1]

As it turns out, in a sense, a function always has one return value, even in Python if you look at how it's implemented. That's because the concept of a function originates in mathematics, where a function is a mapping between values of a certain set and values of another set. However, dynamically typed languages somehow bypass this limitation by allowing you to write code as if a function returned multiple values.

Dr_Asik
I was also going to tell about the concept of a function. It's simply more natural to write `func(another(x))` rather than `func(another(x)[0])[0]` which is what you would have to write if functions always returned multiple values. Too bad this question seems abandoned.
ilya n.