Freddy: Please don't use that code - using a global variable (static) in a recursive function is definitely not a good idea.
Also, it would break if you make a loop in main asking for multiple numbers, since the second time around, the variables that are expected to be zero are no longer zero, but whatever value they had left in them from last time.
It is possible (but not at all necessary) to use recursion to solve the problem, but it should not be done with static variables.
Also:
Code:
rev(n*-1);
...
if(zero_num==1)
rev_num*=-1;
Why not:
Code:
rev(-n);
if(zero_num==1)
rev_num = -rev_num;
(and "zero_num" is really a bad variable name here - calling it "negative" would be a better choice - also, it doesn't need to be a static for the solution to work).
For laughs: Here is a recursive reversing function. I don't think this is the solution the teacher is after, so I don't think I'm spoiling the actual search for a solution (as it requires TWO functions, which is one more than the task asks for):
Code:
int doreverse(int num, int& mul)
{
if (num > 9)
{
int tmp;
tmp = doreverse(num / 10, mul);
mul *= 10;
tmp += (num % 10) * mul;
return tmp;
}
return num;
}
int reverse(int num)
{
int mul = 1;
return doreverse(num, mul);
}
I'm intentionally ignoring negative numbers, as I don't think that was REALLY intended to be part of the task.
--
Mats