Creating Anonymous Functions

You can think of functions as subprograms of your program. They receive an input, do some computation, and then return an output. The function body is where we write expressions to do a computation. The last expression value in the function body is the function’s output. Functions are useful for reusing expressions. Let’s start with a simple example in which we’ll build messages to say hello to Ana, John, and the world. Try typing this in your IEx:

 iex>​ ​"​​Hello, Mary!"
 "Hello, Mary!"
 iex>​ ​"​​Hello, John!"
 "Hello, John!"
 iex>​ ​"​​Hello, World!"
 "Hello, World!"

If we want to say hello to Alice and Mike, we could copy and paste the message and replace the names. But instead we can create a function ...

Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.