Elixir Variables

Welcome to a tutorial on Variables in Elixir. Here you will learn about the different types of variables.

Commonly, a variable makes available a named storage that programs can manipulate. In Elixir, every variable has a specific type, that typically determines the size and layout of the variable's memory, as well as the range of values that can be stored within that memory with the set of operations that can be applied to the variable.

 

Types of Variables

Below are the basic types of variables supported by Elixir.

Integer

They are used for integer values and have a size of 32bit on a 32bit architecture and 64 bits on a 64-bit architecture. They are signed in Elixir. In the case where an integer starts to expand in size above its limit, Elixir convers it in a Big Integer which takes up memory ranging from 3 to n.

 

Floats

In Elixir, Floats have a precision of 64-bit. Although. they are like integers in terms of memory. Also, when defining a float, we can use exponential notation.

 

Boolean

This data type can take up 2 values which is either true or false.

 

Strings

In Elixir, Strings are utf-8 encoded. Also, they have a strings module which provides many functionality to the programmer to manipulate strings.

 

Anonymous Functions/Lambdas

These are functions that can be defined and assigned to a variable. Thereafter used to call this function.

 

Collections

In Elixir, there are lots of collection types available, such as Lists, Tuples, Maps, Binaries, and so on.

 

Variable Declaration

A variable declaration informs the interpreter where and how much to create the storage for the variable, however, Elixir does not permit us to just declare a variable. A variable must be declared and assigned a value instantly. E.g., to create a variable named life and assign it a value 28, you can do it as shown below.

life = 28

The code above will bind the variable life to the value 28. But, if we want to reassign this variable a new value, the syntax above can be also be used.

life = "Hello world"

 

Variable Naming

In Elixir, naming variables follow a snake_case convention, that is, all variables must start with a lowercase letter, and then followed by 0 or more letters (both upper and lower case), followed at the end by an optional '?' OR '!'.

Also, variable names can be started with a leading underscore but it must be used only when ignoring the variable, meaning that the variable will not be used again but is needed to be assigned to something else.

 

Printing Variables

In the interactive shell, variables will print if you just enter the variable name. Take for instance, when you create a variable as shown below:

life = 28

Then you enter 'life' in your shell, you'll get the output below:

28

Although, if you want to output a variable to the console (When running an external script from a file), but you need to provide the variable as input to IO.puts function:

life = 28 
IO.puts life 

or

life = 28 
IO.puts(life) 

And this will give you the output below:

28