nargin

Returns the number of arguments required by a function, or the number of arguments received by a function, as detailed below.

Syntax

R = nargin('functionname')

R = nargin(@functionname)

R = nargin()

Inputs

'functionname'
Text of the function name.
Type: char | string
@functionname
A handle to the function name.
Type: char | string

Outputs

R
The number of arguments required (Case 1) or provided (Case 2).
Case 1
When nargin is called with a function handle or function name, it returns the number of arguments required by the function. 0 means the function accepts no arguments. A positive number indicates a required, minimum argument count. A negative number indicates the first optional argument.
Case 2
When nargin (with no arguments) is called inside a function, it returns a positive number indicating the number of arguments received by the function.
Type: integer

Example

printf('nargin count for function ''cos'' is %d\n', nargin(@cos))
nargin count for function 'cos' is 1
printf('nargin count for function ''clear'' is %d\n', nargin('clear'))
nargin count for function 'clear' is -1
printf('nargin count for function ''sprintf'' is %d\n', nargin('sprintf'))
nargin count for function 'sprintf' is -2
function testfunc(firstarg, secondarg, thirdarg)
	    printf('arguments received %d\n', nargin())
      end

      printf('function %s takes %d arguments\n', 'testfunc', nargin('testfunc'))
function testfunc takes 3 arguments
printf('function %s takes %d arguments\n', 'testfunc', nargin(@testfunc))
function testfunc takes 3 arguments
printf('call testfunc with 0 arguments - ')
      testfunc()
call testfunc with 0 arguments - arguments received 0
printf('call testfunc with 1 arguments - ')
      testfunc(1)
call testfunc with 1 arguments - arguments received 1
printf('call testfunc with 2 arguments - ')
      testfunc(1, 2)
call testfunc with 2 arguments - arguments received 2
printf('call testfunc with 3 arguments - ')
      testfunc(1, 2, 3)
call testfunc with 3 arguments - arguments received 3