Programming |
Passing Optional Arguments to Nested Functions
You can use optional input and output arguments with nested functions, but you should be aware of how MATLAB interprets varargin
, varargout
, nargin
, and nargout
under those circumstances.
varargin
and varargout
are variables and, as such, they follow exactly the same scoping rules as any other MATLAB variable. Because nested functions share the workspaces of all outer functions, varargin
and varargout
used in a nested function can refer to optional arguments passed to or from the nested function, or passed to or from one of its outer functions.
nargin
and nargout
, on the other hand, are functions and when called within a nested function, always return the number of arguments passed to or from the nested function itself.
Using varargin and varargout
varargin
or varargout
used in a nested function can refer to optional arguments passed to or from that function, or to optional arguments passed to or from an outer function.
varargin
or varargout
in its function declaration line, then the use of varargin
or varargout
within that function returns optional arguments passed to or from that function.
varargin
or varargout
are not in the nested function declaration but are in the declaration of an outer function, then the use of varargin
or varargout
within the nested function returns optional arguments passed to the outer function.
In the example below, function C
is nested within function B
, and function B
is nested within function A
. The term varargin{1}
in function B
refers to the second input passed to the primary function A
, while varargin{1}
in function C
refers to the first argument, z
, passed from function B
:
function x = A(y, varargin) % Primary function A B(nargin, y * rand(4)) function B(argsIn, z) % Nested function B if argsIn >= 2 C(z, varargin{1}, 4.512, 1.729) end function C(varargin) % Nested function C if nargin >= 2 x = varargin{1} end end % End nested function C end % End nested function B end % End primary function A
Using nargin and nargout
When nargin
or nargout appears in a nested function, it refers to the number of inputs or outputs passed to that particular function, regardless of whether or not it is nested.
In the example shown above, nargin
in function A
is the number of inputs passed to A
, and nargin
in function C
is the number of inputs passed to C
. If a nested function needs the value of nargin
or nargout
from an outer function, you can pass this value in as a separate argument, as done in function B
.
Example of Passing Optional Arguments to Nested Functions
This example references the primary function's varargin
cell array from each of two nested functions. (Because the workspace of an outer function is shared with all functions nested within it, there is no need to pass varargin
to the nested functions.)
Both nested functions make use of the nargin
value that applies to the primary function. Calling nargin
from the nested function would return the number of inputs passed to that nested function, and not those that had been passed to the primary. For this reason, the primary function must pass its nargin
value to the nested functions.
function meters = convert2meters(miles, varargin) % Converts MILES (plus optional FEET and INCHES input) % values to METERS. if nargin < 1 || nargin > 3 error('1 to 3 input arguments are required'); end function feet = convert2Feet(argsIn) % Nested function that converts miles to feet and adds in % optional FEET argument. feet = miles .* 5280; if argsIn >= 2 feet = feet + varargin{1}; end end % End nested function convert2Feet function inches = convert2Inches(argsIn) % Nested function that converts feet to inches and adds in % optional INCHES argument. inches = feet .* 12; if argsIn == 3 inches = inches + varargin{2}; end end % End nested function convert2Inches feet = convert2Feet(nargin); inches = convert2Inches(nargin); meters = inches .* 2.54 ./ 100; end % End primary function convert2meters
Passing Variable Numbers of Arguments | Returning Output Arguments |
© 1994-2005 The MathWorks, Inc.