Erlang Language Getting started with Erlang Language Hello World

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

There are two things you will need to know when writing a "hello world" application in Erlang:

  1. The source code is written in the erlang programming language using the text editor of your choice
  2. The application is then executed in the erlang virtual machine. In this example we will interact with the erlang VM thorugh the erlang shell.

First the application source code:

Create a new file hello.erl containing the following:

-module(hello).
-export([hello_world/0]).

hello_world() ->
  io:format("Hello, World!~n", []).

Let's have a quick look at what this means:

  • -module(hello). All erlang functions exists inside a module. Modules are then used to build applications, which are a collection of modules. This first line is to identify this module, namely hello. Modules can be compared to Java's packages
  • -export([hello_world/0]). Tells the compiler which functions to make "public" (when compared to OO languages), and the arity of the relevant function. The arity is the number of arguments the function takes. Since in erlang a function with 1 argument is seen as a different function than one with 2 arguments even though the name may be exactly the same. Ie, hello_world/0 is a completely different function than hello_world/1 for example.
  • hello_world() This is the name of the function. The -> indicates the transitioning to the implementation (body) of the function. This can be read as "hello_world() is defined as ...". Take note that hello_world() (no arguments) is identified by hello_world/0 in the VM, and hello_world(Some_Arg) as hello_world/1.
  • io:format("Hello, World!~n", []) From module io, the function format/2 function is called, which is the function for standard output. ~n is a format specifier that means print a new line. The [] is a list of variables to print indicated by format specifiers in the output string, which is in this case nothing.
  • All erlang statements must end with a . (dot).

In Erlang, the result of the last statement in a function is returned.

Now, let's run our application:

Start the erlang shell from same directory as the file hello.erl file:

$ erl

You should get a prompt that looks something like this (your version may be different):

Eshell V8.0  (abort with ^G)
1>

Now enter the following commands:

1> c(hello).
{ok,hello}
2> hello:hello_world().
Hello, World!
ok

Let's go through each line one by one:

  • c(hello) - this command calls the function c on an atom hello. This effectively tells Erlang to find the file hello.erl, compile it into a module (a file named hello.beam will be generated in the directory) and load it into the environment.
  • {ok, hello} - this is the result of calling the function c above. It is a tuple containing an atom ok and an atom hello. Erlang functions usually return either {ok, Something} or {error, Reason}.
  • hello:hello_world() - this calls a function hello_world() from the module hello.
  • Hello, World! - this is what our function prints.
  • ok - this is what our function returned. Since Erlang is a functional programming language, every function returns something. In our case, even though we didn't return anything in hello_world(), the last call in that function was to io:format(...) and that function returned ok, which is in turn what our function returned.


Got any Erlang Language Question?