1
$\begingroup$

I defined a function in Mathematica using Module similar to

function[a0_,b0_]:=Module[{a, b},
  a=a0;
  b=b0;
  subFunction[a,b];
  ...
]

Both function and subFunction (and possible subSubFunctions) contain Print[] statements.

My question: How can I call function[] and at the same time suppress all Print[] statements. I don't want to get the output of these Print[] statements. This should be done without changing the definitions of the functions.

$\endgroup$
  • 1
    $\begingroup$ Function is a builtin symbol. You can't name your own function Function. Generally, try not to start your own functions with capital letters to avoid such conflicts. (I know that it was just an example.) $\endgroup$ – Szabolcs Oct 3 '17 at 8:45
  • $\begingroup$ Thanks for the advice and editing of my post! $\endgroup$ – Jann Oct 3 '17 at 8:49
  • $\begingroup$ This question is similar to stackoverflow.com/questions/8698754/… In addition to the answer given below, try using the On, Off, Message idiom. I think some built-in functions have symbol::verbose defined that you can turn On in order to see some internal progress messages. $\endgroup$ – masterxilo Oct 3 '17 at 23:38
2
$\begingroup$

You can use

Block[{Print},
  function[]
]

A better solution is not to use Print. A common debugging technique is to use a myPrint function instead of Print for printing debugging messages. If this function is not defined, it's as if it weren't there. Nothing will be printed. If you want to turn on debugging, then just myPrint = Print. If you want to turn on debugging only temporarily, then Block[{myPrint = Print}, function[]].

Don't forget that Mathematica as a built-in debugger.

|improve this answer|||||
$\endgroup$
  • $\begingroup$ Thank you very much! That was exactly what I was looking for. The Print statements I used were not meant for debugging but rather for expressing what function is doing. However I am not interested in these details when calling function several times just for getting the desired output. $\endgroup$ – Jann Oct 3 '17 at 8:55
  • 1
    $\begingroup$ @goe, as a suggestion for future development: you might want to implement your future functions to have an option (or optional argument) that would allow you to switch between simple and verbose modes, so that you won't have to resort to trickery like this. $\endgroup$ – J. M.'s technical difficulties Oct 3 '17 at 14:06

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.