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: [mg51937] Re: [mg51886] Functions with optional parameters
  • From: Yasvir Tesiram <yat at omrf.ouhsc.edu>
  • Date: Fri, 5 Nov 2004 02:18:01 -0500 (EST)
  • References: <200411040649.BAA18109@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

G'day,

I hope the following gets you started. Play around with different 
patterns and types of patterns in expressions and have a look in the 
Mathematica Book in Section 2.1.1 and also in A.5.1. Also look at the 
syntax for Sum. Your expression below will not work in Mathematica for 
what you want to do.

Clear[f]
f[x_List] := Apply[Plus, x]
f[1, 2, 3, 4]

doesn't do what you want it to do. But,
f[{1, 2, 3, 4}]
f[{{1, 2, 3, 4}, {a, b, c, d}}]
do.

And

Clear[f1]
f1[x_] := Apply[Plus, List[x]]
f1[1, a, 3]

also doesn't do what you want it to do. But,

Clear[f1]
f1[x__] := Apply[Plus, List[x]]
f1[1, a, 3]
f1[{1, 2, 3, 4}, {a, b, c, d}]

does what you expect. There are probably another umpteen ways of 
writing this in Mathematica.

Cheers
Yas



On Nov 4, 2004, at 12:49 AM, Gregory Lypny wrote:

> 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: [MathGroup]: Descriptive headings
  • Previous by thread: Re: Functions with optional parameters
  • Next by thread: Re: Functions with optional parameters