Programming and Data Types    

Checking the Number of Function Arguments

The nargin and nargout functions let you determine how many input and output arguments a function is called with. You can then use conditional statements to perform different tasks depending on the number of arguments. For example,

Given a single input argument, this function squares the input value. Given two inputs, it adds them together.

Here's a more advanced example that finds the first token in a character string. A token is a set of characters delimited by whitespace or some other character. Given one input, the function assumes a default delimiter of whitespace; given two, it lets you specify another delimiter if desired. It also allows for two possible output argument lists.

The strtok function is a MATLAB M-file in the strfun directory.

  How Functions Work Passing Variable Numbers of Arguments