How to avoid writing more code in a function?
Good afternoon,
Consider the following function:
Code:
double calculate(){
double ret;
for (int a = 0; a<=10; a++){
ret = 2*a;
}
return ret;
}
This is just a hypothetical function where a variable (here, ret) constantly changes its value.
Suppose I need to print the steps of the function; for example, print every value of ret, and put everything between parentheses:
Code:
double calculate(){
double ret;
printf("(");
for (int a = 0; a<=10; a++){
ret = 2*a;
printf("%d", ret);
}
printf(")");
return ret;
}
(codes not tested in a compiler)
So, in a few words, I have a function that makes calculations, and I want to output the result of each step of the calculation to a file. But the problem is that I don't want to "fill" the function with a lot of output functions, like sprintf, printf, fprintf... I just want the function to have the calculation procedure; otherwise, if I need to use the function somewhere else and didn't need the outputs, I would have to erase them all.
Any idea on how I could track and output the steps of the function and keep it "clean" from lines that output to screen/files?
Tell me if I'm not being very clear.
Thank you in advance.