Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 48 ยท Getting Started

Your First Elixir Program

Write your first Elixir program: print with IO.puts and IO.inspect, add comments, tell .ex from .exs, and see how modules and functions fit together.

Every language starts with "Hello, world", and Elixir's is a single line. In this lesson you will write it, learn why IO.puts and IO.inspect are different, and see two ideas that shape every Elixir program: everything is an expression that produces a value, and code lives in modules full of functions.

The Elixir code here is read-only on this site, so run it with elixir file.exs after installing Elixir. The TryIt blocks are JavaScript stand-ins that mimic what the Elixir functions print and return.

Hello, world

Save this as hello.exs:

ELIXIR
IO.puts("Hello, world!")

Run elixir hello.exs and you get:

TEXT
Hello, world!

IO is a module, a named collection of functions. puts is a function inside it (short for "put string"), and the dot joins them: IO.puts. The parentheses hold the arguments. The text in double quotes is a string.

Parentheses are optional when a call is unambiguous, so you will also see IO.puts "Hello". Most Elixir code, and the official formatter, keeps the parentheses; this tutorial does too.

Printing: puts, write and inspect

Elixir gives you a few ways to print, and they differ in what they show:

ELIXIR
IO.puts("one line")        # text plus a newline
IO.write("no newline")     # text only
IO.puts("")                # an empty line
IO.inspect({:ok, [1, 2]})  # a developer-friendly view of any value

IO.puts expects something that can become text: a string, a number or an atom. It prints the text and a newline. IO.write skips the newline. IO.inspect accepts any value and prints it the way you would write it in code, including quotes around strings and brackets around lists, so you can tell a string "42" from a number 42.

IO.inspect has one more trick that makes debugging easy: it returns the value it was given. You can drop it into the middle of a calculation without changing the result, and the optional label: tells you which print is which:

ELIXIR
total = IO.inspect(2 + 3, label: "sum") * 10
IO.puts(total)
TEXT
sum: 5
50

By contrast, IO.puts returns the atom :ok. In Elixir every function returns something. Printing is just a side effect that happens along the way.

JS Stand-in: puts, write and inspect

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Compare the lines for IO.puts("42") and IO.inspect("42"): the first shows the text, the second shows that the value is a string by adding the quotes.

Comments

A comment starts with # and runs to the end of the line. There is no block comment syntax:

ELIXIR
# This whole line is a comment
IO.puts("visible")  # and this one ends with a comment

Documentation is different: it uses @doc and @moduledoc attributes, and you will meet it in typespecs and documentation.

Everything is an expression

In many languages some constructs are statements that do something but have no value. In Elixir nearly everything is an expression: it evaluates to a value. A block of several expressions evaluates to the value of the last one:

ELIXIR
result =
  if 5 > 3 do
    "bigger"
  else
    "smaller"
  end

IO.puts(result)

Here if is an expression, so it can be assigned to result. Likewise a function's return value is just its last expression; there is no return keyword.

ELIXIR
value =
  (
    a = 10
    b = 20
    a + b
  )

IO.inspect(value)
# 30
JS Stand-in: a block returns its last value

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Modules and functions at a glance

Putting every line in a script file works for tiny programs, but real code is organized into modules. A module is created with defmodule, and public functions inside it with def:

ELIXIR
defmodule Greeter do
  def hello(name) do
    "Hello, #{name}!"
  end

  def hello_all(names) do
    Enum.each(names, fn name -> IO.puts(hello(name)) end)
  end
end

Greeter.hello_all(["Ana", "Bo"])
TEXT
Hello, Ana!
Hello, Bo!

Walk through it:

  • defmodule Greeter do ... end defines a module named Greeter. Module names start with a capital letter.
  • def hello(name) do ... end defines a function named hello that takes one argument. Its body is one expression, the string with #{name} interpolated.
  • hello_all calls hello directly (no module prefix is needed inside the same module) and loops with Enum.each, which calls an anonymous function (fn ... end) for every element.
  • Outside the module you call functions as Module.function(args).

Blocks open with do and close with end. Indentation is two spaces by convention but does not matter to the compiler. A later lesson on functions and modules covers private functions, defaults and the capture operator.

.ex versus .exs

Both extensions hold Elixir code, but they are used differently:

Extension Meaning Typical use
.exs Script: compiled in memory and run Quick programs, tests, config
.ex Source: compiled to .beam files Application code inside a Mix project

In a Mix project you put modules in lib/*.ex and tests in test/*_test.exs. For learning, .exs is the easiest: write, run, repeat.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does IO.puts return?

    Every Elixir function returns a value. IO.puts prints a line and returns :ok.
  2. 2. What is special about IO.inspect?

    Because IO.inspect returns its argument, you can insert it into a pipeline or calculation to debug without changing the result.
  3. 3. How do you write a single-line comment in Elixir?

    Comments start with # and run to the end of the line. Elixir has no block comment syntax.
  4. 4. What is the value of a function body with several expressions?

    A block evaluates to its last expression. Elixir has no return keyword.

Summary

  • IO.puts("text") prints a line; IO.write prints without a newline; IO.inspect shows any value and returns it.
  • Comments start with #.
  • Almost everything is an expression with a value; a block's value is its last expression.
  • Code is organized in modules (defmodule) with functions (def), called as Module.function(args).
  • Use .exs for scripts and .ex for compiled source.

Next: Basic Types: Numbers, Atoms and Booleans