পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Tuesday, January 31, 2017

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 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