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?

  • $\begingroup$ Sorry I forgot to update my old answer but you'll get more diverse opinions this way anyhow. $\endgroup$ – Mr.Wizard Feb 25 '13 at 15:57
  • $\begingroup$ @Mr.Wizard No big deal. $\endgroup$ – kale Feb 25 '13 at 16:00
  • $\begingroup$ 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 $\endgroup$ – Dr. belisarius Feb 25 '13 at 16:41
  • $\begingroup$ 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 $\endgroup$ – ssch Feb 25 '13 at 17:16
  • $\begingroup$ @belisarius Yep, sorry. Some extra braces made it in there. Edited... $\endgroup$ – 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


  • $\begingroup$ Wow... I wasn't aware that Module is so much slower! $\endgroup$ – sebhofer Feb 25 '13 at 18:20
  • $\begingroup$ @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. $\endgroup$ – Mr.Wizard Feb 26 '13 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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