Last update October 18, 2007

# Feature Request List / Multiple Return Values

rough spillage of thoughts: will need cleanup

Problem

functions traditionally return only one value

Historic Solutions

• create a custom struct, populate the struct in the function
• use pointers to modify "input" variables directly (now formalised in D with the out keyword)
Background Theory

Ideally a function would look and behave a little like an operator so a basic operation would look like:

1.

```   a = b

```
and the equivalent function:

2.

```  a = f(b)

```
However multiple return values in either of the historic solutions are represented in a non intuitive fashion, example:

3.

```  int a;
someFunction( a , b , c , d );
someOtherFunction( a );

```
without reading the source, following pointer trails and consulting documentation it is not immediately obvious what is happening. Here a is being modified by someFunction before being processed by someOtherFunction. This example, with one assignment could have been written as example 2, above, but with multiple values affected this becomes problematic

Proposed Solution

By delimiting multiple variables within brackets one can create an inline implicitly typed struct. In my examples square brackets and commas are used but other combinations could be considered;

example of simple multiple assignment:

4.

```   int a,b;
[a,b] = someFunction(x,y);

```
the declaration of someFunction would be as follows:

5.

```   [int,int] someFunction( int x , int y )
{
int c,d;
...
return [c,d];
}

```
For examples 4. and 5. to peacefully coexist the grouped datatypes assigned and returned need to match. The compiler on encountering such a block would create an implicit struct in place of each (here of the form struct{ int a , int b } and struct{ int c , int d } and assume the responsibility of cross-copying the values.

Types could therefore also be mixed:

6.

```   [string, int, char] getUserInfo( int iUserID )
{
...
return [ sUserName , iUserAge , cUserGender ];
}

```
Essentially this approach formalises the temporary-struct approach to multiple return values, but with the compiler taking the legwork out of the process.

If a return value was to be ignored for whatever reason the compiler could respect that and not throw an error, example:

7.

```   [ sName , , cGender ] = getUserInfo( iID );

```
Here the programmer is not interested in the age field, an implied NULL is placed between the commas, alternativly and for clarity the NULL could be required and not assumed by the compiler.

(Add : Or maybe, for better clarity, use an OCaml construct like [sName, _, cGender] We could also think of aliasing names for the return, so we could just say : [Name -> sName, Gender -> cGender] )

Muliple assignments can also be made possible, this could therefore also meet the requests for a swap operator:

8.

```   [x,y] = [y,x];

```
Again, the compiler would do the hard work behind the scenes, leaving the programmer free to concentrate on their own creativity.

Javascript 1.7 has an interesting take on multiple return values and assignments as described in the final section here: http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7

Though it does extensively overload the square brackets as per my own examples and it's loosely-typed nature allows these to be cross-treated as arrays

| | | | | | | | | |

(date of last change: October 18, 2007 22:15 )