by Example

Variables


Variables in Erlang are expressions. If a variable
is bound to a value, the return value is that value.




Variables start with an uppercase letter or underscore (_)
and may contain alphanumeric characters, underscore and @.


They are bound to values using pattern matching.
and a variable can only be bound once.












We can take advantage of pattern matching, to bound
multiple variables at one.






The underscore (_) represents a special kind of variable,
the anonymous variable. It can be used when a variable is
required but its value can be ignored.




-module(variables).
-compile(export_all).



showVariables() ->



    Name = "Joe Doe",
    io:format("Name: ~p ~n", [Name]),


    Age = 25,
    io:format("Age: ~p ~n", [Age]),


    IsMale = fun() -> true end,
    io:format("Male: ~p ~n", [IsMale()]),




    {A, [Head,Second|Rest]} = {1, [10,20,30,40]},
    io:format("A = ~p ~n", [A]),
    io:format("Head = ~p ~n", [Head]),
    io:format("Second = ~p ~n", [Second]),
    io:format("Rest = ~p ~n", [Rest]),




    _ = 1,
    _ = 2.
1> c(variables).
{ok,variables}

2> variables:showVariables().
Name: "Joe Doe"
Age: 25
Male: true
A = 1
Head = 10
Second = 20
Rest = [30,40]


Next: Constants