The Function’s Body Is a Block

The doend block is one way of grouping expressions and passing them to other code. They are used in module and named function definitions, control structures…any place in Elixir where code needs to be handled as an entity.

However, doend is not actually the underlying syntax. The actual syntax looks like this:

 
def​ double(n), ​do​: n * 2

You can pass multiple lines to do: by grouping them with parentheses.

 
def​ greet(greeting, name), ​do​: (
 
IO.puts greeting
 
IO.puts ​"How're you doing, #{name}?"
 
)

The doend form is just a lump of syntactic sugar—during compilation it is turned into the do: form. (And the do: form itself is nothing special; it is simply a term in a keyword list.) Typically people use ...

Get Programming 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.