Simple Functions
Explore how to define and use simple anonymous functions in Elixir using the fn keyword. Understand invoking functions with parameters and the role of pattern matching in function calls. Gain hands-on experience by practicing function creation and invocation.
We'll cover the following...
Elixir is a functional language, so it’s no surprise that functions are a basic type.
An anonymous function is created using the fn keyword: fn parameter-list -> body end.
Think of fn...end as being a bit like the quotes that surround a string literal, except here, we’re returning a function as a value, not a string. We can pass that function value to other functions. We can also invoke it, passing in arguments.
At its simplest, a function has a parameter list and a body, which are separated by ...