Learn/Elixir/2. Values and variables
Lesson 2 of 8
Sign in

Elixir, lesson 2

Values and variables

A program works with values: numbers, text, true and false. Elixir has a small set of basic types you will use all the time:

Atoms look odd at first. Think of them as labels: :ok, :error, :gold. Elixir uses them everywhere to tag results. true, false and nil are atoms too, written without the colon.

Binding names to values

You give a value a name with =:

Elixir calls this binding: the name price now points at the value 12. Names are written in : lowercase words joined by underscores, like or . A name must start with a lowercase letter or an underscore.

2 exercises to try what you just read.

Exercise 1

Ticket total

Press Ctrl Enter or Check to test your code.

Exercise 2

Bank balance

Press Ctrl Enter or Check to test your code.

snake_case
ticket_price
max_speed

Values never change

Here is the big idea of Elixir: data is immutable. Once the value 12 exists, nothing can change it. What you can do is bind the same name to a new value:

The second line did not modify 10. It computed a brand-new value, 15, and moved the name score over to it. This sounds like a small detail, but it means a value you hand to a function can never be changed behind your back. That makes programs much easier to reason about.

Because nothing mutates, Elixir has no const keyword. A name you never rebind already behaves like a constant. For values shared across a whole module, Elixir has module attributes, which you will meet in the functions lesson.

Putting values into text

To build a string from values, use interpolation: put any expression inside #{...} in a string.

You can also join two strings with <>, as in "Hello, " <> name. Both sides must already be strings, so interpolation is usually easier when numbers are involved.

Checking a type

Elixir figures out types for you, but you can ask. Functions like is_integer/1, is_float/1 and is_binary/1 (strings are binaries) return true or false. The /1 is part of a function's name in Elixir: it says how many arguments the function takes.

Further reading

  • Getting started: Basic types
  • Getting started: Pattern matching

A cinema sells tickets for 12 euros each, and every order pays a booking fee of 3 euros. The starter already binds these values. For an order of 4 tickets, print:

Compute the total from the variables instead of typing 51 yourself. The program reads no input.

Start a balance at 100. Print it, then rebind balance after a deposit of 50, print it, then rebind it again after a withdrawal of 30, and print it once more:

Use the same name balance the whole way through. The program reads no input.

42          # integer (whole number, any size)
3.75        # float (needs a digit on both sides of the dot)
"Ada"       # string (text, in double quotes)
true        # boolean (true or false)
:gold       # atom (a constant whose name is its value)
nil         # "no value"
price = 12
name = "Ada"
IO.puts(price * 2)   # 24
score = 10
score = score + 5
IO.puts(score)   # 15
item = "coffee"
cups = 3
IO.puts("#{cups} cups of #{item} cost #{cups * 4} euros")
# 3 cups of coffee cost 12 euros
IO.puts(is_integer(42))     # true
IO.puts(is_float(42))       # false
Tickets: 4
Total: 51
Start: 100
After deposit: 150
After withdrawal: 120