Create and Use Erlang Modules and Functions

in #utopian-io7 years ago

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 tutorial 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, 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 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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  


Bu videoyu ne zaman çektirdin. Bu kadar becerikli olduğunu bilmiyordum

Your contribution cannot be approved yet. In your tutorial you state that this tutorial will teach:

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

This could not be found. Also, you need to include :

  • what is erland and why is it used?
  • what is c method.
  • Also, please make the tutorial more descriptive and improve the detail.

See the Utopian Rules. Please edit your contribution to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 98646.90
ETH 3511.62
USDT 1.00
SBD 2.98