A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Hello World in Erlang page! Here, you'll find the source code for this program as well as a description of how the program works.
-module(hello_world).
-export([start/0]).
start() ->
io:format("Hello, World!~n").
Hello World in Erlang was written by:
This article was written by:
If you see anything you'd like to change or update, please consider contributing.
Erlang looks scary when you first look at it, so we'll show the full program and then we'll break it down into parts to fully describe it.
The first key part of an Erlang program is the -module().
preprocessor directive:
-module(hello_world).
Every Erlang file must start with this directive or you'll get a compiler error like the following:
file.erl:2: no module definition
Next, to use functions from the module we've written we have to export them explicitly.
-export([start/0]).
This exports our start
function, it takes no arguments so we reference the function as start/0
. The number of arguments is called the "arity" of the function.
Functions in Erlang start with an atom (for now, think of these as just lowercase letters + underscores), then the parameters, followed by an arrow ->
. The following functions are both valid:
my_function() ->
ok.
myfunction() ->
ok.
Our start
function only does one thing for this simple program, it calls the format
function from the io
module to print characters to standard output by default. io:format()
. The string "Hello world!~n"
includes the newline control sequence ~n
- you can see a list of control sequences available for use in the documentation for io:fwrite
here (scroll down to "Available control sequences").
To run this example, you need to follow a few steps:
erlc hello_world.erl
in your terminal to compile the program to a BEAM file - this results in the file hello_world.beam
erl
.l(hello_world).
- remember to end with a period! This is to load a module into the shellhello_world:start().
- you should see Hello, World!
printed to the shell.q().
or Ctrl-C, a (abort).