English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Erlang, all variables are bound to the '=' statement. All variables must start with an uppercase letter. In other programming languages, the '=' symbol is used for assignment but is not applicable to Erlang. As mentioned before, variables are defined by using the '=' statement.
Something to pay attention to in Erlang is that variables are immutable, which means that in order to change the value of a variable, it needs to be destroyed and recreated.
The previous chapter introduced the following basic variables in Erlang-
Numbers- Used to represent integers or floating-point numbers. For example:10.
Boolean - This represents a boolean value, which can be true or false.
Bit String- Bit strings are used to store untyped memory regions. For example: << 40,50 >>.
Tuple- Tuples are composite data types with a fixed number of items. For example: {40,50}.
Map- Maps are composite data types with a variable number of keys-- Value-associated composite data types. Each key-value association in a map is called an association pair. For example: {type => person, age => 25}.
List- Lists are composite data types with a variable number of items. For example: [40,40].
The general syntax for defining variables is as follows-
var-name = var-value
Where,
var-name - This is the name of the variable.
var-value - This is the value bound to the variable.
The following are examples of variable declarations-
-module(helloworld). -export([start/0]). start() -> X = 40, Y = 50, Result = X + Y, io:fwrite("~w",[Result]).
In the above example, we have2a variable, one is bound to a value40's X, and the other is bound to a value50's Y. Another variable named Result is bound to the addition of X and Y.
The output of the above program will be
90
As mentioned above, variable names must start with an uppercase letter. Let's take an example of a variable declared in lowercase.
-module(helloworld). -export([start/0]). start() -> X = 40, Y = 50, result = X + Y, io:fwrite("~w",[Result]).
If you try to compile the above program, the following compile-time error will occur.
helloworld.erl:8: variable 'Result' is unbound
Secondly, all variables can only be assigned once. Let's take an example of assigning a variable more than once.
-module(helloworld). -export([start/0]). start() -> X = 40, Y = 50, X = 60, io:fwrite("~w",[X]).
If you try to compile the above program, you will receive the following compile-time error.
helloworld.erl:6: Warning: variable 'Y' is unused helloworld.erl:7: Warning: no clause will ever match helloworld.erl:7: Warning: the guard for this clause evaluates to 'false'
In this section, we will discuss how to use various functions of printing variables.
You should have already seen this (io:fwrite) in all the above programs.fwriteFunctions are part of the 'io' module or Erlang itself and can be used to output the value of variables in a program.
The following examples show more parameters that can be used with the fwrite statement.
-module(helloworld). -export([start/0]). start() -> X = 40.00, Y = 50.00, io:fwrite("~f~n",[X]), io:fwrite("~e",[Y]).
The output of the above program will be-
40.000000 5.00000e+1
For the above program, the following points should be noted.
~ −This character indicates that some formatting of the output is required.
~f−The parameter is a floating-point number, written as [-] ddd.ddd, where precision is the number of decimal places. The default precision is6and cannot be less than1.
~n−printlnThis prints to a new line.
~e−The parameter is a floating-point number, written as [-] d.ddde + -ddd, where precision is the number of digits to be written. The default precision is6and cannot be less than2.