Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

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

Search the Archive

Re: Functions with optional parameters

  • To: mathgroup at smc.vnet.net
  • Subject: [mg51945] Re: [mg51886] Functions with optional parameters
  • From: "David Park" <djmp at earthlink.net>
  • Date: Fri, 5 Nov 2004 02:18:46 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

Gregory,

Look up Section 2.3.9, Optional and default arguments in The Mathematica
Book, available in Help.

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

addThem[1, 2]
3

addThem[1, 2, 3]
6

Of course, there are easier ways to add a variable length list of numbers so
another example might be more instructive. How about...

combindThem[a_, b_, opt_:Plus] := a~opt~b

combindThem[a, b]
a + b

combindThem[a, b, Times]
a b

combindThem[a, b, Power]
a^b

combindThem[a, b, #1^2 + #2^2 & ]
a^2 + b^2

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/


From: Gregory Lypny [mailto:gregory.lypny at videotron.ca]
To: mathgroup at smc.vnet.net

Hello everyone,

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.

	Greg




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