Function Calls and Pattern Matching
Understand how to match arguments with the parameter list of a specific function.
In the previous chapter, we looked at how anonymous functions use pattern matching. The same is true of named functions. The difference is that we write the function multiple times, each time with its own parameter list and body. Although this looks like multiple function definitions, it’s actually multiple clauses of the same definition.
When we call a named function, Elixir tries to match our arguments with the parameter list of the first definition (clause). If it can’t match them, it tries the next definition of the same function and checks to see if it matches. It continues until it runs out of candidates.
Get hands-on with 1400+ tech skills courses.