English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A module is a set of functions recombined into a single name in a single file. In addition, all functions in Erlang must be defined in a module.
Most basic functions, such as arithmetic, logical, and boolean operators, are already available because the default module is loaded at runtime. Each other function defined in the module you are going to use needs to be used Module: Function (Arguments) to call it.
Using the module, you can declare two types of content: functions and attributes. Attributes are metadata that describe the module itself, such as its name, functions that should be visible to the outside world, the author of the code, etc. This metadata is very useful because it informs the compiler how to perform its work, and because it allows people to retrieve useful information from compiled code without having to refer to the source code.
The syntax for function declaration is as follows :
-module(modulename)
Here, modulename is the name of the module. This must be on the first line of the module code. The following program shows an example of a module called helloworld.
-module(helloworld). -export([start/0]). start() -> io:fwrite("Hello World").
The output result of the above code is -
Hello World
Module attributes define specific attributes of the module, which consist of tags and values.
The general syntax of attributes is -
-Tag(Value)
The following example program is used to demonstrate how module attributes are used -
-module(helloworld). -author("TutorialPoint"). -version("1.0"). -export([start/0]). start() -> io:fwrite("Hello World").
Program definition above2Custom attributes: author and version, respectively used to represent the author and version number of the program.
The output result of the above code is -
Hello World
Pre-built-in attributes
Erlang has some pre-built-in attributes that can be connected to modules, let's take a look.
Exported properties will export a list of function and parameter counts to other modules. It will define the module interface. We have seen this in the previous example.
export([FunctionName1/FunctionArity1,.,FunctionNameN/FunctionArityN])
Here,
FunctionName − This is the function name 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 result of the above code is -
Hello World
Import attributes are used to import functions from another module for local use
-import (modulename , [functionname/parameter]).
Here,
Modulename − This is the name of the module that needs to be imported
functionname/parameter − This is the function that needs to be imported in the module
-module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite("Hello, world!\n").
In the above code, we use the keyword 'import' to import the library 'io', specifying the import of the 'fwrite' function. So now, every time we call the fwrite function, we do not need to include the module name each time.
The output result of the above code is -
Hello, world!