wood burning stoves 2.0
The moose likes Java in General and the fly likes Problem to exit a procedure Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Problem to exit a procedure" Watch "Problem to exit a procedure" New topic

Problem to exit a procedure

Komlavi Ekouevi

Joined: Sep 14, 2011
Posts: 3
I have a recusrive function and I am trying to exit with the return statement but for some reasons the return statement continue to call the recursive function. Here it is how my function looks like

public void Solve ()
if ( Stopping Condition )


How can I exit this function? Thanks
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

JsuperJeeck Koffi wrote:for some reasons the return statement continue to call the recursive function.

No, that isn't what is happening. A return statement doesn't call any methods. What is happening is, your "Stopping Condition" is never true.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46410
Welcome to the Ranch

A recursion is usually only useful if it returns a value. The calling method uses that value. Look at this factorial function, where the method calls itself.

Also a recursion should have a base case. That means if a certain value is found (in the case of factorial, 0), a predetermined result is returned, in this case 1. The recursion should converge towards its base case, otherwise it is liable to go on for ever (or at least until it exhausts your stack space or memory available).
Note there are also exclusions; negative values don't have factorials, and values > 21 (I think) give arithmetic overflow. Hence the Exceptions.
Note the letter L to make the 1 into a long.
Consider Paul's rocket mass heater.
subject: Problem to exit a procedure
It's not a secret anymore!