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 the do: syntax for single-line blocks, and doend for multiline ones.

This means our times example would probably be written as follows:

mm/times1.exs
 defmodule​ Times ​do
 def​ double(n), ​do​: n * 2
 end

We could even write it as

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

(but please don’t).