Reputation: 9
I made a function that computes the sum of digits of an integer. Trying to make the code shorter, I put in comment the if statement to see if it would still work so I can remove it but I get a StackOverflowError
, why?
This is my code:
public static int sumDigits(int n) {
//if(n%10==n){ return n;}
return n%10+sumDigits(n/10);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(sumDigits(12611));
}
Upvotes: 1
Views: 177
Reputation: 140
The statement that you commented out was the base case for this recursive function. Without a base case this function will loop infinitely.
When you work out the example in the main method you get:
sumDigits(12611)
= 1 + sumDigits(1261)
= 1 + ( 1 + sumDigits(126))
= 1 + ( 1 + (6 + sumDigits(12)))
= 1 + ( 1 + (6 + ( 2 + sumDigits(1))))
= 1 + ( 1 + (6 + ( 2 + ( 1 + sumDigits(0))))) //at this point the commented out if statement would have returned
= 1 + ( 1 + (6 + ( 2 + ( 1 + ( 0 + sumDigits(0))))))
= 1 + ( 1 + (6 + ( 2 + ( 1 + ( 0 + ( 0 + sumDigits(0)))))))
...
At this point the program is stuck in an infinite loop. The commented out statement would have returned when n was 1, thus preventing this situation.
Upvotes: 2
Reputation: 881113
A recursive function can be defined as expressing an operation as a function of that operation on a value closer to an end point.
Hence, every recursive function needs an end condition at some point, otherwise it will recur infinitely (or, more correctly, until you blow your stack).
The basic definition of the "sum of digits" recursive method (for non-negative values of n
) is:
def sumOfDigits (n):
if n < 10:
return n
return (n % 10) + sumOfDigits (n / 10) # assumes integer division.
That first bit, the end condition, is very important, and you seem to have commented yours out for some reason.
Upvotes: 3
Reputation: 18609
stackover flow error occurs whenever the address stack in the memory allocated for the program can not store any new address.
so when you recursively call sumDigits()
the system keep on saving the last tracked in LIFO manner. so that it becomes easy for the system to get back to the previously address, which is must for the recursion.
If you do recursion infinitely or above the memory constraints, you will suffer stackOverflow error.
Upvotes: 2
Reputation: 5764
Your recursion does not have a terminating condition. Else, you call the recursive function over and over again, eventually the stack
will overflow
Upvotes: 1
Reputation: 16474
It never returns, but just recurses deeper and deeper.
You do have a return statement, but before returning it needs to compute the value of the expression n%10+sumDigits(n/10)
which involves infinite recursion.
NB: Each time you call the function from itself, the function's context (local variables) are added on top of the stack. The stack has a limited size. Eventually you reach that size and StackOverflowError is the error that's thrown in that condition.
Upvotes: 4
Reputation: 115328
Because you are calling sumDigits()
from itself and the code that should cause you to return from infinite recursion is commented out. Uncomment line if(n%10==n){ return n;}
Upvotes: 1
Reputation: 4624
That's because there is no stop clause. You will invoke sumDigits
forever.
Upvotes: 3