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([main/1]).
main(_) ->
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([main/1]).
This exports our main
function, it takes one argument so we reference the function as main/1
. 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 main
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, just run escript hello_world.erl
.