# Have a function accept an argument of type \`Array{Real}\`

**URL:** <https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 15, 2022, 9:59am UTC](https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307 "2022-09-15T09:59:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![this\_josh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/this_josh/32/42679_2.png) [@this\_josh](https://discourse.julialang.org/u/this_josh)\
**Post date:** [September 15, 2022, 9:59am UTC](https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307/1 "2022-09-15T09:59:03Z")

</div>

I have a function

```julia
function square(a::Real)
    return a[1]^2
end

```

this works with both `Int64` and `Float64`. But if I make it accept an array of `Real`s I get this

```nohighlight
julia> function square(a::Array{Real})
           return a[1]^2
       end
square (generic function with 1 method)

julia> square([Float64(2)])
ERROR: MethodError: no method matching square(::Vector{Float64})
Closest candidates are:
  square(::Array{Real}) at REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

julia> square([Int64(2)])
ERROR: MethodError: no method matching square(::Vector{Int64})
Closest candidates are:
  square(::Array{Real}) at REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

```

How can I correctly type a function that can accept an array of `Real` numbers?

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [September 15, 2022, 11:13am UTC](https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307/2 "2022-09-15T11:13:05Z")

</div>

Julia type parameters are invariant, i.e. `Array{Subtype}` is not a subtype of `Array{BaseType}`.  
For a function to accept arrays of any subtype of real numbers, you have to explicitly express that:

```julia
function square(a::Array{T} where T<:Real)

```

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [September 15, 2022, 11:47am UTC](https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307/3 "2022-09-15T11:47:12Z")

</div>

A shorter syntax would be `a::Array{<:Real}` but then you have no handle on the type, so it depends what you need

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 15, 2022, 12:25pm UTC](https://discourse.julialang.org/t/have-a-function-accept-an-argument-of-type-array-real/87307/4 "2022-09-15T12:25:29Z")

</div>

This is in the [Julia FAQ](https://docs.julialang.org/en/v1/manual/faq/#Why-doesn't-it-work-to-declare-foo(bar::Vector%7BReal%7D)-42-and-then-call-foo(%5B1%5D)?):

> You can solve this problem with something like `foo(bar::Vector{T}) where {T<:Real}` (or the short form `foo(bar::Vector{<:Real})` if the static parameter `T` is not needed in the body of the function).

See the FAQ and the linked sections of the manual for more explanation.
