From: usw wickramasinghe Date: 2009-03-12T04:43:28+09:00 Subject: Re: return doesn't always exit my method --000e0cd146142a5f8d0464dd1c10 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit On Wed, Mar 11, 2009 at 6:18 AM, Benjamin Thomas wrote: > Hi, I'm new to Ruby and programming in general. > I'm coding a little script to train on multiplication tables but > something's wrong: > > If I type 'exit', the function should normally exit, via a return > statement. But this does not always happen and I really can't understand > why. I've been scratching my head for a few hours on that so I'm posting > here hoping somebody will be able to spot my obvious mistake! > > I'm trying to apply here the concept of recursion so maybe this is where > I'm messing up. > > Thanks for your input, > Ben Obviously the flow in your pro gramme is due to the recursive nature of it.As correctly pointed out by Christopher , return in a recursive algorithm doesn't necessarily mean your returning from the function entry point you have started it. It does only mean that you have returned from a specific instance of the function (or a stack level one may call it..) and ONLY when u have returned from all the instances of the function in the flow , we can safely say function has finally returned it's control . So don't be fooled by a return statement in a recursive funtion as it only provide a way for you to return the control in the current stack .Also Always remember to provide a return (or termination logic) in the context of a recursive function which otherwise will lead to disasterous unending loops in the flow. Regards -Udayanga --000e0cd146142a5f8d0464dd1c10--