Return multiple values: Difference between revisions
(Added output to D code) |
(Added PicoLisp) |
||
Line 132: | Line 132: | ||
echo "33 + 12 = $sum\n"; |
echo "33 + 12 = $sum\n"; |
||
echo "33 - 12 = $difference\n";</lang> |
echo "33 - 12 = $difference\n";</lang> |
||
=={{header|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> |
|||
=={{header|Pike}}== |
=={{header|Pike}}== |
Revision as of 06:51, 13 October 2011
Show how to return more than one value from a function.
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. To see all the values, use stuff like multiple-value-call
. On a clisp prompt:
<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>
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>
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>
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>
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>
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>
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>
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>
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>