MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
On the other hand, nargin and nargout are functions. Within any function, including
nested functions, calls to nargin or nargout return the number of arguments for that
function. If a nested function requires the value of nargin or nargout from an outer
function, pass the value to the nested function.

For example, create a function in a file named showNumArgs.m that passes the number of
input arguments from the primary (parent) function to a nested function.

function showNumArgs(varargin)

disp(['Number of inputs to showNumArgs: ',int2str(nargin)]);
nestedFx(nargin,2,3,4)

function nestedFx(n,varargin)
disp(['Number of inputs to nestedFx: ',int2str(nargin)]);
disp(['Number of inputs to its parent: ',int2str(n)]);
end

end

Call showNumArgs and compare the output of nargin in the parent and nested functions.

showNumArgs(0,1)

Number of inputs to showNumArgs: 2
Number of inputs to nestedFx: 4
Number of inputs to its parent: 2

See Also
nargin | nargout | varargin | varargout

21 Function Arguments

Free download pdf