Return Multiple Output Arguments

If the output argument in the function header contains a list of names, then each of these name will become a local variable for the function. At the end of the function call, a list will be built using these output variables and returned to the caller. For example
>> summary = function x -> (mean, min, Q1, median, Q3, max)
            x = sort(x);
            n = length(x);
            s = x[1];
            min = x[1];
            max = x[1];
            for k = 2 : n
                 if x[k] < min
                      min = x[k];
                 end
                 if x[k] > max
                      max = x[k];
                 end
                 s += x[k];
            end
            mean = s / n;

            global.findmedian = function y -> md
                 N = length(y)
                 if N % 2
                      md = y[(N + 1) / 2];
                 else
                      md = (y[N / 2] + y[N / 2 + 1]) / 2;
                 end
            end

            median = findmedian(x)

            if n % 2
                 Q1 = findmedian(x[1 : (n + 1) / 2]);
                 Q3 = findmedian(x[(n + 1) / 2 : n]);
            else
                 Q1 = findmedian(x[1 : n / 2]);
                 Q3 = findmedian(x[n / 2 + 1 : n]);
            end

       end
>> s = summary([3, 5, 2, 1, 9, 10, 22])
     (7.42857, 1, 2.5, 5, 9.5, 22)
The function returns a single value, which is list of all output arguments. We can use a multi-assignment statement to pass all the list assignments to individual variables of the caller
(mean, min, Q1, median, Q3, max) = summary([3, 5, 2, 1, 9, 10, 22]);



oz 2009-12-22