MathGroup Archive 2004

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

Search the Archive

Re: Functions with optional parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51963] Re: Functions with optional parameters
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Fri, 5 Nov 2004 02:19:57 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On 11/4/04 at 1:49 AM, gregory.lypny at videotron.ca (Gregory Lypny)
wrote:

>How can I create functions with optional parameters?  For example,
>a simple function to add two or three numbers:

>addThem[a_,b_,c_]:=If[c is not empty, Sum[a,b,c],Sum[a,b]]

>I'm not sure of the proper syntax to test whether c is null or
>empty.

In this particular case, it might be simpler/faster to give c a default value of 0, i.e.,

addThem[a_,b_,c_:0]:=a+b+c

addThem[1, 2]
3

addThem[1, 2, 3]
6

My guess is adding 0 is faster than testing to see whether c is empty and deciding which branch to take
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: foreach loop
  • Next by Date: Re: foreach loop
  • Previous by thread: Re: Functions with optional parameters
  • Next by thread: Re: Functions with optional parameters