# Is there a way to declare types for all arguments in a function?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108>\
**Category:** New to Julia\
**Tags:** syntax\
**Created:** [April 13, 2019, 2:12pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108 "2019-04-13T14:12:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 13, 2019, 2:12pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/1 "2019-04-13T14:12:32Z")

</div>

Let’s look at this function

```julia
function foo(a::Int, b::Int, c::Int)::Int 
    return a + b +c
end

```

Is there a way to say all of a, b and c are Ints instead of repeating? Or is the “right” way to put the arguments in an array and pass the function that instead?

Edit: Ops, completely wrong section at first sorry.

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [April 13, 2019, 2:42pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/2 "2019-04-13T14:42:12Z")

</div>

You can do:

```julia
julia> function foo(aa::Vararg{Int,3})
       a,b,c = aa
       return a + b +c
       end
       
       
foo (generic function with 1 method)

julia> foo(1,2,3)
6

julia> foo(1,2)
ERROR: MethodError: no method matching foo(::Int64, ::Int64)
Closest candidates are:
  foo(::Int64, ::Int64, ::Int64) at REPL[25]:2
Stacktrace:
 [1] top-level scope at none:0

julia> foo(1,2,3,4)
ERROR: MethodError: no method matching foo(::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
  foo(::Int64, ::Int64, ::Int64) at REPL[25]:2
Stacktrace:
 [1] top-level scope at none:0

```

but unless there are very many arguments, I find the `Vararg` less clear.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 13, 2019, 2:54pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/3 "2019-04-13T14:54:04Z")

</div>

If you just want to avoid repetition, you can use a type parameter restricted to a concrete type, eg

```julia
function foo(a::T, b::T, c::T)::T where {T <: Int}
    return a + b +c
end

```

But (as we are in _First steps_), recall that there is no advantage from declaring types for reasons other than dispatch (broadly). So most experience users of Julia would write

```julia
foo(a, b, c) = a + b + c

```

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 13, 2019, 4:15pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/4 "2019-04-13T16:15:29Z")

</div>

Yeah that was kind of what I was looking for thanks! Although you might be right it’s not obvious what’s the clearest.

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 13, 2019, 4:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/5 "2019-04-13T16:17:45Z")

</div>

Good point. Although I still find it slightly confusing when types matter and when they don’t. You haven’t seen the last of me here in first steps to put it like that.

---

<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:** [April 13, 2019, 4:30pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/6 "2019-04-13T16:30:19Z")

</div>

Types in function arguments are generally not related to performance. Think of them as a filter, saying which methods are called for which arguments.

In your example above, your type declarations have zero impact on performance, they just limit your `foo` to `Int` arguments. And your return-type declaration is redundant because the compiler can already infer it.

(The whole point of Julia is that you can write type-generic code that is still fast.)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 13, 2019, 7:29pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/7 "2019-04-13T19:29:20Z")

</div>

> [@Donkeyfish87](#):
>
> I still find it slightly confusing when types matter and when they don’t.

Simple rule of thumb: put concrete type (or type parameter) annotations on your struct fields, otherwise they are optional.

---

<div class="post-metadata">

**Author:** ![Donkeyfish87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/donkeyfish87/32/10654_2.png) [@Donkeyfish87](https://discourse.julialang.org/u/Donkeyfish87)\
**Post date:** [April 13, 2019, 11:31pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-declare-types-for-all-arguments-in-a-function/23108/8 "2019-04-13T23:31:46Z")

</div>

Thanks guys, helpful tips. I find it better to go a bit overboard at first, since I’m not used to thinking about types at all. My specialty is slow matlab code with bugs in it so this is new stuff for me. It gives me a sense of accomplishment knowing my bug at least is an Int now.
