# Julia Function

**URL:** <https://discourse.julialang.org/t/julia-function/46303>\
**Category:** General Usage\
**Created:** [September 9, 2020, 3:11am UTC](https://discourse.julialang.org/t/julia-function/46303 "2020-09-09T03:11:29Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hola](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@hola](https://discourse.julialang.org/u/hola)\
**Post date:** [September 9, 2020, 3:11am UTC](https://discourse.julialang.org/t/julia-function/46303/1 "2020-09-09T03:11:29Z")

</div>

hi.

kindly help with this. Stuck at it for hours.

How can I implement a Julia function for the product of two matrices AB via dot products, linear combination of columns, linear combination of rows and sum of outer products. A flag should indicate which of the four methods is used.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 9, 2020, 3:41am UTC](https://discourse.julialang.org/t/julia-function/46303/2 "2020-09-09T03:41:54Z")

</div>

Can you expand a bit on your use case? It’s bad style to change the output type of a function via an input flag, since it’s a potential source of type instability. It’s more common to use multiple dispatch or write separate methods. If you’re set on using flags, you could write a few methods with a `Val` flag to dispatch

```julia
holaprod(A, B, ::Val{:dotproduct}) = dot(A, B)
holaprod(A, B, ::Val{:columns}) = ...
...

```

which can then be called with _e.g._ `holaprod(A, B, Val(:dotproduct))`

---

<div class="post-metadata">

**Author:** ![hola](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@hola](https://discourse.julialang.org/u/hola)\
**Post date:** [September 9, 2020, 3:47am UTC](https://discourse.julialang.org/t/julia-function/46303/3 "2020-09-09T03:47:31Z")

</div>

I am not facing any issues when writing separate methods.

the challenge comes when I am trying to create one function that allows me to implement all 4 methods within that function.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [September 9, 2020, 3:51am UTC](https://discourse.julialang.org/t/julia-function/46303/4 "2020-09-09T03:51:29Z")

</div>

What I’m saying is that you shouldn’t try to implement all four within one method, since that goes directly against one of the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/#Break-functions-into-multiple-definitions). Can you write multiple methods with the same name and dispatch using a `Val` flag as I suggested above? What’s the underlying use case - what are you trying to accomplish by using flags instead of dispatch?

---

<div class="post-metadata">

**Author:** ![hola](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@hola](https://discourse.julialang.org/u/hola)\
**Post date:** [September 9, 2020, 3:56am UTC](https://discourse.julialang.org/t/julia-function/46303/5 "2020-09-09T03:56:42Z")

</div>

i understand. With flags just wanted to see and understand how the code is affected in terms of performance.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 9, 2020, 3:57am UTC](https://discourse.julialang.org/t/julia-function/46303/6 "2020-09-09T03:57:47Z")

</div>

Welcome to the community! If this is a homework question, make sure to indicate that and expect to get only hints on how to solve it yourself, not the solution.

---

<div class="post-metadata">

**Author:** ![hola](https://avatars.discourse-cdn.com/v4/letter/h/e47774/32.png) [@hola](https://discourse.julialang.org/u/hola)\
**Post date:** [September 9, 2020, 4:00am UTC](https://discourse.julialang.org/t/julia-function/46303/7 "2020-09-09T04:00:00Z")

</div>

Sure. Thanks for the welcome. understood. Never asked for the solution.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 9, 2020, 8:12am UTC](https://discourse.julialang.org/t/julia-function/46303/8 "2020-09-09T08:12:42Z")

</div>

I’m not sure I understand the difficulty. If you already know how to do the different implementations, switching between them shouldn’t be more complicated than something like

```julia
function my_flexible_matrix_product(A, B, method)
    if method == "method1"
        # code to implement the first method
    elseif method == "method2"
        # code to implement the second method
    else
        error("Unknown method.")
    end
    return result
end

```

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [September 9, 2020, 9:14am UTC](https://discourse.julialang.org/t/julia-function/46303/9 "2020-09-09T09:14:49Z")

</div>

> [@GunnarFarneback](#):
>
> ```julia
> function my_flexible_matrix_product(A, B, method)
> if method == "method1"
> # code to implement the first method
> elseif method == "method2"
> # code to implement the second method
> else
> error("Unknown method.")
> end
> return result
> end
> 
> ```

Note that the preferred way is to use symbols (`:method1`) instead of strings (`"method1"`) for such things. I think it’s mostly for performance reasons.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [September 10, 2020, 7:07am UTC](https://discourse.julialang.org/t/julia-function/46303/10 "2020-09-10T07:07:56Z")

</div>

Could you please exemplify the use of symbols here?

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [September 10, 2020, 10:03am UTC](https://discourse.julialang.org/t/julia-function/46303/11 "2020-09-10T10:03:50Z")

</div>

```julia
function my_flexible_matrix_product(A, B, method)
    if method == :method1
        # code to implement the first method
    elseif method == :method2
        # code to implement the second method
    else
        error("Unknown method.")
    end
    return result
end

```

and then you use it with `my_flexible_matrix_product(A, B, :method1)`.
