To expand briefly on @pkofod’s response, defining named functions within a function is not supported. To achieve the desired behavior, you can to use anonymous functions. So your second example should be written instead as
function myfun(x, y)
if y > 0
f = x -> x
else
f = x -> 10000
end
y = f(x)^2
return y
end
As I understand it, allowing f to be a “regular” function here is problematic because it may have different methods depending on the code path, and rebuilding its method list on each call is slow.
The simpler, more Julian approach here would be to define f outside of myfun:
f(x, y) = y > 0 ? x : 10000
myfun(x, y) = f(x, y)^2
Defining “named” function in local scope/as closure is fully supported, the difference is the function type.
In the same scope, all functions definitions of the same name are of the same type. So
function f()
...
g(...) = ...
...
g(...) = ...
end
Will create a type with two methods. The type and methods definitions are generated at compile (lowering) time and be define before f is called. (Though calling them with the wrong signature might cause undefined variable error)
Anonymous functions don’t have a name (because, well, they are anonymous…) so they each have their own types.