# Function declaration order?

**URL:** https://discourse.julialang.org/t/function-declaration-order/28298
**Category:** General Usage
**Tags:** question
**Created:** [September 2, 2019, 5:12pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298 "2019-09-02T17:12:14Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jasonsun0310](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonsun0310/32/10082_2.png) [@jasonsun0310](https://discourse.julialang.org/u/jasonsun0310)
#### Post date: [September 2, 2019, 5:12pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/1 "2019-09-02T17:12:14Z")

</div>

I am a bit confused about the function declaration order.

For example, if I write following module:

```julia
module A
function callzeros()
    zeros(rand(5,5))
end

function callzeros2()
    myzeros()
end
callzeros2()
function myzeros()
   print("myzero")   
end

function Base.zeros(like::Array{T}) where T<:Any
    return zeros(size(like))
end

callzeros()
end

```

Then is the function `callzeros` going to recognized the my override? In general, does the function declaration order matter? If it does, is there any way get around this requirement like putting a function declaration in the header file in C++?

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [September 2, 2019, 5:22pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/2 "2019-09-02T17:22:42Z")

</div>

Declaration order doesn’t matter unless you’re overwriting a method (i.e. the exact same method name and argument types).

---

<div class="post-metadata">

### Author: ![jasonsun0310](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonsun0310/32/10082_2.png) [@jasonsun0310](https://discourse.julialang.org/u/jasonsun0310)
#### Post date: [September 2, 2019, 5:25pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/3 "2019-09-02T17:25:24Z")

</div>

Can I clarify a little bit more: if I am overloading the same function with a slightly different type signatures in different submodules of a module, the order does not matter?

I am still a bit confused. In the example I gave, this module does not compile because `myzeros` is not recognized by `callzeros2()`

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [September 2, 2019, 5:37pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/4 "2019-09-02T17:37:25Z")

</div>

> [@jasonsun0310](#):
>
> Can I clarify a little bit more: if I am overloading the same function with a slightly different type signatures in different submodules of a module, the order does not matter?

Exactly.

However, when you _run_ code, then only the already-defined methods are available. Normally, you wouldn’t run code in a module (or you run it at the very end), so order does not matter.

---

<div class="post-metadata">

### Author: ![jasonsun0310](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonsun0310/32/10082_2.png) [@jasonsun0310](https://discourse.julialang.org/u/jasonsun0310)
#### Post date: [September 2, 2019, 5:40pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/5 "2019-09-02T17:40:43Z")

</div>

> [@cstjean](#):
>
> However, when you _run_ code, then only the already-defined methods are available. Normally, you wouldn’t run code in a module (or you run it at the very end), so order does not matter.

Hmmm. Interesting… So the rule of thumb is that as long as I am doiing everything inside function definition(within the module), I don’t need to worry about the declaration order, right?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 2, 2019, 5:41pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/6 "2019-09-02T17:41:33Z")

</div>

it’s called multi-dispatch (as it’s different from C++), more here: [Methods · The Julia Language](https://docs.julialang.org/en/v1/manual/methods/index.html)

also: [JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski - YouTube](https://youtu.be/kc9HwsxE1OY?t=495)

---

<div class="post-metadata">

### Author: ![jasonsun0310](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonsun0310/32/10082_2.png) [@jasonsun0310](https://discourse.julialang.org/u/jasonsun0310)
#### Post date: [September 2, 2019, 5:45pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/7 "2019-09-02T17:45:55Z")

</div>

this is fabulous luv…even with video! I think I misunderstood the multidispatch until now!

---

<div class="post-metadata">

### Author: ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)
#### Post date: [September 2, 2019, 5:54pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/8 "2019-09-02T17:54:03Z")

</div>

Exactly. The method is only “looked up” when it’s actually called. Inside definitions you can have whatever you want.

---

<div class="post-metadata">

### Author: ![jasonsun0310](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jasonsun0310/32/10082_2.png) [@jasonsun0310](https://discourse.julialang.org/u/jasonsun0310)
#### Post date: [September 2, 2019, 5:55pm UTC](https://discourse.julialang.org/t/function-declaration-order/28298/9 "2019-09-02T17:55:13Z")

</div>

thanks,luv.
