Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1f65b9e

Browse files
author
Bryan Enders
committed
Clarify Getting Started 8. Modules
If the previous instructions were followed verbatim, some output would not be displayed. Additionally, `iex` would crash upon reaching the UndefinedFunctionError.
1 parent 056b002 commit 1f65b9e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

getting-started/modules.markdown

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ defmodule Math do
100100
end
101101
end
102102

103-
Math.sum(1, 2) #=> 3
104-
Math.do_sum(1, 2) #=> ** (UndefinedFunctionError)
103+
IO.puts Math.sum(1, 2) #=> 3
104+
IO.puts Math.do_sum(1, 2) #=> ** (UndefinedFunctionError)
105105
```
106106

107107
Function declarations also support guards and multiple clauses. If a function has several clauses, Elixir will try each clause until it finds one that matches. Here is an implementation of a function that checks if the given number is zero or not:
@@ -117,18 +117,25 @@ defmodule Math do
117117
end
118118
end
119119

120-
Math.zero?(0) #=> true
121-
Math.zero?(1) #=> false
122-
123-
Math.zero?([1,2,3])
124-
#=> ** (FunctionClauseError)
120+
IO.puts Math.zero?(0) #=> true
121+
IO.puts Math.zero?(1) #=> false
122+
IO.puts Math.zero?([1,2,3]) #=> ** (FunctionClauseError)
125123
```
126124

127125
Giving an argument that does not match any of the clauses raises an error.
128126

129127
## Function capturing
130128

131-
Throughout this tutorial, we have been using the notation `name/arity` to refer to functions. It happens that this notation can actually be used to retrieve a named function as a function type. Let's start `iex` and run the `math.exs` file defined above:
129+
Throughout this tutorial, we have been using the notation `name/arity` to refer to functions. It happens that this notation can actually be used to retrieve a named function as a function type. Let's edit `math.exs` to look like this:
130+
131+
```elixir
132+
defmodule Math do
133+
def zero?(0), do: true
134+
def zero?(x) when is_number(x), do: false
135+
end
136+
```
137+
138+
And start `iex`, running the `math.exs` file defined above:
132139

133140
```bash
134141
$ iex math.exs

0 commit comments

Comments
 (0)