Why methods cannot return multiple values

Curiously, is there any technical restriction if there are multiple return values ​​for methods in languages ​​such as java, c, C ++ or a restriction, only by specification? In assembly language, I understand that a call can set a single value for registration.

+5
source share
10 answers
  • Because in days C there is / was the only register used to store the return value.
  • Because if you need more values, you can just return a structlink (in Java / C #) or a pointer.
  • Since you can use the out parameter.

, . . ( , ++ tuple ( TR1, ++ 11 boost), )

+19

, return. . !

struct Person
{
   std::string Name;
   int Age;
   std::string Qualification;
   //...
};

Person GetInfo()
{
    Person person;
    //fill person members ...
    return person;
}

std::pair, std::vector, std::map, std::list . ++ 0x std::tuple.

+9

Genie , . , . , , , ()... , .: -)

: -)

+6

, . , , :

(int, int, int) call(int x, int y, int z);

:

(a, b, c) = call(1, 2, 3);

. , . , , .

, return:

(int, int, int) call(int x, int y, int z);
{
  return x+1, y+1, z+1
}

, :

(err, filehandle) = OpenFileDialog(...)

, . . : , .

+3

. , , , , , .

2 .

+2

, . , C . , , ML Haskell, , .

:

, , , , ML Haskell - "" . , , , , .

, , , "" , . - , (c.f call-with-values), - MATLAB:

function [x,y] = myFunc(a, b)
   ...
end

[p, q] = myFunc(3,4)

, (cons cell, array, ) .

+2

, / ABI. , . - , .

+1

, ++, :

std::tuple<int,float> func()
{
    return std::make_tuple(1, 2.f);
}

int i;
float f;

std::tie(i, f) = func();

.

0

.

, .

second - ( const) .

0

, , , , C Java .

++ , , , Ruby python - , , return, , , -.

, ++ 17 (. ) ...

auto [x,y,z] = expression;

... return - , - , a tuple struct public. const, const.

This function also documents this ...

for (const auto& [key,value] : mymap)
    ...

... that avoids the reuse of less expressive ->firstand ->second.

When moving C ++ in this direction, it is likely that C and other C-language languages ​​will look closely to do the same.

0
source

All Articles