Erlang – Functions


Erlang – Functions


”;


Erlang is known as a functional programming language, hence you would expect to see a lot of emphasis on how functions work in Erlang. This chapter covers what all can be done with the functions in Erlang.

Defining a Function

The syntax of a function declaration is as follows −

Syntax

FunctionName(Pattern1… PatternN) ->
Body;

Where,

  • FunctionName − The function name is an atom.

  • Pattern1… PatternN − Each argument is a pattern. The number of arguments N is the arity of the function. A function is uniquely defined by the module name, function name, and arity. That is, two functions with the same name and in the same module, but with different arities are two different functions.

  • Body − A clause body consists of a sequence of expressions separated by comma (,):

The following program is a simple example of the use of functions −

Example

-module(helloworld). 
-export([add/2,start/0]). 

add(X,Y) -> 
   Z = X+Y, 
   io:fwrite("~w~n",[Z]). 
   
start() -> 
   add(5,6).

The following pointers should be noted about the above program −

  • We are defining two functions, one is called add which takes 2 parameters and the other is the start function.

  • Both functions are defined with the export function. If we don’t do this, we will not be able to use the function.

  • One function can be called inside another. Here we are calling the add function from the start function.

The output of the above program will be −

Output

11

Anonymous Functions

An anonymous function is a function, which has no name associated with it. Erlang has the facility to define anonymous functions. The following program is an example of an anonymous function.

Example

-module(helloworld). 
-export([start/0]). 

start() -> 
   Fn = fun() -> 
      io:fwrite("Anonymous Function") end, 
   Fn().

The following points need to be noted about the above example −

  • The anonymous function is defined with the fun() keyword.

  • The Function is assigned to a variable called Fn.

  • The Function is called via the variable name.

The output of the above program will be −

Output

Anonymous Function

Functions with Multiple Arguments

Erlang functions can be defined with zero or more parameters. Function overloading is also possible, wherein you can define a function with the same name multiple times, as long as they have different number of parameters.

In the following example, the function demo is defined with multiple arguments for each function definition.

Example

-module(helloworld). 
-export([add/2,add/3,start/0]). 

add(X,Y) -> 
   Z = X+Y, 
   io:fwrite("~w~n",[Z]). 
   
add(X,Y,Z) -> 
   A = X+Y+Z, 
   io:fwrite("~w~n",[A]). 
 
start() ->
   add(5,6), 
   add(5,6,6).

In the above program, we are defining the add function twice. But the definition of the first add function takes in two parameters and the second one takes in three parameters.

The output of the above program will be −

Output

11
17

Functions with Guard Sequences

Functions in Erlang also have the capability of having guard sequences. These are nothing but expressions which only when evaluated to true will cause the function to run.

The syntax of a function with a guard sequence is shown in the following program.

Syntax

FunctionName(Pattern1… PatternN) [when GuardSeq1]->
Body;

Where,

  • FunctionName − The function name is an atom.

  • Pattern1… PatternN − Each argument is a pattern. The number of arguments N is the arity of the function. A function is uniquely defined by the module name, function name, and arity. That is, two functions with the same name and in the same module, but with different arities are two different functions.

  • Body − A clause body consists of a sequence of expressions which are separated by a comma (,).

  • GuardSeq1 − This is the expression which gets evaluated when the function is called.

The following program is a simple example of the use of a function with a guard sequence.

Example

-module(helloworld). 
-export([add/1,start/0]). 

add(X) when X>3 -> 
   io:fwrite("~w~n",[X]). 

start() -> 
   add(4).

The output of the above program is −

Output

4

If the add function was called as add(3), the program will result in an error.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *