       Re: Indefinite numbers of arguments in a function

• To: mathgroup at smc.vnet.net
• Subject: [mg87844] Re: Indefinite numbers of arguments in a function
• From: Albert Retey <awnl at arcor.net>
• Date: Fri, 18 Apr 2008 07:10:16 -0400 (EDT)
• References: <fu9fnt\$c91\$1@smc.vnet.net>

```Patrick Klitzke wrote:
> Hello everybody,
> Is it possible to define a function in Mathematica, where the numbers of
> arguments does not matter?
>
> 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
>
> I also know that I can create a list as one argument:
>
> MyPlus[list_List] := (
>   m = 0; For[n = 1, n < Length[list] + 1, n++, m += list[[n]] ];
>   m
>    )
>
>
> But since there are functions like Plus, there has to be a way to define
> those kind of functions.
>
> I would be very glad, if someone could give me his advice.
>
> Best regards,
>
> Patrick Klitzke
>
> email:   philologos14 at gmx.de

Any symbol can have attributes which control how it behaves. The
attribute you are looking for is Orderless:

In:= SetAttributes[fff,Orderless]
In:= fff[1,2,3,4]==fff[2,4,3,1]
Out= True

How would you know? Check the attributes of Plus and see which fits, if
in doubt, look them up in the documentation:

In:= Attributes[Plus]
Out= {Flat,Listable,NumericFunction,OneIdentity,Orderless,Protected}

hth,

albert

```

• Prev by Date: Re: Why isn't Expand[] grouping terms by order?
• Next by Date: Re: Abs[x] function
• Previous by thread: Re: Indefinite numbers of arguments in a function
• Next by thread: Re: Indefinite numbers of arguments in a function