Erlang - Variables



In Erlang, all the variables are bound with the ‘=’ statement. All variables need to start with the upper case character. In other programming languages, the ‘=’ sign is used for the assignment, but not in the case of Erlang. As stated, variables are defined with the use of the ‘=’ statement.

One key thing to note in Erlang is that variables are immutable, which means that in order for the value of the variable to change, it needs to be destroyed and recreated again.

The following basic variables in Erlang are explained in the last chapter −

  • Numbers − This is used to represent an integer or a float. An example is 10.

  • Boolean − This represents a Boolean value which can either be true or false.

  • Bit String − A bit string is used to store an area of un-typed memory. An example is <<40,50>>.

  • Tuple − A tuple is a compound data type with a fixed number of terms. An example is {40,50}.

  • Map − A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. An example is {type=>person,age=>25}.

  • List − A list is a compound data type with a variable number of terms. An example is [40,40].

Variable Declarations

The general syntax of defining a variable is as follows −

Syntax

var-name = var-value

Where,

  • var-name − This is the name of the variable.

  • var-value − This is the value bound to the variable.

Following is an example of variable declaration −

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   X = 40, 
   Y = 50, 
   Result = X + Y, 
   io:fwrite("~w",[Result]).

In the above example, we have 2 variables, one is X which is bound to the value 40 and the next is Y which is bound to the value of 50. Another variable called Result is bound to the addition of X and Y.

The output of the above program will be −

Output

90

Naming Variables

As discussed, variable names have to start with uppercase. Let’s take an example of a variable declared in lower case.

Example

-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, you will get the following compile time error.

Output

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.

Example

-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.

Output

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'

Printing Variables

In this section we will discuss how to use the various functions of printing variables.

Using the io:fwrite function

You would have seen this (io:fwrite) used in all of the above programs. The fwrite function is part of the ‘io’ module or Erlang, which can be used to output the value of variables in the program.

The following example shows a few more parameters which can be used with the fwrite statement.

Example

-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 −

Output

40.000000
5.00000e+1

The following pointers should be noted about the above program.

  • ~ − This character symbolizes that some formatting needs to be carried out for the output.

  • ~f − The argument is a float which is written as [-]ddd.ddd, where the precision is the number of digits after the decimal point. The default precision is 6 and it cannot be less than 1.

  • ~n − This is to println to a new line.

  • ~e − The argument is a float which is written as [-]d.ddde+-ddd, where the precision is the number of digits written. The default precision is 6 and it cannot be less than 2.

Advertisements