⚠️ Warning: This is a draft ⚠️

This means it might contain formatting issues, incorrect code, conceptual problems, or other severe issues.

If you want to help to improve and eventually enable this page, please fork RosettaGit's repository and open a merge request on GitHub.

{{task|Basic language learning}} [[Category:Functions and subroutines]] {{omit from|GUISS}}

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

ACL2

;; To return multiple values:
(defun multiple-values (a b)
(mv a b))

;; To extract the values:
(mv-let (x y)
(multiple-values 1 2)
(+ x y))

Ada functions can only return one type. That type could be an array or record holding multiple values, but the usual method for returning several values is using a procedure with 'out' parameters. By default, all parameters are 'in', but can also be 'out', 'in out' and 'access'. Writing to an 'out' parameter simply changes the value of the variable passed to the procedure.

procedure MultiReturn is
procedure SumAndDiff (x, y : Integer; sum, diff : out Integer) is begin
sum := x + y;
diff := x - y;
end SumAndDiff;
inta : Integer := 5;
intb : Integer := 3;
thesum, thediff : Integer;
begin
SumAndDiff (inta, intb, thesum, thediff);
Put_Line ("Sum:" & Integer'Image (thesum));
Put_Line ("Diff:" & Integer'Image (thediff));
end MultiReturn;

{{out}}

Sum: 8
Diff: 2

Agena

Agena allows functions to return multiple values.

Tested with Agena 2.9.5 Win32

# define a function returning three values
mv := proc() is
return 1, 2, "three"
end ; # mv

scope # test the mv() proc
local a, b, c := mv();
print( c, b, a )
epocs

ALGOL 68

{{works with|ALGOL 68G|Any - tested with release 2.6.win32}} Procedures in Algol 68 can only return one value, so to return multiple values, a structure (or array if all the values have the same mode) can be used.

# example mode for returning multiple values from a procedure #
MODE PAIR = STRUCT( STRING name, INT value );

# procedure returning multiple values via a structure #
PROC get pair = ( INT a )PAIR:
CASE a
IN #1#    ( "H",  0 )
,  #2#    ( "He", 1 )
,  #3#    ( "Li", 3 )
OUT       ( "?",  a )
ESAC
;

main: (
# use the result as a whole #
print( ( get pair( 3 ), newline ) );
# access the components separately #
print( ( name OF get pair( 1 ), value OF get pair( 2 ), newline ) )
)

{{out}}

Li         +3
H         +1

ALGOL W

Algol W procedures can't return arrays but records can be used to return multiple values.

begin
% example using a record type to return multiple values from a procedure %
record Element ( string(2) symbol; integer atomicNumber );
reference(Element) procedure getElement( integer value n ) ;
begin
Element( if      n < 1 then   "?<"
else if n > 3 then   "?>"
else case n of ( %1% "H"
, %2% "He"
, %3% "Li"
)
, n
)
end getElement ;
% test the procedure %
begin
reference(Element) elementData;
for n := 0 until 4 do begin
elementData := getElement(n);
write( s_w := 0, i_w := 1
, atomicNumber(elementData)
, " "
, symbol(elementData)
);
end
end

end.

ANSI Standard BASIC

The most straightforward way of returning multiple values is to specify them as parameters.

100 DECLARE EXTERNAL SUB sumdiff
110 !
120 CALL sumdiff(5, 3, sum, diff)
130 PRINT "Sum is "; sum
140 PRINT "Difference is "; diff
150 END
160 !
170 EXTERNAL SUB sumdiff(a, b, c, d)
180 LET c = a + b
190 LET d = a - b
200 END SUB

ATS

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

//
#include
//
(* ****** ****** *)

(
x: int, y: int
) : (int, int) = (x+y, x-y)

(* ****** ****** *)

implement
main0 () = let
val (sum, diff) = addsub (33, 12)
in
println! ("33 + 12 = ", sum);
println! ("33 - 12 = ", diff);
end (* end of [main0] *)

AutoHotkey

{{works with|AutoHotkey_L}} Functions may return one value. The conventional way to return multiple values is to bundle them into an Array.

return [x + y, x - y]
}

AutoIt

Return an array.

Local \$aReturn
\$aReturn = \$iX + \$iY
\$aReturn = \$iX - \$iY
Return \$aReturn
EndFunc

=

BaCon

= BaCon can return homogeneous dynamic arrays, or RECORD data holding heterogeneous types.

' Return multiple values
RECORD multi
LOCAL num
LOCAL s\$
END RECORD

FUNCTION f(n) TYPE multi_type
LOCAL r = { 0 } TYPE multi_type
r.num = n
r.s\$ = "Hitchhiker's Guide"
RETURN r
END FUNCTION

DECLARE rec TYPE multi_type
rec = f(42)
PRINT rec.num
PRINT rec.s\$
PRINT rec.s\$

{{out}}

prompt\$ ./return-multiple
42
Hitchhiker's Guide

=

BBC BASIC

= The most straightforward way of returning multiple values is to specify them as RETURNed parameters.

PROCsumdiff(5, 3, sum, diff)
PRINT "Sum is " ; sum
PRINT "Difference is " ; diff
END

DEF PROCsumdiff(a, b, RETURN c, RETURN d)
c = a + b
d = a - b
ENDPROC

==={{header|IS-BASIC}}=== 100 NUMERIC SUM,DIFF 110 CALL SUMDIFF(5,3,SUM,DIFF) 120 PRINT "Sum is";SUM:PRINT "Difference is";DIFF 130 END 140 DEF SUMDIFF(A,B,REF C,REF D) 150 LET C=A+B:LET D=A-B 160 END DEF

## Bracmat

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

```bracmat

You can use pattern matching to extract the components:

& out\$("33 + 12 = " !sum)
& out\$("33 - 12 = " !difference)
);

{{out}}

33 + 12 =  45
33 - 12 =  21

C

C has structures which can hold multiple data elements of varying types.

#include <stdio.h>

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

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

int main()
{
Composite C = example();

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

return 0;
}

{{out}}

Values from a function returning a structure : { 1, 2.300000, a, Hello World, 45.678000}

C99 and above also allow structure literals to refer to the name, rather than position, of the element to be initialized:

#include <stdio.h>

typedef struct {
char *first, *last;
} Name;

Name whatsMyName() {
return (Name) {
.first = "James",
.last = "Bond",
};
}

int main() {
Name me = whatsMyName();
printf("The name's %s. %s %s.\n", me.last, me.first, me.last);
return 0;
}

{{out}}

The name's Bond. James Bond.

C++

Since C++11, the C++-standard-library includes tuples, as well as an easy way to destructure them.

#include <algorithm>
#include <array>
#include <cstdint>
#include <iostream>
#include <tuple>

std::tuple<int, int> minmax(const int * numbers, const std::size_t num) {
const auto maximum = std::max_element(numbers, numbers + num);
const auto minimum = std::min_element(numbers, numbers + num);
return std::make_tuple(*minimum, *maximum) ;
}

int main( ) {
const auto numbers = std::array<int, 8>{{17, 88, 9, 33, 4, 987, -10, 2}};
int min{};
int max{};
std::tie(min, max) = minmax(numbers.data(), numbers.size());
std::cout << "The smallest number is " << min << ", the biggest " << max << "!\n" ;
}

{{out}}

The smallest number is -10, the biggest 987!