Return multiple values
Show how to return more than one value from a function.
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.
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>
You can 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>
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>
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>
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>
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>
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>