# Passing arrays with unspecified type

**URL:** <https://discourse.julialang.org/t/passing-arrays-with-unspecified-type/39407>\
**Category:** General Usage\
**Created:** [May 13, 2020, 2:08pm UTC](https://discourse.julialang.org/t/passing-arrays-with-unspecified-type/39407 "2020-05-13T14:08:48Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [May 13, 2020, 2:28pm UTC](https://discourse.julialang.org/t/passing-arrays-with-unspecified-type/39407/2 "2020-05-13T14:28:49Z")

</div>

As a general rule, it is important to label fields of a _struct_ or the elements of a container you are constructing with their types, but you _do not_ need to label the types of your function inputs for performance. In fact, it says so right here in the manual: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html#Type-declarations-1)

> In many languages with optional type declarations, adding declarations is the principal way to make code run faster. This is _not_ the case in Julia. In Julia, the compiler generally knows the types of all function arguments, local variables, and expressions.

What that means is that any of the following function definitions will give _exactly the same performance_:

```julia
function f1(a) # matches any `a`; equivalent to `a::Any`
  ...
end

function f2(a::AbstractArray) # matches an array with any number of dimensions
  ...
end

function f3(a::AbstractVector) # matches an array with exactly one dimension
  ...
end

function f4(a::AbstractVector{<: Number}) # matches an array with exactly one dimension
                                           # whose elements are all some kind of `Number`
  ...
end

function f5(a::Vector{Float64}) # matches only exactly a Vector of Float64
  ...
end

```

Try it! Define each of those functions and install BenchmarkTools.jl via:

```julia
]add BenchmarkTools

```

and then do:

```julia
using BenchmarkTools
@btime f1($a)
@btime f2($a)
@btime f3($a)
@btime f4($a)
@btime f5($a)

```

So why would you choose `f4` instead of `f1` ? Adding types to your function arguments is a matter of API design. If you define an `f4(a::AbstractVector{<:Number})`, then you can also define `f4(a::SomeOtherType)` and the compiler will automatically pick the appropriate method for whatever input is passed in. Or you might choose to specify your argument types as a signal to users of your code so that they know what you’re expecting them to provide.

---

_[View the full topic](https://discourse.julialang.org/t/passing-arrays-with-unspecified-type/39407)._
