Dat: > The following appeared in chapter 3 of the Ruby's User Guide: > > def fact(n) > if n == 0 > 1 > else > n * fact(n-1) > end > end > > Is everything OK here? I can't see any problem here. We're calculating factorial in a normal imperative approach using recursion (which eats stack). Just don't call fact(-1) or with any negative argument (will eat you stack faster than you think, but *don't crash*, damn I like Ruby). Oh, and if you think why the code works, you can think it to be def fact(n) if n == 0 return 1 else return n * fact(n-1) end # puts n end The difference between the chapter 3 example and this code is that the latter will work even if you remove the comment character (but it won't print anything, if you happened to think so) whereas the chap 3 version won't. To amaze a little bit more (at least I was amazed with this), one can rewrite: def fact(n) ret = if n == 0 1 else n * fact(n-1) end puts n ret end Now we assign to ret the value of the whole if-else-end construct, which happens to be 1 or n*fact(n-1) depending of the if condition, then we print n and finally return the value of ret. If one doesn't say explicitly "return some_var" the return value of the function is the last evaluated value, that is the value of the last executed code. - Aleksi