elixir variables
Elixir Variables
In Elixir, variables are used to store and manipulate data. Here are some key points about Elixir variables:
- Variables in Elixir are created by assigning a value to a name using the
=
operator. - Elixir variables are dynamically typed, meaning they can hold values of any type.
- Variable names in Elixir start with a lowercase letter or an underscore, followed by any combination of lowercase letters, digits, and underscores.
- Elixir variables are immutable, which means their values cannot be changed once assigned. Instead, when a variable is "modified," a new variable with the updated value is created.
- Elixir uses pattern matching to assign values to variables. For example,
x = 10
assigns the value10
to the variablex
. - Variables in Elixir are lexically scoped, meaning they are only accessible within the scope in which they are defined.
Here's an example of using variables in Elixir:
x = 10
y = 20
sum = x + y
IO.puts("The sum of #{x} and #{y} is #{sum}")
In this example, the variables x
and y
are assigned the values 10
and 20
, respectively. The variable sum
is then assigned the result of adding x
and y
. Finally, the IO.puts
function is used to print the sum.
Please let me know if you have any further questions.