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:
-
The text Hello world! is what
IO.puts/1printed. -
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:
|
Anatomy of IO.puts/1
Two things worth understanding in this one call:
-
IOis a module. Modules are how Elixir groups related functions together. You usually need to import or require a module before using it, butIOis so common that Elixir makes it available automatically. -
puts/1is a function. The/1is the function’s arity, the number of arguments it takes.IO.puts/1takes a single argument (the string to print). A module can have many functions with the same name as long as their arities differ, for exampleIO.puts/1(print to standard output) andIO.puts/2(print to a specific device).
We cover modules and functions in detail in modules and functions.