Elixir matches a function call to its body based on the value of its arguments.
defmodule Math do def factorial(0): do: 1 def factorial(n): do: n * factorial(n - 1) end
Here, factorial of positive numbers matches the second clause, while factorial(0)
matches the first. (ignoring negative numbers for the sake of simplicity). Elixir tries to match the functions from top to bottom. If the second function is written above the first, we will an unexpected result as it goes to an endless recursion. Because factorial(0)
matches to factorial(n)