Reputation: 117
I have three related questions about specifying types of function arguments and outputs. I'm defining a function f that I expect to call many times, so I'm looking to improve efficiency everywhere I can. My function definition looks something like this:
function f(x, y)
...
return z
end
I know that x
will be an Array{Float64,1}, y
will be an Array{Float64,2}, and z
will be a Float64.
My questions are:
function f(x::Array{Float64}, y::Array{Float64})
?x
is 1-dimensional and y
is 2-dimensional, i.e. function f(x::Array{Float64,1}, y::Array{Float64,2})
?z
, i.e. function f(x::Array{Float64,1}, y::Array{Float64,2})::Float64
?Thanks so much! And apologies if these questions have been addressed previously.
Upvotes: 4
Views: 93
Reputation: 9086
There is a huge benefit for the human reader of the code. Consider the difference for a human reading for the first time (or perhaps after a long time away from the code) :
function f(x, y)
vs.
function f(x::Array{Float64}, y::Array{Float64})::Float64
Upvotes: 3
Reputation: 2995
In general, no (to all questions). The beauty of Julia is that its compiler is excellent at figuring these things out for you on the fly. The performance tips is the best place to look for how to write efficient code, like making sure your function is type-stable.
Upvotes: 8