Next: Variable-length Argument Lists, Previous: Defining Functions, Up: Functions and Scripts [Contents][Index]
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.
It is possible to use the nthargout
function to obtain only some
of the return values or several at once in a cell array.
See Cell Array Objects.
Return the nth output argument of function given by the function handle or string func. Any arguments after func are passed to func. The total number of arguments to call func with can be passed in ntot; by default ntot is n. The input n can also be a vector of indices of the output, in which case the output will be a cell array of the requested output arguments.
The intended use nthargout
is to avoid intermediate variables.
For example, when finding the indices of the maximum entry of a
matrix, the following two compositions of nthargout
m = magic (5); cell2mat (nthargout ([1, 2], @ind2sub, size (m), nthargout (2, @max, m(:)))) ⇒ 5 3
are completely equivalent to the following lines:
m = magic (5); [~, idx] = max (M(:)); [i, j] = ind2sub (size (m), idx); [i, j] ⇒ 5 3
It can also be helpful to have all output arguments in a single cell in the following manner:
USV = nthargout ([1:3], @svd, hilb (5));
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.
Within a function, return the number of values the caller expects to receive. If called with the optional argument fcn—a function name or handle—return the number of declared output values that the function can produce. If the final output argument is varargout the returned value is negative.
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
.
In the second usage,
nargout (@histc) % or nargout ("histc")
will return 2, because histc
has two outputs, whereas
nargout (@imread)
will return -2, because imread
has two outputs and the second is
varargout.
At the top level, nargout
with no argument is undefined and will
produce an error. nargout
does not work for built-in functions and
returns -1 for all anonymous functions.
It is good practice at the head of a function to verify that it has been called correctly. In Octave the following idiom is seen frequently
if (nargin < min_#_inputs || nargin > max_#_inputs) print_usage (); endif
which stops the function execution and prints a message about the correct way to call the function whenever the number of inputs is wrong.
For compatibility with MATLAB, nargchk
, narginchk
and
nargoutchk
are available which provide similar error checking.
Return an appropriate error message string (or structure) if the number of inputs requested is invalid.
This is useful for checking to see that the number of input arguments supplied to a function is within an acceptable range.
See also: nargoutchk, narginchk, error, nargin, nargout.
Check for correct number of arguments or generate an error message if the number of arguments in the calling function is outside the range minargs and maxargs. Otherwise, do nothing.
Both minargs and maxargs need to be scalar numeric values. Zero, Inf and negative values are all allowed, and minargs and maxargs may be equal.
Note that this function evaluates nargin
on the caller.
See also: nargchk, nargoutchk, error, nargout, nargin.
Check for correct number of output arguments.
On the first form, returns an error unless the number of arguments in its
caller is between the values of minargs and maxargs. It does
nothing otherwise. Note that this function evaluates the value of
nargout
on the caller so its value must have not been tampered with.
Both minargs and maxargs need to be a numeric scalar. Zero, Inf and negative are all valid, and they can have the same value.
For backward compatibility reasons, the other forms return an appropriate error message string (or structure) if the number of outputs requested is invalid.
This is useful for checking to see that the number of output arguments supplied to a function is within an acceptable range.
Next: Variable-length Argument Lists, Previous: Defining Functions, Up: Functions and Scripts [Contents][Index]