Is the first way the "Ruby way" (how I wrote it) and the second more functional/the Golang way (from a Golang video tutorial)?
(Also, really warming up to my new Vim colorscheme...) https://octodon.social/media/1wicQXUJE9DFWHtu_rM
@schlink Hm. I dunno much about Golang, but in Ruby I think I'd write:
def factorial(num):
if num <= 1:
return 1
end
num * factorial(num - 1)
end
@noelle huh. I was taught not to have `return` statements early in methods (unless error checking), which I first resisted but eventually came around to as it can improve readability if you're consistent about it.
I'm actually not 100% how your method works!! I'd have thought it would always return a 1...
@noelle I at first thought
return n * factorial(n-1)
was the sleekest Ruby solution but I'm getting a "stack level too deep" error when I run it.
@noelle oh I see-- you were showing that I could use the `return` keyword. The only reason I used it in the first screenshot is I think Go requires it. I'm all for implicit returns in Ruby!
So `return 1` is basically a loop break out of your method. That's interesting and new to me! Thanks!
@schlink Because it's not stopping when you hit 1. :)