The documentation on variable scoping clearly states that a for loop defines a new scope. This means result
is going out of scope when execution leaves the for loop. Hence it is undefined when you call println(result)
Defining result
in advance of the for loop should give the behaviour you are expecting:
function any(vec)
dim = size(vec)
result = -1
for i in 1:dim[2]
...
Or if you do not wish to assign a default value, and are sure the for loop will set its value, you can do:
function any(vec)
dim = size(vec)
local result
for i in 1:dim[2]
...
In the first example, if the for loop does not set a value, result
will be -1
.
In the the second example, not setting a value in the for loop will leave result
undefined.