Return multiple values

From Rosetta Code
Task
Return multiple values
You are encouraged to solve this task according to the task description, using any language you may know.

Show how to return more than one value from a function.

AutoHotkey

Works with: AutoHotkey_L

Functions may return one value. The conventional way to return multiple values is to bundle them into an Array. <lang AutoHotkey>addsub(x, y) {

 return [x + y, x - y]

}</lang>

C

C has structures and unions which can hold multiple data element of varying types. Unions also use the same memory space for storing different types of data. That's why for the union, the num comes out as 67 instead of 99. 67 is the value of 'C' in ASCII. <lang c>

  1. include<stdio.h>

typedef struct{ int integer; float decimal; char letter; char string[100]; double bigDecimal; }Composite;

typedef union{ int num; char letter; }Zip;

Composite example() { Composite C = {1,2.3,'a',"Hello World",45.678}; return C; }

Zip example2() { Zip r; r.num = 99; r.letter = 'C'; return r; }


int main() { Composite C = example(); Zip rar = example2();

printf("Values from a function returning a structure : { %d, %f, %c, %s, %f}",C.integer, C.decimal,C.letter,C.string,C.bigDecimal); printf("\n\n\nValues from a function returning a union : { %d, %c}",rar.num,rar.letter);

return 0; } </lang> Output: <lang> Values from a function returning a structure : { 1, 2.300000, a, Hello World, 45.678000}


Values from a function returning a union : { 67, C} </lang>

C++

The new 2011 C++ standard includes tuples, which allow a number of different values of even different types to be passed around. <lang cpp>#include <algorithm>

  1. include <iostream>
  2. include <tr1/tuple>

std::tr1::tuple<const int , const int> minmax ( const int * numbers , const int num ) {

  const int *maximum = std::max_element ( numbers , numbers + num ) ;
  const int *minimum = std::min_element ( numbers , numbers + num ) ;
  std::tr1::tuple<const int , const int> result( *maximum , *minimum ) ;
  return result ;

}

int main( ) {

  const int numbers[ ] = { 17 , 88 , 9 , 33 , 4 , 987 , -10 , 2 } ;
  int numbersize = sizeof( numbers ) / sizeof ( int ) ;
  std::tr1::tuple<const int , const int> result = minmax( numbers , numbersize ) ;
  std::cout << "The greatest number is " << std::tr1::get<0>( result ) 
     << " , the smallest " << std::tr1::get<1>( result ) << " !\n" ;
  return 0 ;

}</lang> Output:

The greatest number is 987 , the smallest -10 !

C#

<lang c sharp>using System; using System.Collections.Generic; using System.Linq;

namespace ReturnMultipleValues {

   internal class Program
   {
      static void Main()
      {
          var minMax = MinMaxNum(new[] {4, 51, 1, -3, 3, 6, 8, 26, 2, 4});
          int min = minMax.Item1;
          int max = minMax.Item2;
          Console.WriteLine("Min: {0}\nMax: {1}", min, max);
      }
       static Tuple<int,int> MinMaxNum(IEnumerable<int> nums)
       {
           var sortedNums = nums.OrderBy(num => num).ToArray();
           return new Tuple<int, int>(sortedNums.First(), sortedNums.Last());
       }
   }

}</lang>

Output

Min: -3
Max: 51

Common Lisp

Besides the obvious method of passing around a list, Common Lisp also allows a function to return multiple values. When citing the return values, if no interest is shown for multiple values, only the first (the primary return value) is used. Multiple values are not a data structure such as a tuple, list or array. They are a true mechanism for returning multiple values.

Returning a single value is accomplished by evaluating an expression (which itself yields a single value) at the end of a body of forms.

<lang lisp>(defun return-three ()

 3)</lang>

The next possibility is that of returning no values at all. For this, the values function is used, with no arguments:

<lang lisp>(defun return-nothing ()

 (values))</lang>

To combine the values of multiple expressions into a multi-value return, values is used with arguments. The following is from an interactive CLISP session. CLISP's listener shows multiple values separated by a semicolon:

<lang lisp>[1]> (defun add-sub (x y) (values-list (list (+ x y) (- x y)))) ADD-SUB [2]> (add-sub 4 2)  ; 6 (primary) and 2 6 ; 2 [3]> (add-sub 3 1)  ; 4 (primary) and 2 4 ; 2 [4]> (+ (add-sub 4 2) (add-sub 3 1))  ; 6 + 4 10 [5]> (multiple-value-call #'+ (add-sub 4 2) (add-sub 3 1)) ; 6+2+4+2 14</lang>

What happens if something tries to use the value of a form which returned (values)? In this case the behavior defaults to taking the value nil:

<lang lisp>(car (values)) ;; no error: same as (car nil)</lang>

What if the values function is applied to some expressions which also yield multiple values, or which do not yield any values? The answer is that only the primary value is taken from each expression, or the value nil for any expression which did not yield a value:

<lang lisp>(values (values 1 2 3) (values) 'a)</lang>

yields three values:

-> 1; NIL; A

This also means that values can be used to reduce a multiple value to a single value:

<lang lisp>;; return exactly one value, no matter how many expr returns,

nil if expr returns no values

(values expr)</lang>

Multiple values are extracted in several ways.

1. Binding to variables:

<lang lisp>(multiple-value-bind (dividend remainder) (truncate 16 3)

 ;; in this scope dividend is 5; remainder is 1
 )</lang>

2. Conversion to a list:

<lang lisp>(multiple-value-list (truncate 16 3)) ;; yields (5 1)</lang>

3. Reification of multiple values as arguments to another function:

<lang lisp>;; pass arguments 5 1 to +, resulting in 6: (multiple-value-call #'+ (truncate 16 3))</lang>

4. Assignment to variables: <lang lisp>;; assign 5 to dividend, 1 to remainder: (multiple-value-setq (dividend remainder) (truncate 16 1))</lang> (values ...) syntax is treated as a multiple value place by setf and other operators, allowing the above to be expressed this way: <lang lisp>(setf (values dividend remainder) (truncate 16 1))</lang>

D

<lang d>import std.stdio, std.typecons;

auto addSub(T)(T x, T y) {

   return tuple(x + y, x - y);

}

void main() {

   auto r = addSub(33, 12);
   writefln("33 + 12 = %d\n33 - 12 = %d", r.tupleof);

}</lang> Output:

33 + 12 = 45
33 - 12 = 21

This pull request:

https://github.com/D-Programming-Language/dmd/pull/341

if accepted will allow nice tuple destructuring code like: <lang d>(auto m1, m2) = addSub(33, 12);</lang>


Delphi

Delphi functions return a single value, but var parameters of a function or procedure can be modified and act as return values.

<lang Delphi>program ReturnMultipleValues;

{$APPTYPE CONSOLE}

procedure GetTwoValues(var aParam1, aParam2: Integer); begin

 aParam1 := 100;
 aParam2 := 200;

end;

var

 x, y: Integer;

begin

 GetTwoValues(x, y);
 Writeln(x);
 Writeln(y);

end.</lang>

Factor

With stack-oriented languages like Factor, a function returns multiple values by pushing them on the data stack. For example, this word */ pushes both x*y and x/y.

<lang factor>USING: io kernel math prettyprint ; IN: script

*/ ( x y -- x*y x/y )
   [ * ] [ / ] 2bi ;

15 3 */

[ "15 * 3 = " write . ] [ "15 / 3 = " write . ] bi*</lang>

Its stack effect declares that */ always returns 2 values. To return a variable number of values, a word must bundle those values into a sequence (perhaps an array or vector). For example, factors (defined in math.primes.factors and demonstrated at Prime decomposition#Factor) returns a sequence of prime factors.

Forth

It is natural to return multiple values on the parameter stack. Many built-in operators and functions do so as well (/mod, open-file, etc.). <lang forth>: muldiv ( a b -- a*b a/b )

 2dup / >r * r> ;</lang>

Go

Functions can return multiple values in Go:

<lang go>func addsub(x, y int) (int, int) {

 return x + y, x - y

}</lang>

Or equivalently using named return style:

<lang go>func addsub(x, y int) (sum, difference int) {

 sum = x + y
 difference = x - y
 return

}</lang>

When a function returns multiple values, you must assign to a comma-separated list of targets:

<lang go>sum, difference := addsub(33, 12) fmt.Printf("33 + 12 = %d\n", sum) fmt.Printf("33 - 12 = %d\n", difference)</lang>

Haskell

Every function returns one value. The conventional way to return multiple values is to return a tuple.

<lang haskell>addsub x y =

 (x + y, x - y)</lang>

You can use pattern matching to extract the components:

<lang haskell>main = do

 let sum, difference = addsub 33 12
 putStrLn $ "33 + 12 = " ++ show sum
 putStrLn $ "33 - 12 = " ++ show difference</lang>

Icon and Unicon

Icon and Unicon values range from simple atomic values like integers and strings to structures like lists, tables, sets, records. The contents of structures are heterogeneous and any of them could be used to return multiple values all at once. Additionally, generators are supported that return multiple results one at a time as needed.

The following examples return 1, 2, 3 in different ways:

<lang Icon>procedure retList() # returns as ordered list return [1,2,3] end

procedure retSet() # returns as un-ordered list insert(S := set(),3,1,2) return S end

procedure retLazy() # return as a generator suspend 1|2|3 end

procedure retTable() # return as a table T := table() T["A"] := 1 T["B"] := 2 T["C"] := 3 return T end

record retdata(a,b,c)

procedure retRecord() # return as a record, least general method return retdata(1,2,3) end</lang>

J

To return multiple values in J, you return an array which contains multiple values. Since the only data type in J is array, this is sort of like asking how to return only one value in another language.

<lang j> 1 2+3 4 4 6</lang>

Liberty BASIC

Using a space-delimited string to hold the array. LB functions return only one numeric or string value, so the function returns a string from which can be separated the two desired values. <lang lb> data$ ="5 6 7 22 9 3 4 8 7 6 3 -5 2 1 8 9"

a$ =minMax$( data$) print " Minimum was "; word$( a$, 1, " "); " & maximum was "; word$( a$, 2, " ")

end

function minMax$( i$) min = 1E6 max =-1E6 i =1 do

   t$    =word$( i$, i, " ")
   if t$ ="" then exit do
   v     =val( t$)
   min   =min( min, v)
   max   =max( max, v)
   i =i +1

loop until 0 minMax$ =str$( min) +" " +str$( max) end function </lang>

 Minimum was -5 & maximum was 22


OCaml

Every function returns one value. The conventional way to return multiple values is to return a tuple.

<lang ocaml>let addsub x y =

 x + y, x - y</lang>

(Note that parentheses are not necessary for a tuple literal in OCaml.)

You can use pattern matching to extract the components:

<lang ocaml>let sum, difference = addsub 33 12 in

 Printf.printf "33 + 12 = %d\n" sum;
 Printf.printf "33 - 12 = %d\n" difference</lang>

Space safety of tuples

The OCaml programmer should be aware that when multiple values are returned with a tuple, the finalisation does not handle each values independently, but handles the tuple as a whole. So all the values are only finalised when all the values are not reachable anymore.

There is also an explanation of this behaviour in the module Space_safe_tuple of the Jane Street's core library.

<lang ocaml>let pair a b =

 let ra = Array.create 1 a
 and rb = Array.create 1 b in
 let f r = Printf.printf "> finalised: %d\n%!" r.(0) in
 Gc.finalise f ra;
 Gc.finalise f rb;
 (ra, rb)

let () =

 let a, b = pair 1 2 in
 let c, d = pair 3 4 in
 Gc.full_major ();  (* garbage collection *)
 Printf.printf "Used: %d\n%!" a.(0)</lang>

Here we see that b is not finalised even if it is not used after the garbage collection:

$ ocamlopt -w y -o pair.opt pair.ml
$ ./pair.opt 
> finalised: 4
> finalised: 3
Used: 1

The workaround is to explicitly access to each value with functions like fst and snd that return only one element of the tuple:

<lang ocaml>val fst : 'a * 'b -> 'a val snd : 'a * 'b -> 'b</lang>

<lang ocaml>let pair a b =

 let ra = Array.create 1 a
 and rb = Array.create 1 b in
 let f r = Printf.printf "> finalised: %d\n%!" r.(0) in
 Gc.finalise f ra;
 Gc.finalise f rb;
 (ra, rb)

let () =

 let ab = pair 1 2 in
 let a = fst ab
 and b = snd ab in
 let c, d = pair 3 4 in
 Gc.full_major ();  (* garbage collection *)
 Printf.printf "Used: %d\n%!" a.(0)</lang>

Now we see that b is finalised:

$ ocamlopt -w y -o pair2.opt pair2.ml
$ ./pair2.opt 
> finalised: 4
> finalised: 3
> finalised: 2
Used: 1

PARI/GP

The usual way to return multiple values is to put them in a vector: <lang parigp>foo(x)={

 [x^2, x^3]

};</lang>

Pascal

See Delphi

Perl

Functions may return lists of values:

<lang perl>sub foo {

   my ($a, $b) = @_;
   return $a + $b, $a * %b;

}</lang>

PHP

Every function returns one value. The conventional way to return multiple values is to bundle them into an array.

<lang php>function addsub($x, $y) {

 return array($x + $y, $x - $y);

}</lang>

You can use the list() construct to assign to multiple variables:

<lang php>list($sum, $difference) = addsub(33, 12); echo "33 + 12 = $sum\n"; echo "33 - 12 = $difference\n";</lang>

PicoLisp

A PicoLisp function returns a single value. For multiple return values, a cons pair or a list may be used. <lang PicoLisp>(de addsub (X Y)

  (list (+ X Y) (- X Y)) )</lang>

Test: <lang PicoLisp>: (addsub 4 2) -> (6 2)

(addsub 3 1)

-> (4 2)

(+ (car (addsub 4 2)) (car (addsub 3 1)))

-> 10

(sum + (addsub 4 2) (addsub 3 1))

-> 14</lang>

Pike

multiple values are returned through an array. an array can be assigned to separate variables. <lang Pike>array(int) addsub(int x, int y) {

   return ({ x+y, x-y });

}

[int z, int w] = addsub(5,4);</lang>

PL/I

Example 1 illustrates a function that returns an array: <lang PL/I>

  define structure 1 h,
                     2 a (10) float;
  declare i fixed binary;

sub: procedure (a, b) returns (type(h));

  declare (a, b) float;
  declare p type (h);
  do i = 1 to 10;
     p.a(i) = i;
  end;
  return (p);

end sub; </lang> Example 2 illustrates a function that returns a general data structure:

   define structure 1 customer,
                      2 name,
                        3 surname character (20),
                        3 given_name character (10),
                      2 address,
                        3 street character (20),
                        3 suburb character (20),
                        3 zip fixed decimal (7);

sub2: procedure() returns (type(customer));
   declare c type (customer);
   get edit (c.surname, c.given_name) (L);
   get edit (c.street, c.suburb, c.zip) (L);
   return (c);
end sub2;

Example 3 illustrates the return of two values as a complex value:

comp: procedure(a, b) returns (complex);
   declare (a, b) float;

   return (complex(a, b) );
end comp;

Python

Every function returns one value. The conventional way to return multiple values is to bundle them into a tuple.

<lang python>def addsub(x, y):

 return x + y, x - y</lang>

(Note that parentheses are not necessary for a tuple literal in Python.)

You can assign to a comma-separated list of targets:

<lang python>sum, difference = addsub(33, 12) print "33 + 12 = %s" % sum print "33 - 12 = %s" % difference</lang>

There is no discernable difference between "returning multiple values" and returning a single tuple of multiple values. It is just a more pedantic/accurate statement of the mechanism employed.

Retro

Functions take and return values via a stack. This makes returning multiple values easy.

<lang Retro>: addSubtract ( xy-nm )

 2over - [ + ] dip ;

</lang>

Ruby

Every function returns one value. The conventional way to return multiple values is to bundle them into an Array.

Use an array literal:

<lang ruby>def addsub(x, y)

 [x + y, x - y]

end</lang>

Or use return with 2 or more values:

<lang ruby>def addsub(x, y)

 return x + y, x - y

end</lang>

(With at least 2 values, return makes a new Array. With 1 value, return passes the value, without making any Array. With 0 values, return passes nil.)

Assignment can split the Array into separate variables.

<lang ruby>sum, difference = addsub(33, 12) puts "33 + 12 = #{sum}" puts "33 - 12 = #{difference}"</lang>

Scheme

Scheme can return multiple values using the values function, which uses continuations: <lang scheme>(define (addsub x y)

 (values (+ x y) (- x y)))</lang>

You can use the multiple values using the call-with-values function: <lang scheme>(call-with-values

 (lambda () (addsub 33 12))
 (lambda (sum difference)
   (display "33 + 12 = ") (display sum) (newline)
   (display "33 - 12 = ") (display difference) (newline)))</lang>

The syntax is kinda awkward. SRFI 8 introduces a receive construct to make this simpler: <lang scheme>(receive (sum difference) (addsub 33 12)

 ; in this scope you can use sum and difference
 (display "33 + 12 = ") (display sum) (newline)
 (display "33 - 12 = ") (display difference) (newline))</lang>

SRFI 11 introduces a let-values construct to make this simpler: <lang scheme>(let-values (((sum difference) (addsub 33 12)))

 ; in this scope you can use sum and difference
 (display "33 + 12 = ") (display sum) (newline)
 (display "33 - 12 = ") (display difference) (newline))</lang>

Standard ML

Every function returns one value. The conventional way to return multiple values is to return a tuple.

<lang sml>fun addsub (x, y) =

 (x + y, x - y)</lang>

You can use pattern matching to extract the components:

<lang sml>let

 val (sum, difference) = addsub (33, 12)

in

 print ("33 + 12 = " ^ Int.toString sum ^ "\n");
 print ("33 - 12 = " ^ Int.toString difference ^ "\n")

end</lang>

Tcl

Tcl commands all return a single value, but this value can be a compound value such as a list or dictionary. The result value of a procedure is either the value given to the return command or the result of the final command in the body in the procedure. (Commands that return “no” value actually return the empty string.) <lang tcl>proc addsub {x y} {

   list [expr {$x+$y}] [expr {$x-$y}]

}</lang> This can be then assigned to a single variable with set or to multiple variables with lassign. <lang tcl>lassign [addsub 33 12] sum difference puts "33 + 12 = $sum, 33 - 12 = $difference"</lang>

TXR

TXR functions return material by binding unbound variables.

The following function potentially returns three values, which will happen if called with three arguments, each of which is an unbound variable:

<lang txr>@(define func (x y z)) @ (bind w "discarded") @ (bind (x y z) ("a" "b" "c")) @(end)</lang>

The binding w, if created, is discarded because w is not in the list of formal parameters. However, w can cause the function to fail because there can already exist a variable w with a value which doesn't match "discarded".

Call:

<lang txr>@(func t r s)</lang>

If t, r and s are unbound variables, they get bound to "a", "b" and "c", respectively via a renaming mechanism. This may look like C++ reference parameters or Pascal "var" parameters, and can be used that way, but isn't really the same at all.

Failed call ("1" doesn't match "a"):

<lang txr>@(func "1" r s)</lang>

Successful call binding only one new variable:

<lang txr>@(func "a" "b" s)</lang>