Task

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




include "cowgol.coh";
 
# In Cowgol, subroutines can simply define multiple output parameters.
sub MinMax(arr: [uint8], len: intptr): (min: uint8, max: uint8) is
    min := 255;
    max := 0;
    while len > 0 loop
        len := len - 1;
        var cur := [arr];
        if min > cur then min := cur; end if;
        if max < cur then max := cur; end if; 
        arr := @next arr;
    end loop;
 
    # Values are also returned automatically.
end sub;
 
# Example of usage:
var nums: uint8[] := {23, 65, 33, 12, 95, 5, 32, 91, 135, 25, 8};
var least: uint8;
var most: uint8;
 
# Accept two output parameters from a function
(least, most) := MinMax(&nums[0], @sizeof nums);
 
print("Min: "); print_i8(least); print_nl();
print("Max: "); print_i8(most); print_nl();