Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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!

share|improve this question

marked as duplicate by Michael E2, rm -rf Nov 28 '13 at 1:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
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). –  rm -rf Nov 28 '13 at 1:09

2 Answers 2

up vote 1 down vote accepted

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

f[x__] := List[x]

This is an example

f[1]
{1}
f[1,2]
{1,2}

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.

f[42]
{1, {42}}
f[a,b,c,d,e]
{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.

share|improve this answer
    
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? –  LCFactorization Nov 28 '13 at 0:56
1  
Why List[x] instead of {x}? :) –  rm -rf Nov 28 '13 at 1:10
    
@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. –  Peltio Nov 28 '13 at 1:24

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

 f[x_]:=x^2

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

 f[{2, 3, 4}]
 {4,9,16}

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.

share|improve this answer

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