Re: Default value

*To*: mathgroup at smc.vnet.net*Subject*: [mg74872] Re: Default value*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>*Date*: Mon, 9 Apr 2007 06:14:52 -0400 (EDT)*Organization*: The Open University, Milton Keynes, UK*References*: <evabdq$dav$1@smc.vnet.net>

Bruno Campanini wrote: > I have a function f [ i_, m_, iv_ ] and would like > to have a default value for iv equal to i. > > I neither can write f [ i_, m_, iv_: i_ ], nor f [ i_, m_, iv_ : i ] > ans the error "Default value for iv_ contains a pattern" arises. Well, both of them work, thought not in a way you expect. In[1]:= Clear[f] f[i_, m_, iv_:i_] := (i + m)*iv f[1, 2, 3] f[1, 2] Optional::"opdef" : "The default value for the optional argument (iv_:i_) contains a pattern. More... Out[3]= 9 Out[4]= 3 i_ In[5]:= Clear[f] f[i_, m_, iv_:i] := (i + m)*iv f[1, 2, 3] f[1, 2] Out[7]= 9 Out[8]= 3 i > Any workaround? > > Bruno > What you want is an optional third argument that if no value is provided to it by the user, then it takes the value of the first argument. The following code does behave like that. In[9]:= Clear[f] f[i_, m_] := Block[{iv = i}, (i + m)*iv] f[i_, m_, iv_] := (i + m)*iv f[2, 3, 5] f[2, 3] Out[12]= 25 Out[13]= 10 Regards, Jean-Marc