Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Regarding my recent question on using a default value for a function argument when a pattern was not met yielded some interesting answers, but the general consensus was "Yes this can be done, but there are better options."

Assuming the same conditions that certain function arguments (rt and pt in this case) should default to zero when less than zero, the three main options are as follows:


func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := 
     func[{qi, dei, b, dmin, Max[0, rt], Max[0, pt], t}]

func[{qi_, dei_, b_, dmin_, rt_, pt_, t_}] := (*do stuff*)


func[qi_, dei_, b_, dmin_, rt_?Negative, pt_, t_] := func[qi, dei, b, dmin, 0, pt, t]
func[qi_, dei_, b_, dmin_, rt_, pt_?Negative, t_] := func[qi, dei, b, dmin, rt, 0, t]

func[qi_, dei_, b_, dmin_, rt_, pt_, t_] :=(*do stuff*)


func[qi_, dei_, b_, dmin_, rt_, pt_, t_] := Module[{rtt = Max[0,rt], ptt = Max[0,pt]},
   (*do stuff with rtt and ptt instead*) ]

What are the pros and cons of each function definition? Which is the most efficient?

share|improve this question
Sorry I forgot to update my old answer but you'll get more diverse opinions this way anyhow. – Mr.Wizard Feb 25 '13 at 15:57
@Mr.Wizard No big deal. – kale Feb 25 '13 at 16:00
Methink you've lost the braces around some functions' arguments. I'm not editing your question because you already rolled back one edit. But please recheck – Dr. belisarius Feb 25 '13 at 16:41
Your function with _?Negative doesn't do what the other does, for instance if you pass rt=-3; pt=1 it will be evaluated as rt=0; pt=0 instead of rt=0;pt=1 – ssch Feb 25 '13 at 17:16
@belisarius Yep, sorry. Some extra braces made it in there. Edited... – kale Feb 25 '13 at 18:47

To start the discussion I'll look at timings. A simplistic test indicates that the DownValues method is somewhat faster, and this agrees with my past experience as well.

f1[a_, b_, c_, d_] := f1[{a, b, Max[c, 0], Max[d, 0]}]
f1[{a_, b_, c_, d_}] := a + b + c + d

f2[a_, b_, c0_, d0_] := With[{c = Max[c0, 0], d = Max[d0, 0]}, a + b + c + d]

a = RandomInteger[{-5, 5}, {1*^6, 4}];

f1 @@@ a // Timing // First
f2 @@@ a // Timing // First



Note that I changed Module to With. This should always be faster when it can be used.
For comparison:

f2m[a_, b_, c0_, d0_] := Module[{c = Max[c0, 0], d = Max[d0, 0]}, a + b + c + d]

f2m @@@ a // Timing // First


share|improve this answer
Wow... I wasn't aware that Module is so much slower! – sebhofer Feb 25 '13 at 18:20
@sebhofer Yes, for simple (fast) functions the overhead of Module is significant. With does not let you modify variables. Block is faster than Module but you have to be very careful to avoid unintended consequences. You can often combine e.g. Module outside the definition of the function and Block inside for something reasonably fast. – Mr.Wizard Feb 26 '13 at 1:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.