Just curious is there any technical limitation in having multiple return values for methods in languages like java, c, c++ or limitation is just by spec? In assembly language I understand callee can pop one value to register.
-
2This is a nonsense. A function can return multiple values (as an example see Nawaz's answer) – BЈовић Jun 23 '11 at 18:29
-
Related: http://stackoverflow.com/questions/321068/returning-multiple-values-from-a-c-function – Kirill V. Lyadvinsky Jun 23 '11 at 18:33
-
Though its more philosophical question, value means an instance of object may it be struct or object. touple is good example I was not aware of. [out of certain boundary everything looks nonsense] – Dileep Jun 23 '11 at 18:35
-
3@VJo: To be pedant, that's still just a single value. It just happens to be aggregate. – GManNickG Jun 23 '11 at 18:38
-
@GMan: +1. The point at which the question invoked registers is the point at which this clearly became a philosophical question and not a "how do I?" one. – Edward Thomson Jun 23 '11 at 19:17
10 Answers
- Because in the days of C there is/was a single register used to hold the return value.
- Because if you need more values, you can just return a
struct
, reference (in Java/C#), or pointer. - Because you can use an out parameter.
Allowing multiple return values would add complexity, and it's simply worked around. There's no reason for it to be there. (Indeed, in C++ you can return a tuple
(from TR1, C++11, or boost) which effectively is multiple return values)

- 104,103
- 58
- 317
- 552
-
"there is/was a single register" - on these grounds we should count ourselves lucky we can return a `long` by value ;-) – Steve Jessop Jun 23 '11 at 18:49
-
@Steve: I don't think `long long` existed in "ye olden days" of C. (A struct is certainly larger than a register and I think you can return those in C89, though you could not in K&R) – Billy ONeal Jun 23 '11 at 18:50
-
I changed my comment from `long long` to `long`. I was dithering because on the one hand, as you say `long long` is new. On the other hand, these days `long` is rarely bigger than a register anyway, so it's a bit non-obvious why it's such a grand concession :-) – Steve Jessop Jun 23 '11 at 18:57
Its by design, because there is no need to allow multiple values in return statement. You can always define a struct with all the needed members, and create an instance of the struct and return it. Simple!
Example,
struct Person
{
std::string Name;
int Age;
std::string Qualification;
//...
};
Person GetInfo()
{
Person person;
//fill person's members ...
return person;
}
You can use std::pair
, std::vector
, std::map
, std::list
and so on. In C++0x, you can use std::tuple
as well.

- 353,942
- 115
- 666
- 851
-
-
2Yes, but you still have a function that returns a single value even though that single value happens to be a structure. – Edward Thomson Jun 23 '11 at 18:39
If the Genie gave you only one wish, you could just wish to have any number of wishes. It's the same with just one return value from a method. You can use your return value as a pointer to an address where an object full of attributes resides and then query those attributes (properties)... This way there really is no limitation. :-)
Fun coding and many happy returns :-)

- 20,434
- 21
- 120
- 152
It's just a decision and because people are used to it. In principle there wouldn't be anything preventing a language designer from implementing a syntax like this:
(int, int, int) call(int x, int y, int z);
and a function call could look like this:
(a, b, c) = call(1, 2, 3);
or whatever syntax they would choose for this task. Though one could discuss if it would add to readability. And as others have pointed out, some languages implement this by tuples or similar constructs.
Sure, the return statement:
(int, int, int) call(int x, int y, int z);
{
return x+1, y+1, z+1
}
You could even think of useful applications like:
(err, filehandle) = OpenFileDialog(...)
where the function can return either a detailed error code or a valid file handle. Though exceptions take this place nowadays. But exceptions are in some sense a way to return at least two alternating values, either the requested function return value or the raised exception.

- 5,621
- 1
- 22
- 30
-
-
3Python, for example, uses very similar syntax to this to disguise functions that return tuples, so they appear to return multiple values. And in the case of Python, you don't have to declare what a function returns, so you avoid the scary-looking `(int, int, int)`. – Steve Jessop Jun 23 '11 at 18:54
-
Maybe I'm just not very good at this whole programming thing, but that just seems confusing. :) – Jay Jun 23 '11 at 18:54
-
@Steve: Thanks I'm not good enough in Python, though i remembered somehow that it was possible. I must admit that I actually chose C++ like syntax, since it looks funnier. We could add an arbitrary amount of confusion by allowing for named return values. – thorsten müller Jun 23 '11 at 18:59
-
@Jay: wait 'til you see what happens in Python when you want to ignore one of three return values. `_` is a valid variable name, and variables don't have to be pre-declared, so the idiom is `a, _, c = call(1,2,3)`! For example `prefix, _, suffix = "foo-bar".partition("-")`. Now `prefix` is `"foo"`, `suffix` is `"bar"`, and `_` is `"-"`, but we aren't planning to use that. – Steve Jessop Jun 23 '11 at 19:00
Because good programming languages encourage programmers to do the right thing. If a method needs to return multiple values, those values probably are related, and thus should be group together in something like a struc.
Just my 2 cents.

- 6,224
- 4
- 20
- 23
-
4Neither C nor C++ are designed to "encourage programmers to do the right thing" – Billy ONeal Jun 23 '11 at 18:36
-
2Also, C++ returns a `pair` from some standard library functions, notably `map::insert`. This doesn't group them together in any way that means anything, it's just the easiest hack available to achieve a goal that would more neatly be described as, "returning two values". So if C++ is trying to tell us to group things meaningfully, it hasn't set an ideal example. – Steve Jessop Jun 23 '11 at 18:52
-
LOL. I never said C or C++ were _good_ programming languages. However _good_ is a relative term. – Jay Jun 23 '11 at 18:52
-
C and C++ were two of the languages in the question. And for the record, I disagree with your assertion that the programming language is responsible to encourage programmers to do the right thing -- an idiot is an idiot no matter what programming language (s)he is working in. – Billy ONeal Jun 23 '11 at 19:03
It's mostly due to historical reasons having to do with machine calling conventions. Also because C doesn't have a pattern matching syntax on the callee side to retrieve the results. Note that languages like ML or Haskell have a syntactically lightweight tuple type that is perfectly usable for returning multiple values.
Edited:
Actually thinking about it a little bit, I guess if you wanted to split hairs, ML and Haskell still have a "single" return value. It's just that tuples are so lightweight syntactically that it's convenient to think about functions returning multiple values rather than a single tuple.
To be totally rigorous, there are two languages that I can think of that have "proper" multiple-values returns that are not just tuples in some shape. One is Scheme, (c.f call-with-values
), and the other is MATLAB:
function [x,y] = myFunc(a, b)
...
end
[p, q] = myFunc(3,4)
In both of these languages, there is a special syntactic distinction between a single value that happens to be an aggregate (cons cell, array, respectively) and multiple values.

- 12,465
- 1
- 25
- 33
It's just a decision made by the language and/or ABI designers. No more, no less. In assembly language, you can make those decisions yourself, though - I'm not sure what your last comment means.

- 219,201
- 40
- 422
- 469
It is useful to support this, and given how people find it convenient in other languages, C and Java may move that way too.
C++ is already standardising on the kind of convenient, intuitive handling of return values familiar from e.g. Ruby and python for the function-caller side, which is more important than at the return
itself, because a single function is likely called from a great many call sites.
Specifically, the C++17 paper here (see also wording here) documents a notation...
auto [x,y,z] = expression;
...where expression can be a function returning - or any other expression evaluating to - an array, a tuple
, or a struct
with all public
members. The above can be preceded by const
to make the local variables const
.
The feature also documents this...
for (const auto& [key,value] : mymap)
...
...which avoids repeated use of the less-expressive ->first
and ->second
.
With C++ moving in this direction, it's likely C and other C-derived languages will look carefully at doing likewise.

- 102,968
- 15
- 177
- 252
We don't need the ability to return multiple values built into the C++ language, because the library works just fine:
std::tuple<int,float> func()
{
return std::make_tuple(1, 2.f);
}
int i;
float f;
std::tie(i, f) = func();
Most other languages have similar functionality in their standard library.

- 29,236
- 5
- 72
- 110
-
This isn't really the "Standard" library yet -- at least until C++0x is ratified. – Billy ONeal Jun 23 '11 at 18:37
Actually, there are at least 2 ways to return multiple values.
First is to return create a struct or class, put all the return data, and return it.
second is to pass parameters by reference (non-const) and put the values in there.

- 48,127
- 24
- 147
- 185