Functions are grouped together in modules. A program will
often be spread across several modules, each containing functions that are
logically grouped together. Modules consist of files with the .erl suffix, where the
file and module names have to be the same. Modules are named using
so in Example 2-2, the
demo module would be stored in a file called
Example 2-2. A module example
-module(demo). -export([double/1]). % This is a comment. % Everything on a line after % is ignored. double(Value) -> times(Value, 2). times(X,Y) -> X*Y.
export directive contains a list of exported functions of the
Function/Arity. These functions are
global, meaning they can be called from outside the
module. And finally, comments in Erlang start with the percent symbol
%) and span to the end of the line.
Make sure you use them everywhere in your code!
Global calls, also called fully qualified function calls, are made
by prefixing the module name to the function. So, in Example 2-2, calling
demo:double(2) would return
4. Local functions can be called only from
within the module. Calling them by prefixing the call with the module name
will result in a runtime error. If you were wondering what
math:sqrt/1 did in Example 2-1, it calls the
sqrt (square root) function from the
math module, which comes as part of the
Erlang standard distribution.
Functions in Erlang are uniquely identified by their name, their arity, and the module in which they are defined. ...