# How to suppress printing statments in function call

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.

• 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.) – Szabolcs Oct 3 '17 at 8:45
• Thanks for the advice and editing of my post! – Jann Oct 3 '17 at 8:49
• 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. – masterxilo Oct 3 '17 at 23:38

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.

• 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. – Jann Oct 3 '17 at 8:55
• @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. – J. M.'s technical difficulties Oct 3 '17 at 14:06