Return multiple values: Difference between revisions

Content added Content deleted
(Show how to return more than one value from a function. First examples use Factor, Ruby.)
 
Line 19: Line 19:
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.
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|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>
=={{header|Ruby}}==
=={{header|Ruby}}==
Every function returns one value. The conventional way to return multiple values is to bundle them into an Array.
Every function returns one value. The conventional way to return multiple values is to bundle them into an Array.