Create and Use Erlang Modules and Functions

How to Reuse Ericsson Erlang Code in Multiple Applications

© Mark Alexander Bain

Aug 13, 2009
Create and Use Erlang Modules and Functions, Mark Alexander Bain
Rather that typing in the same code everytime that they start a new shell, the Erlang programmer can store all of their functions in a module and then use and reuse their

The article Get Started with the Erlang Programming Language is a brief introduction to Erlang and discusses how to:

  • obtain and install Erlang
  • run the Erlang shell
  • enter and evaluate simple statements

However, there is a limitation to that, and it's the fact that all of the code has to be retyped every time that a shell is started. Fortunately the solution is quite simple, and that's to:

  • create an Erlang module
  • populate the module with Erlang functions
  • load the module into an Erlang shell
  • use the Erlang functions to carry out the required operations

The first step is, therefore, to create an Erlang module.

Creating an Erlang Module

An Ericsson Erlang module is, quite simply, a text file. And the minimal module always contains a module header:

-module(circle).

Here the module is to be named "circle". The only constraints at this point are that:

  • the module name must be the same as the file name
  • the file must have a ".erl" extension

In this example, therefore, the file name will be "circle.erl".

Creating an Erlang Function

The next stage is to add functionality to the module. This will consist of a number of functions that the programmer will call from the Erlang shell once the module has been loaded. The function definition consists of two steps:

  • any functions to be made public must be exported. If they are not exported then then will only be used from within the module
  • the function is defined after the export statement

The export statement must always come before the function definitions, for example:

-export([circumference/1]).
pi() -> 3.14159265358979.
circumference(R) -> 2 * pi()* R.

In this example:

  • only the circumference function is to be exported and it will expect 1 input variable
  • the "pi" function requires no inputs but can only be used within the module (it will be a private function)
  • the circumference function with return the circumference of a circle when supplied with its radius

Once this code has been saved into "circle.erl" then it is ready for use in the Erlang shell.

Loading an Erlang Module

The module can no be accessed if:

  • erl is started from the directory where the module is located
  • the module is stored in erl's "start in" directory

It can then be loaded by using the c method:

c (circle).

Remembering, of course, that every line must be terminated with a full stop (or period). This will load the module (as seen in figure 1 at the bottom of this article), and then any exported functions will be available to the user.

Using Functions from an Erlang Module

With the module loaded, the programmer can access its functions, for example:

circle:circumference (2.5).

This (as can be seen in figure 2) will return 15.70796326794895, but any attempt to access the pi function will result in a error (because that is private and can only be used by the circumference function). The programmer can now go on to create more complex Erlang applications and will be able to reuse their code wherever it is required.


The copyright of the article Create and Use Erlang Modules and Functions in Computer Programming Tutorials is owned by Mark Alexander Bain. Permission to republish Create and Use Erlang Modules and Functions in print or online must be granted by the author in writing.


Create and Use Erlang Modules and Functions, Mark Alexander Bain
Figure 1: Loading an Ericsson Erlang Module, Mark Alexander Bain
Figure 2: Simple Erlang Evaluation., Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo