Muzhawir Amri

Back

In this post, we’ll learn how to create variables in Elixir. Since Elixir is a functional programming language, the concept of variables is different from languages like C, mainly because Elixir data is immutable. By the end of this post, we’ll also understand how Elixir manages memory automatically.

Binding - Creating Variables#

Elixir is a dynamic language, so we don’t need to declare a data type when creating a variable. The type is determined automatically based on the given value.

The process of assigning a value to a variable is called binding, not assignment. When we write monthly_salary = 1000, we are binding the name monthly_salary to the value 1000. The variable now references the value, it doesn’t store or modify it directly. For example:

iex> monthly_salary = 1000
1000

iex> monthly_salary  ❸
1000
elixir

Explanation:

  • ❶ Binds the variable monthly_salary to 1000.
  • ❷ IEx returns the value 1000 as the result of the binding.
  • ❸ Verifies the value of the variable monthly_salary.
  • ❹ IEx returns 1000, which was previously bound to the variable.

Now, we can use this variable in other expressions, like:

iex> monthly_salary * 12
12000
elixir

Variable names in Elixir must begin with a lowercase letter (a-z) or an underscore (_). After that, they can contain a combination of letters, numbers (0-9), and underscores:

valid_variable_name
also_valid_1
validButNotRecommended

NotValid
elixir

Elixir also allows variable names to end with a question mark (?) to indicate a boolean value, and an exclamation mark (!) for variables that might raise an error. This is not mandatory, but it’s a recommended convention to make our code easier to read and understand:

valid_name?
also_ok!
elixir

Data Immutability in Elixir#

Because Elixir is a functional language, all data is immutable. This means that once a value is created, it cannot be changed.

If we want to “update” a value, we’re actually creating a new copy and storing it in a different memory location. The original data remains intact and unchanged.

Rebinding - Updating Variables#

Rebinding is the process of binding a variable to a new value without modifying the old value in memory. Here’s an example:

iex> monthly_salary = 1000
1000

iex> monthly_salary ❷
1000

iex> monthly_salary = 1500
1500

iex> monthly_salary ❹
1500
elixir

Explanation:

  • ❶ Binds the variable monthly_salary to 1000.
  • ❷ Verifies the value of monthly_salary.
  • ❸ Rebinds the variable to 1500.
  • ❹ Verifies the result after rebinding.

What actually happens is: the value 1000 still exists in memory, but monthly_salary now points to a new value, 1500, stored at a different memory location. The old value is neither overwritten nor immediately deleted.

You might be wondering: “Doesn’t memory usage keep increasing if every update creates a new copy?”, and that’s where garbage collection comes in.

Memory Management in Elixir#

After rebinding, the old value (1000) remains in memory for a while. But since there are no active references to it, it is considered unreachable.

Elixir runs a garbage collection process automatically, which detects and removes values that are no longer accessible. This way, we don’t need to manage memory manually, and the system remains efficient, even though all data is immutable.

References#

  • Saša Jurić. (2024). Elixir In Action (3rd ed.). Manning Publications.