links: [[Functions in Elixir]] --- The use of pattern matching is dominant in assertive, idiomatic Elixir code. When using the match operator, if the pattern on the left matches the right, any variables on the left are bound, and the value of the right side is returned. A `matchError` is raised if there is no match ```elixir {_, denominator} = Float.ratio(0.25) # => {1, 4} # The variable `denominator` is bound to the value 4 ``` ## Pattern Matching in Named Functions Pattern matching is also a useful tool while creating multiple function clauses. Pattern matching can also be used on the function's arguments, which determines which function clause to invoke -- from the top of the file down until the first match. Variables maybe bound in a function head and used in the function body ```elixir defmodule Example do def named_function(:a = variable_a) do {variable_a, 1} end def named_function(:b = variable_b) do {variable_b, 2} end end Example.named_function(:a) # => {:a, 1} Example.named_function(:b) # => {:b, 2} Example.named_function(:c) # => ** (FunctionClauseError) no function clause matching in Example.named_function/1 ``` --- tags: #elixir #function #patterns sources: - [Exercism syllabus pattern matching](https://exercism.org/tracks/elixir/concepts/pattern-matching) -