# Convenient nargout Behavior 8

Posted by **Loren Shure**,

Here's a question that was recently posed at MathWorks.

I'm trying to invoke a function handle. It either returns 0 or 1 output arguments. If it returns an output argument, I want to get it; if not I would like to just invoke the function and be on my way. I have no way of knowing ahead of time whether the function returns something or not.

This situation can arise when you are building an application in which you expect to work with user-supplied functions. You
can solve this using `try` construct, but this situation arises frequently enough that we decided to introduce the `nargout` bump. So what happens?

### Contents

### nargout Bump from 0 to 1

Many MATLAB functions return 1 result even when called with an `nargout` is 0. For these, here's a technique that may be of interest. It allows for a bump in `nargout` on the left-hand side.

Let's assume the variable `c` is unitialized and then call a function `f`.

[c{1:0}] = f(...);

`f` is called with `nargout` of 0, because `1:0` is an empty vector. If `f` returns no arguments, `c` is an empty `cell`. *However*, if `f` returns a result, `c` is a 1x1 cell containing that result.

### Why does nargout Behave this Way?

Since this is not a widely known technique, I'll explain one more use of it. Suppose you are writing a function `foo`, and after some preparation you want `foo` to return all of the results, if any, of a call on `bar`, passing along to `bar` the number of arguments given to `foo`. The template looks like this.

function varargout = foo(...) ... whatever ... [varargout{1:nargout}] = bar(...);

Anonymous functions use exactly this technique to pass along `nargout` to their body and to return that body's results as the result of the anonymous function. This requirement on anonymous functions,
and the observation that a significant number of other functions work in the same way, is why we introduced the bump.

### Will this Simplify Some Code?

I wonder if this information allows you to simplify some of your code. Please share your results here.

Get the MATLAB code

Published with MATLAB® 7.8

**Category:**- Best Practice,
- Less Used Functionality,
- Robustness

### Note

Comments are closed.

## 8 CommentsOldest to Newest

**1**of 8

Useful. It allowed me to change:

str = sprintf('"%s" %s', xpdf_path, cmd); if nargout [varargout{:}] = system(str); else system(str); end

to:

[varargout{1:nargout}] = system(sprintf('"%s" %s', xpdf_path, cmd));

Oliver

**2**of 8

I use similar technique to access subfunctions because I don’t want to have many small functions in different m-files though ‘class’ can solve this too.

**3**of 8

Thanks Loren. This code really helped me in using subsref in my classes. I wanted to only change some subsref functionality and call the builtin version for the rest, but ran into output argument issues. I ended up doing something like:

function varargout = subsref(this, s)

use_built_in = true;

% Add custom subsref here and set use_built_in to false.

% Use default subsref for others.

if use_built_in

[varargout{1:nargout}] = builtin(‘subsref’, this, s);

end

end

**4**of 8

I’m delighted that several of you find good use in this nargout behavior.

–Loren

**5**of 8

I understand that 1:0 is an empty matrix.

But, by what is happening with [c{1:0}] = f(…)

Is it really doing something like: [c{1:0}] = [] ?

Also, what do you mean when you say, “when called with an nargout is zero . . . ?” The FRP is nicely clear in explaining that it can be called without an input argument to return the number of output arguments a user has supplied. Or, it can be called with a function handle or a function name as an input argument to return the number of output arguments the function was written to provide. So, which use are you referring to?

**6**of 8

OysterEngineer-

When MATLAB goes to calcualate [c{1:0}] = f(…),

it is finding 0 elements on the left side (numel([c{1:0}]) IS 0) which IS nargout(f), and therefore f gets called as if nargout is 0.

–Loren

**7**of 8

That helps some. I see how the numel([c{1:0}0] is 0.

But, I haven’t been able to invent a function, f, with no outputs such that nargout(‘f’) or nargout(@f)is also 0. I keep getting -1.

**8**of 8

Thanks. This trick makes the following function almost trivial.

function v = mdgrid(A, N) % MDGRID multi-dimensional grid % % Returns all possible combinations of N elements of A % % See also ndgrid. % [out{1:N}] = ndgrid(A); v = cat(N+1, out{:});

## Recent Comments