# Differences between mutliple functions and function with multiple methods

**URL:** <https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317>\
**Category:** Performance\
**Tags:** question, functions\
**Created:** [April 15, 2021, 8:22am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317 "2021-04-15T08:22:32Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [April 15, 2021, 8:22am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/1 "2021-04-15T08:22:32Z")

</div>

For one of my projects, I have multiple implementations of the same analytical computations. These implementations offer different speed / precision trade-offs, and involve mostly a two step process where the entries and outputs are the same, but not the intermediate results. The final result is a matrix.

```julia
struct myintermediateresult1
...
end
struct myintermediateresult2
...
end
function myfirstmethod1(args...)::myintermediateresult1
...
end
function myfirstmethod2(args...)::myintermediateresult2
...
end
function mysecondmethod1(arg::myintermediateresult1)::Matrix{Float64}
...
end
function mysecondmethod2(arg::myintermediateresult2)::Matrix{Float64}
...
end

```

When I call this in my script, I use a symbol as argument of the calling function and wrap these methods in an `if then else` construct e.g.

```julia
if arg_symbol === :method1
    intermediateresult = myfirstmethod(args...)
else 
    intermediateresult = mysecondmethod(args...)
end

```

Therefore, the variable `intermediateresult` is not always of the same type.  
JeffreySarnoff explained that one could also declare a single function with multiple methods using for instance

```julia
function myfirstmethod(args...,::Val{:method1})::myintermediateresult1
...
end
function myfirstmethod(args...,::Val{:method2})::myintermediateresult2
...
end

```

and avoid the `if then else` altogether.

What are the differences between these implementations, especially in a situation where I often compare the different implementations ? Is there a difference in terms of precompilation or efficiency ? This is probably related to the multiple dispatch (so I guess the second option is more julianic) but can someone highlight some major differences ?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 15, 2021, 8:33am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/2 "2021-04-15T08:33:35Z")

</div>

If I understand you correctly, you have some distinct structs and some sets of methods, one method set for each struct type.

```julia
struct StructA
   value::Int32
end

struct StructB
  value::Float64
end

function f1_a(x::StructA) ...
function f1_b(x::StructB) ...
function f2_a(x::StructA) ...
function f2_b(x::StructB) ...

```

that will work without any ifelse-ing

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [April 15, 2021, 9:04am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/3 "2021-04-15T09:04:30Z")

</div>

Well actually, if you look at `myfirstmethod1` and `myfirstmethod2`, both share the same input arguments, so I cannot use directly multiple dispatch without specifying an additional argument (as proposed in the second implementation) or use an `if the else` construct.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 15, 2021, 9:12am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/4 "2021-04-15T09:12:48Z")

</div>

We use `Val` for that sort of disambiguation  
(note the curly braces in the function definition and the parens in the call).

`function f(::Val{:asymbol}, args..) ...`  
`function f(::Val{:bsymbol}, args..) ...`  
`f(Val(:asymbol), ..)` or `f(Val(:bsymbol), ..)` as you require

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [April 15, 2021, 9:15am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/5 "2021-04-15T09:15:45Z")

</div>

So you mean there is a difference between

```julia
function f(::Val{:asymbol}, args..) ...

```

and

```julia
function f(:asymbol, args..) ...

```

?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 15, 2021, 9:17am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/6 "2021-04-15T09:17:28Z")

</div>

absolutely – the form with Val does what you want, the other is not Julia code.

---

<div class="post-metadata">

**Author:** ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)\
**Post date:** [April 15, 2021, 9:20am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/7 "2021-04-15T09:20:59Z")

</div>

Indeed ! That’s my bad. That is actually what I meant in my first post. I will edit accordingly.  
However, my initial question remains… what are the main differences between these two implementations ?

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [April 15, 2021, 9:25am UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/8 "2021-04-15T09:25:02Z")

</div>

In my opinion, one is cleaner, easier to maintain over time (particularly if you add more cases)  
In this particular case, performance differences should be inconsequential unless you do this a huge number of times [in that case benchmark each approach].

---

<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:** [April 16, 2021, 3:57pm UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/9 "2021-04-16T15:57:19Z")

</div>

I’d rather follow the first reply in this topic and wrap input arguments in some sort of a buffer:

```julia
struct Method1Input
    ...
end

struct Method2Input
    ...
end

function first_step(input::Method1Input)::IntermediateResult1
    ...
end

function first_step(input::Method2Input)::IntermediateResult2
    ...
end

```

Then, instead of

```julia
first_step(Val(:method1), args...)

```

just call

```julia
first_step(Method1Input(args...))

```

---

<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:** [April 16, 2021, 4:01pm UTC](https://discourse.julialang.org/t/differences-between-mutliple-functions-and-function-with-multiple-methods/59317/10 "2021-04-16T16:01:50Z")

</div>

Another way is to pass a function as an extra argument:

```julia
function first_step(method::Function, args...)
    method(args...)
end

function method1(args...)
   ...
end

function method2(args...)
   ...
end

```
