Re: Indefinite numbers of arguments in a function
- To: mathgroup at smc.vnet.net
- Subject: [mg87865] Re: [mg87818] Indefinite numbers of arguments in a function
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Sat, 19 Apr 2008 03:32:08 -0400 (EDT)
- References: <200804180637.CAA12448@smc.vnet.net>
On Apr 18, 2008, at 2:37 AM, Patrick Klitzke wrote: > Hello everybody, > Is it possible to define a function in Mathematica, where the > numbers of > arguments does not matter? Use the appropriate patterns f[x__] := .... Allows one or more values to match x f[x___] := ... Allows zero or more values for x. > > I know the function Plus is defined like that: > > I call the function with two arguments( for example Plus[5,3]) or I > can > call the function with five arguments (for example > Plus[1,6,4,6,8]). > > How can i define a function in Mathematica like that? I know I can > define for ever number of arguments a function like that: > MyPlus[a_,b_]:=a+b > MyPlus[a_,b_,c_]:=a+b+c > MyPlus[a_,b_,c_,d_]:=a+b+c+d > MyPlus[a_,b_,c_,d_,e_]:=a+b+c+d+e MyPlus[a__]:=Plus[a] or MyPlus[a__]:=Sum[{a}[[i]],{i,Length[{a}]}] if you don't want to use Plus. Regards, Ssezi
- References:
- Indefinite numbers of arguments in a function
- From: Patrick Klitzke <philologos14@gmx.de>
- Indefinite numbers of arguments in a function