You get two different answers because you are testing two different functions:
- the function handle to addOne
- an anonymous function (which happens to call some other function/s inside it, but these are irrelevant).
Testing the simple function handle returns the number of output arguments supported by that function:
However nargout only measures the output of the anonymous function itself, not any functions that happen to be called inside it. And anonymous functions attempt to return as many output arguments as are requested when the code is evaluated, i.e. the anonymous function syntax supports any number of output arguments, which the nargout documentation tells us is indicated by a negative output value. For example:
These all return -1 because nargout can only check the anonymous function itself, NOT any functions that happen to be called inside it!