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).
The syntax of a function declaration is as follows −
The following program shows an example of a module called helloworld.
The general syntax of an attribute is −
The output of the above program is −
The output of the above program will be −
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).
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 line 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 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.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!\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 −
Output
Hello, world!
No comments:
Post a Comment