Hello world!

Every language tutorial starts the same way. In IEx, call the function IO.puts/1, which prints a line of text to the terminal:

iex> IO.puts("Hello world!")
Hello world!
:ok

You see two lines in the output:

  1. The text Hello world! is what IO.puts/1 printed.

  2. The atom :ok is the function’s return value. In Elixir, every expression returns a value, and IEx always shows it. Get used to seeing that :ok, it appears after almost every I/O call.

Strings use double quotes

Always wrap strings with double quotes. Single quotes create a charlist, an older Erlang data type that behaves very differently. If you need a double quote inside the string, escape it with a backslash:

iex> IO.puts("With double quotes: \"Hello world!\"")
With double quotes: "Hello world!"
:ok

Anatomy of IO.puts/1

Two things worth understanding in this one call:

  • IO is a module. Modules are how Elixir groups related functions together. You usually need to import or require a module before using it, but IO is so common that Elixir makes it available automatically.

  • puts/1 is a function. The /1 is the function’s arity, the number of arguments it takes. IO.puts/1 takes a single argument (the string to print). A module can have many functions with the same name as long as their arities differ, for example IO.puts/1 (print to standard output) and IO.puts/2 (print to a specific device).

We cover modules and functions in detail in modules and functions.