Skip to content

Commit

Permalink
Add a small clarification on the example showing leaky if block. More…
Browse files Browse the repository at this point in the history
…over, a new example is shown where the leaky declarations cause bugs.
  • Loading branch information
harikb committed Apr 7, 2015
1 parent cd653bb commit 9a7123d
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/manual/control-flow.rst
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,30 @@ So, we could have defined the ``test`` function above as
println("x is ", relation, " than y.")
end

The variable ``relation`` is declared inside the ``if`` block, but used
outside. However, when depending on this behavior, make sure all possible
code paths define a value for the variable. The following change to
the above function results in a runtime error

.. doctest::

julia> function test(x,y)
if x < y
relation = "less than"
elseif x == y
relation = "equal to"
end
println("x is ", relation, " than y.")
end
test (generic function with 1 method)

julia> test(1,2)
x is less than than y.

julia> test(2,1)
ERROR: UndefVarError: relation not defined
in test at none:7

``if`` blocks also return a value, which may seem unintuitive to users
coming from many other languages. This value is simply the return value
of the last executed statement in the branch that was chosen, so
Expand Down

0 comments on commit 9a7123d

Please sign in to comment.