I'd probably have a second default parameter to control the levels of indentation (a static would have to be reset somehow if you called the function more than once in the program otherwise the indentation would be messed up in successive function calls). The function could be called (from main for example) without a specified second value which would default to 0, within the function itself recursive calls would be made by specifying this parameter (and incrementing by 1 each step).
Code:
unsigned long factorial(unsigned long n, unsigned int steps = 0)
{
...
// print out "steps" number of tab chars or something
// print out current "n"
...
// recursive call with second parameter
return n * factorial(n-1,steps+1);
}
int main()
{
// When calling function from within main, don't specify the second default parameter.
std::cout << factorial(4) << std::endl;
std::cout << factorial(5) << std::endl;
return 0;
}
You could use a static member, but I think you'd still need to use a default parameter to clear/reset the static value:
Code:
unsigned long factorial(unsigned long n, bool reset = true)
{
static unsigned steps;
// Either increment or reset static value based on second func arg
if( reset ) steps = 0;
else ++steps;
...
// print out "steps" number of tab chars or something
// print out current "n"
...
// recursive call with second function argument
return n * factorial(n-1,false);
}
int main()
{
// When calling function from within main, don't specify the second default parameter.
std::cout << factorial(4) << std::endl;
std::cout << factorial(5) << std::endl;
return 0;
}
As to what the "local" is that's being mentioned I have no idea.