In Julia, is there any difference at all between these to two three ways of creating a function:
- With keyword
function
function g1(x)
# compute something, store it in `result`
return result # (return keyword is optional)
end
- With keyword
let
g2(x) = let
# compute something, store it in `result`
return result # (return keyword is optional)
end
- With keyword
begin
g3(x) = begin
# compute something, store it in `result`
return result # (return keyword is optional)
end
The answer below is: these three definitions are exactly equivalent.
EDIT: clarify the question, and emphasis on the answer.
Note that the
letvariant also creates a function. Therefore you could also write:to get the same effect.
The reason is that all after
=here is withing2function scope, so no matter if you dobegin-endorlet-endthe expression is in this scope. Theletvariant creates an extra hard scope, but it does not change anything as there is nothing in the function scope that is not inletscope in your example.let-endwould make a difference frombegin-endif it were in a global scope, but in your case you introduce it within function scope.