In the previous post, we explained compound pattern matching. Today we move to pattern matching in functions. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.
Pattern matching feature shows true power when combined with functions. c++ has a great feature called function overloading. You can have a function with the same name but with different input argument types:
int print(std::string s);
int print(double dvalue);
int print(double dvalue, int prec);
Pattern matching in Elixir functions takes a step forward. In the above screenshot, it is clear that the function argument is a pattern. This leads us to exciting multiclause functions where we could have several function definitions for different argument values. The result is less code that is more conscious.
- how to call lambda function
- function arguments are patterns
- you will see a lot of function pattern match errors that indicate a bug in your code because you did not cover user input for your function.