In Elixir, a single function can have multiple clauses. This is achieved by pattern matching the function's arguments and by using guards.
# pattern matching the argument
def number(7) do
"Awesome, that's my favorite"
end
# using a guard
def number(n) when is_integer(n) do
"That's not my favorite"
end
def number(_n) do
"That's not even a number!"
end
FunctionClauseError
is raised by the BEAM VM._
otherwise a warning is emitted by the compiler.fn
13 -> "Awesome, that's my favorite"
_ -> "That's not my favorite"
end
Note that multiple clause functions should not be confused with function overloading that you might know from other programming languages. In Elixir, functions are identified by their name and arity only, not types of arguments (since there is no static typing). The function number/1
from the example is considered to be a single function regardless of how many clauses it has.