O'Reilly logo

Programming Elixir by Dave Thomas

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6Modules and Named Functions

  • Modules, the basic unit of code

  • Defining public and private named functions

  • Guard clauses

  • Module directives and attributes

  • Calling functions in Erlang modules

Once a program grows beyond a couple of lines, you’ll want to structure it. Elixir makes this easy. You break your code into named functions and organize these functions into modules. In fact, in Elixir named functions must be written inside modules.

Let’s look at a simple example. Navigate to a working directory and create an Elixir source file called times.exs.

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

Here we have a module named Times. It contains a single function, double. Because our function takes a single ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required