# Do you put return type in function definitions?

**URL:** <https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120>\
**Category:** General Usage\
**Created:** [December 10, 2019, 8:40pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120 "2019-12-10T20:40:39Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [December 10, 2019, 8:40pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/1 "2019-12-10T20:40:39Z")

</div>

Do you have any opinion about annotating function definitions with return types? Like it or not and why?

Here’s a dummy example:

```julia
add(x, y)::Int = x + y

```

It gives additional type checking:

```julia
julia> add(1,2)
3

julia> add(1.0,2)
3

julia> add(1.1, 2)
ERROR: InexactError: Int64(3.1)

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 10, 2019, 8:47pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/2 "2019-12-10T20:47:16Z")

</div>

In general, don’t. The fact that methods in julia can automatically infer return types is a feature, not a bug.

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [December 10, 2019, 9:51pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/3 "2019-12-10T21:51:05Z")

</div>

I agree that it’s a feature. The question is whether it’s “idiomatic” to annotate return type and make Julia do type checks at runtime.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 10, 2019, 10:01pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/4 "2019-12-10T22:01:45Z")

</div>

No it’s not. There are cases where return type annotation is useful. However, you must not take that as a license to use it for all or most of your functions.

It also has little to do with runtime type check…

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [December 10, 2019, 10:20pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/5 "2019-12-10T22:20:07Z")

</div>

FWIW, I sometimes find it helpful to annotate return types in function definitions if I’m using several function calls within the same function but this is mostly for clarity for own own sake when I come back to large projects after a few weeks or months.

I don’t think performance is affected by whether you annotate return type in a function or not, but correct me if I’m wrong.

---

<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:** [December 11, 2019, 8:20am UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/6 "2019-12-11T08:20:39Z")

</div>

I try to write mostly generic code so I never do this.

The only thing I ever use is its [twin brother](https://docs.julialang.org/en/v1/manual/types/#Type-Declarations-1) `some_function_call(x, y)::SomeType` to give the compiler a nudge for inference, but that’s an entirely different thing.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 11, 2019, 2:36pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/7 "2019-12-11T14:36:49Z")

</div>

In general I don’t do it, as I use Julia as a dynamic language.  
The same reason, why I don’t do it neither (in general) for the function parameters.

Thats how I start. And during evolving of the code I think here or there I should specify the types.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 11, 2019, 2:44pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/8 "2019-12-11T14:44:25Z")

</div>

> [@Tamas\_Papp](#):
>
> The only thing I ever use is its [twin brother](https://docs.julialang.org/en/v1/manual/types/#Type-Declarations-1) `some_function_call(x, y)::SomeType` to give the compiler a nudge for inference, but that’s an entirely different thing.

Do I understand this right?  
You know that `some_function_call` returns `SomeType`, but you help the compiler with specifiying it explicitely in the line of calling `some_function_call`?

Do you have an example on which I can see why this can be?

---

<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:** [December 11, 2019, 4:30pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/9 "2019-12-11T16:30:56Z")

</div>

Type inference can fail in some complex settings, but this usually happens in nontrivial code so I don’t have a self-contained MWE to demonstrate. The last time I needed this was a

```julia
mapreduce(some_complicated_function::AbstractVector, hcat, xs)::AbstractMatrix

```

because inference was shaky (it was a `Union` of a few things). This was in one of the release candidates for 1.3 so I don’t know if it persists, also it was under many layers in a 10k LOC codebase.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 11, 2019, 5:11pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/10 "2019-12-11T17:11:54Z")

</div>

> [@Tamas\_Papp](#):
>
> Type inference can fail in some complex settings, but this usually happens in nontrivial code so I don’t have a self-contained MWE to demonstrate.

I expected this, but your example is a good one. I didn’t imagine, when I asked, that functions e.g. can be parameters as well and the return type can change with that and adding some complexity does the rest.

---

<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:** [December 11, 2019, 5:23pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/11 "2019-12-11T17:23:59Z")

</div>

> [@Tamas\_Papp](#):
>
> mapreduce(some\_complicated\_function::AbstractVector, hcat, xs)::AbstractMatrix

Sorry, this is nonsensical, it was more like

```julia
mapreduce(x -> some_complicated_function(x)::AbstractVector, hcat, xs)::AbstractMatrix

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 11, 2019, 7:25pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/12 "2019-12-11T19:25:25Z")

</div>

I think in general it is nice to see the return type in the place where the function is defined (i. e. at the top). Much better than having to hunt for it through the source code, emulating the work of the compiler really. For instance, what does this function return

```julia
inv(B::BunchKaufman{<:BlasComplex})

```

on line [350 of bunchkaufman.jl](https://github.com/JuliaLang/julia/blob/46ce4d79337bdd257ee2e3d2f4bb1c55ff0a5030/stdlib/LinearAlgebra/src/bunchkaufman.jl)? Wouldn’t it be nice if this information was on the line where this function is defined?

---

<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:** [December 12, 2019, 8:51am UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/13 "2019-12-12T08:51:41Z")

</div>

For a lot of high-level idiomatic Julia code, the return type may be just be an implementation detail, and/or depend on the input types in a way that is impractical to document with `::`. Eg

```julia
"""
    make_hermitian(A)

Return a hermitian matrix that is similar to `A` in size and type.

For unit testing.
"""
make_hermitian(A) = Hermitian(A' * A)

make_hermitian(rand(10, 10))

make_hermitian(@SMatrix rand(10, 10))

```

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [December 12, 2019, 9:05am UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/14 "2019-12-12T09:05:25Z")

</div>

True.

---

<div class="post-metadata">

**Author:** ![bcon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bcon/32/23495_2.png) [@bcon](https://discourse.julialang.org/u/bcon)\
**Post date:** [May 12, 2022, 7:15pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/15 "2022-05-12T19:15:43Z")

</div>

Note that Julia [does not dispatch over return type](https://discourse.julialang.org/t/multiple-dispatch-depending-on-output-type/7639/11) as may be desired when searching this topic.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 30, 2024, 4:17pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/16 "2024-10-30T16:17:01Z")

</div>

9 posts were split to a new topic: [A function return type of `::AbstractArray{AbstractString}` causes a performance penalty?](https://discourse.julialang.org/t/a-function-return-type-of-abstractarray-abstractstring-causes-a-performance-penalty/122003)

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 30, 2024, 4:14pm UTC](https://discourse.julialang.org/t/do-you-put-return-type-in-function-definitions/32120/25 "2024-10-30T16:14:06Z")

</div>

A post was split to a new topic: [Why does a function return type of `::AbstractArray{AbstractString}` cause a performance penalty?](https://discourse.julialang.org/t/why-does-a-function-return-type-of-abstractarray-abstractstring-cause-a-performance-penalty/122002)
