In Matlab, such issue is handled by

varargout, // a cell structure to store all output arguments
varargin, // a cell structure to store all input arguments
nargout, // number of output arguments
nargin // number of input arguments

Is there any equivalent implementation in Mathematica, and how? A simple working example would be much appreciated!

  • $\begingroup$ Duplicate: mathematica.stackexchange.com/q/314/5 (My question was rather out of curiosity... I wouldn't advise you to ever write code that makes use of it). As for the input variables, it has been addressed below (and is reasonably well documented as well). $\endgroup$
    – rm -rf
    Commented Nov 28, 2013 at 1:09

2 Answers 2


Use a doble underscore (triple if you want your function to accept no arguments at all):

f[x__] := List[x]

This is an example


You can specify the type of argument, and you can also have composite data structures like sequences of two element lists, for example. As for determining the number of argument, in the simple example given above, I'd use the Length of the list of the sequence of arguments given by x, that is Length[List[x]]. The following function is declaring narg to make it local (but you might also use a global variable in order to access it independently from the functions that use it / but you must clarify what you need those values for if you want a sensible example.

f[x__] := Block[{narg},
    narg = Length[ List[x] ];
    {narg, List[x]}

In this toy example we output a list with the number of arguments and a list of the arguments passed to the function.

{1, {42}}
{5, {a,b,c,d,e}}

The number of outputs can be a a little conundrum. For example, in the above example there is one output: a list with two elements. If you consider that "two outputs" you could concoct a way to return that number too by using Length. In any case, if you let your function return the number of output too, you'd return n+1 outputs.

You might at this point define two global variables: $nargin and $nargout or even a global stack with those values but... why should you need these values in Mathematica is the key question here.

  • $\begingroup$ Thanks. This explains the varying argument number. How to obtain the number of input arguments? and how to use input arguments one by one? And also how to handle output arguments? $\endgroup$ Commented Nov 28, 2013 at 0:56
  • 1
    $\begingroup$ Why List[x] instead of {x}? :) $\endgroup$
    – rm -rf
    Commented Nov 28, 2013 at 1:10
  • $\begingroup$ @rm-rf I wanted to make clear that I was wrapping a head around x. I-m not a big fan of 'too compact' code. $\endgroup$
    – Peltio
    Commented Nov 28, 2013 at 1:24

One simple way is to bundle the various arguments in a vector. An example would be:


Obviously, this works fine for scalars x. If you make x a vector:

 f[{2, 3, 4}]

You can query the Length of the vector by Length[x] and then use this in your function. For example:

g[x_] := Length[x]^2

does what you think it would, imitating somewhat nargin.


Not the answer you're looking for? Browse other questions tagged or ask your own question.