I creating a factorial function in Elixir:
def factorial(0), do: 1
def factorial(n) when n > 0, do: n * factorial(n - 1)
First of all, I love how straightforward and elegant the solution is in Elixir. But, there is one thing that I do not understand. On the second line, I used this code: do: n * factorial(n - 1). Say we give the function an argument of 5. that line would look like this. do: 5 * factorial(5 - 1) so why doesn't the answer just come out to this: 20? Basically my question is. How does it know to continue to the base case? We aren't explicitly telling the program to continue until 0, are we? If someone could break this down for that would be awesome!