May 2019
Beginner to intermediate
466 pages
10h 44m
English
If you have previous programming experience, you might be surprised to see that invoking the add function correctly returns the expected value, despite the fact that we didn't put any explicit return statement in the function's body. In Julia, a function automatically returns the result of the last expression that was evaluated. This is usually the last expression in the body of the function.
An explicit return keyword is also available. Using it will cause the function to exit immediately, with the value passed to the return statement:
julia> function add(x, y)
return "I don't feel like doing math today"
x + y
end
add (generic function with 1 method) julia> add(1, 2)
"I don't feel like doing math today"
Read now
Unlock full access