Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

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


  • Prev by Date: Re: Product command with matrices
  • Next by Date: Re: How to solve this simple equation?
  • Previous by thread: Indefinite numbers of arguments in a function
  • Next by thread: Re: Indefinite numbers of arguments in a function