From: Phillip Gawlowski Date: 2010-01-04T01:52:23+09:00 Subject: Re: Few clarifications on recursion On 03.01.2010 17:16, Panagiotis Atmatzidis wrote: > Although it introduces many interesting concepts to me (i. e. I did not knew that after the "if" statement a single _1_ with no quotes could be used) I fail to see how the num changes value. The way I read the code here: > > num * factorial(num-1) > > is 30*29 for "puts factorial(30)" not 30*29*28... which is how it works. I've tested the code works fine. > > The book although, in my opinion is excellent (so far) for starters, does not explain - or it does but I did not understand it nevertheless - how this piece of code works. In a nutshell, recursion means performing the same action until a condition is met. In your case, factorial does "num * (num - 1)" until num is 1 (or smaller than one). It then gives you the result. You can test this with: def factorial num if num < 0 puts "You can't take the factoria of a negative number" end puts "num is: #{num}" # this prints our the value of num before each # recursion if num <= 1 1 else num * factorial(num-1) end end puts factorial 30 I hope that clears up your confusing about recursion? (If that *was* where you were confused..) -- Phillip Gawlowski