Next: , Previous: Defining Functions, Up: Functions and Scripts


13.2 Multiple Return Values

Unlike many other computer languages, Octave allows you to define functions that return more than one value. The syntax for defining functions that return multiple values is

     function [ret-list] = name (arg-list)
       body
     endfunction

where name, arg-list, and body have the same meaning as before, and ret-list is a comma-separated list of variable names that will hold the values returned from the function. The list of return values must have at least one element. If ret-list has only one element, this form of the function statement is equivalent to the form described in the previous section.

Here is an example of a function that returns two values, the maximum element of a vector and the index of its first occurrence in the vector.

     function [max, idx] = vmax (v)
       idx = 1;
       max = v (idx);
       for i = 2:length (v)
         if (v (i) > max)
           max = v (i);
           idx = i;
         endif
       endfor
     endfunction

In this particular case, the two values could have been returned as elements of a single array, but that is not always possible or convenient. The values to be returned may not have compatible dimensions, and it is often desirable to give the individual return values distinct names.

In addition to setting nargin each time a function is called, Octave also automatically initializes nargout to the number of values that are expected to be returned. This allows you to write functions that behave differently depending on the number of values that the user of the function has requested. The implicit assignment to the built-in variable ans does not figure in the count of output arguments, so the value of nargout may be zero.

The svd and lu functions are examples of built-in functions that behave differently depending on the value of nargout.

It is possible to write functions that only set some return values. For example, calling the function

     function [x, y, z] = f ()
       x = 1;
       z = 2;
     endfunction

as

     [a, b, c] = f ()

produces:

     a = 1
     
     b = [](0x0)
     
     c = 2

along with a warning if the value of the built-in variable warn_undefined_return_values is nonzero.

— Built-in Function: nargout ()
— Built-in Function: nargout (fcn_name)

Within a function, return the number of values the caller expects to receive. If called with the optional argument fcn_name, return the maximum number of values the named function can produce, or -1 if the function can produce a variable number of values.

For example,

          f ()
     

will cause nargout to return 0 inside the function f and

          [s, t] = f ()
     

will cause nargout to return 2 inside the function f.

At the top level, nargout is undefined.

— Built-in Variable: warn_undefined_return_values

If the value of warn_undefined_return_values is nonzero, print a warning if a function does not define all the values in the return list which are expected. The default value is 1.

— Function File: nargchk (nargin_min, nargin_max, n)

If n is in the range nargin_min through nargin_max inclusive, return the empty matrix. Otherwise, return a message indicating whether n is too large or too small.

This is useful for checking to see that the number of arguments supplied to a function is within an acceptable range.