Modules are a bunch of functions regrouped in a single file, under a single name. Additionally, all functions in Erlang must be defined in modules.
Most of the basic functionality like arithmetic, logic and Boolean operators are already available because the default modules are loaded when a program is run. Every other function defined in a module you will ever use needs to be called with the form Module:Function (Arguments).
With a module, you can declare two kinds of things: functions and attributes. Attributes are metadata describing the module itself such as its name, the functions that should be visible to the outside world, the author of the code, and so on. This kind of metadata is useful because it gives hints to the compiler on how it should do its job, and also because it lets people retrieve useful information from compiled code without having to consult the source.
The syntax of a function declaration is as follows −
-module(modulename)
Where, modulename is the name of the module. This has to be the first line of the code in the module.
The following program shows an example of a module called helloworld.
-module(helloworld). -export([start/0]). start() -> io:fwrite("Hello World").
The output of the above program is −
Hello World
A module attribute defines a certain property of a module. A module attribute consists of a tag and a value.
The general syntax of an attribute is −
-Tag(Value)
An example of how the attribute can be used is shown in the following program −
-module(helloworld). -author("TutorialPoint"). -version("1.0"). -export([start/0]). start() -> io:fwrite("Hello World").
The above program defines 2 custom attributes called author and version which contains the program author and program version number respectively.
The output of the above program is −
Hello World
Erlang has some pre-built attributes which can be attached to modules. Let’s take a look at them.
The exports attribute will take a list of functions and arity to export for consumption by other modules. It will define the module interface. We have already seen this in all of our previous examples.
export([FunctionName1/FunctionArity1,.,FunctionNameN/FunctionArityN])
Where,
FunctionName − This is the name of the function in the program.
FunctionArity − This is the number of parameters associated with the function.
-module(helloworld). -author("TutorialPoint"). -version("1.0"). -export([start/0]). start() -> io:fwrite("Hello World").
The output of the above program will be −
Hello World
The import attribute is used to import functions from another module to use it as local.
-import (modulename , [functionname/parameter]).
Where,
Modulename − This is the name of the module which needs to be imported.
functionname/parameter − the function in the module which needs to be imported.
-module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite("Hello, world!\n").
In the above code, we are using the import keyword to import the library ‘io’ and specifically the fwrite function. So, now whenever we invoke the fwrite function, we don’t have to mention the io module name everywhere.
The output of the above program will be −
Hello, world!