← Back to lesson 02: Let the code speak

Let the code speak: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Open IEx, enter an expression, and tell code, results, and errors apart.

Open IEx, run 2 + 3 and String.upcase("cat"), then compare each input with the value IEx returns.

Copy into IEx and press Enter after each line

Run one line at a time. Guess the result before you press Enter.

Original full example
# An addition expression returns a number
2 + 3

# A function call returns new text
String.upcase("cat")

# IO.puts prints a line on the screen
IO.puts("Hello, BEAM")

2 + 3 returns 5.

String.upcase("cat") returns "CAT".

IO.puts("Hello, BEAM") prints the text, then IEx shows :ok. For now, read it as a “done” label.

Steps for the full example

  1. In 2 + 3, + adds the numbers on its left and right.
  2. String.upcase("cat") gives the string to the upcase function in the String module.
  3. IO.puts("Hello, BEAM") creates two visible results: the printed text and the return value :ok shown by IEx.

Original lesson notes

IEx is Elixir's interactive window. You enter a small piece of code, and it answers at once. You do not need a project or a long list of commands yet.

An expression is code that produces a result. 2 + 3 produces 5. String.upcase("cat") produces "CAT".

When you see an error, pause. Read the last few lines. Look for a name the computer does not know or data that does not fit. An error is not a lost point. It is another kind of result.

Symbols and terms
iex
Enter this in a terminal to open IEx.
iex>
This is the prompt. Enter only the code after it. Do not copy the prompt.
#
This line is a comment. Elixir does not run it.
IEx
Short for Interactive Elixir. It is like scratch paper for trying one or two lines of code.
expression
Code that produces a value when it runs. A number, an operation, or a function call can all be expressions.
return value
The result an expression gives back when it finishes. It does not always print a sentence.

Original full exercise

Ask IEx to multiply 7 by 6, then turn beam into uppercase text.

Exercise starter
# Replace the question mark with an operator
7 ? 6

# Finish the function name
String.____("beam")

First you should see 42, then "BEAM".

Show hint

Multiplication uses *. The function that made text uppercase was upcase.

Show original answer
Reference answer
# The star means multiplication
7 * 6

# upcase returns a new uppercase string
String.upcase("beam")
Is iex> part of the code?

No. It is the prompt IEx shows while it waits for your code.

Original lesson takeaways
Design trade-offs and language comparisons
Original course references