- Erlang - Web Programming
- Erlang - Drivers
- Erlang - Performance
- Erlang - Concurrency
- Erlang - OTP
- Erlang - Distributed Programming
- Erlang - Ports
- Erlang - Databases
- Erlang - Emails
- Erlang - Processes
- Erlang - Funs
- Erlang - Binaries
- Erlang - BIFS
- Erlang - Guards
- Erlang - Pattern Matching
- Erlang - Preprocessors
- Erlang - Header Files
- Erlang - Macros
- Erlang - Exceptions
- Erlang - Records
- Erlang - Tuples
- Erlang - Maps
- Erlang - Atoms
- Erlang - File I/O
- Erlang - Lists
- Erlang - Strings
- Erlang - Numbers
- Erlang - Recursion
- Erlang - Modules
- Erlang - Functions
- Erlang - Decision Making
- Erlang - Loops
- Erlang - Operators
- Erlang - Variables
- Erlang - Data Types
- Erlang - Shell
- Erlang - Basic Syntax
- Erlang - Environment
- Erlang - Overview
- Erlang - Home
Erlang Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Erlang - Modules
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 functionapty pke 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).
Defining a Module
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 −
Syntax
-module(modulename)
Where, modulename is the name of the module. This has to be the first pne of the code in the module.
The following program shows an example of a module called helloworld.
Example
-module(helloworld). -export([start/0]). start() -> io:fwrite("Hello World").
The output of the above program is −
Output
Hello World
Module Attributes
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 −
Syntax
-Tag(Value)
An example of how the attribute can be used is shown in the following program −
Example
-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 −
Output
Hello World
Pre-built Attributes
Erlang has some pre-built attributes which can be attached to modules. Let’s take a look at them.
Export
The exports attribute will take a pst 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.
Syntax
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.
Example
-module(helloworld). -author("TutorialPoint"). -version("1.0"). -export([start/0]). start() -> io:fwrite("Hello World").
The output of the above program will be −
Output
Hello World
Import
The import attribute is used to import functions from another module to use it as local.
Syntax
-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.
Example
-module(helloworld). -import(io,[fwrite/1]). -export([start/0]). start() -> fwrite("Hello, world! ").
In the above code, we are using the import keyword to import the pbrary ‘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 −
Output
Hello, world!Advertisements