Return multiple values: Difference between revisions

added go
m (→‎{{header|Factor}}: Use bi* to prevent swap.)
(added go)
Line 17:
 
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.
 
=={{header|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>
 
=={{header|J}}==
Anonymous user