Archive for March 19, 2011

Introduction to Erlang : Declaring Functions

This entry is part 7 of 16 in the series Introduction to Erlang

Functions

As you know by now, Erlang is a functional programming language. As this suggests, functions are the basic “ingredient” of an Erlang program. In my point of view, different programming paradigms pose different problem solving philosophy:

  • Procedural: describe the steps needed to be taken to solve the problem
  • Object-orientation: design the objects that will lead you to the solution
  • Logical (Declarative): describe the problem properly and let the language solve it
  • Functional: define small and precise functions that alltogether solve the problem

With this in mind, lets continue on how to declare a function (in a module).

Examples

While introducing functions, I will use several examples that implement list functions, although there are built-ins (BIFs) that implement the same functionality. The reason I will do so is that most of these functions are small, easy to understand, and operate on lists; one of the most, if not the most, important type in Erlang.

Declaring a Function

A simple function declaration has the following format:

function_name(Argument1, Argument2, ...) ->
    Statement1,
    Statement2,
    ... .

Where a statement can be another function call, an assignement, a comparison, a control statement (if for example), or a statement called for its side effects.
Read the rest of this entry »