# Why does Julia have multiple ways to define functions?

**URL:** <https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 2, 2023, 11:31pm UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424 "2023-08-02T23:31:02Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 1, 2024, 2:21pm UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424/22 "2024-10-01T14:21:55Z")

</div>

> [@hatmatrix](#):
>
> The [docs](https://docs.julialang.org/en/v1/manual/functions/) present at least three ways: the standard form, “compact” form, anonymous form, etc. Then there are the `do` blocks…

there are about 20 ways [Meet some mind-blowing ways to define functions in Julia: a tour of the many ways to create functions in Julia | by ZJ Dai | Medium](https://medium.com/@evalparse/there-are-so-many-ways-to-define-a-function-sort-of-in-julia-7821e49373cd)

lol

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [October 15, 2024, 7:48am UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424/23 "2024-10-15T07:48:31Z")

</div>

It’s cool that the language gives you flexibility to do all these things (like the method of overloading) but this is in ridiculous territory!

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [October 16, 2024, 10:58am UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424/24 "2024-10-16T10:58:24Z")

</div>

actually i feel uneasy about this. I would prefer fewer ways to define functions

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 16, 2024, 11:36am UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424/25 "2024-10-16T11:36:59Z")

</div>

> [@sadish-d](#):
>
> Another example: implicit multiplication: `a=[1,2]; a'a` gives you `5`. But `[1,2]'[1,2]` gives `2`.

I honestly still find this confusing. What is happening to for that to result be `2`?

```julia-repl
julia> ([1,2]')[1,2]
2

julia> ([1,2]') * [1,2]
5

```

Oh, I got it. Yeah, that’s quite confusing.

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [October 16, 2024, 12:47pm UTC](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424/26 "2024-10-16T12:47:23Z")

</div>

> [@lmiq](#):
>
> I honestly still find this confusing. What is happening to for that to result be `2`?

It is an indexing operation actually 😃 It just happens to work out with the specific numbers in this example.  
So `([1,2]')` just gives the “row vector” `[1;;2]` and the following `[1,2]` indexes it at position `1,2` so first row, second column which is… `2` 🎉

Consider as a sanity check:

```julia-repl
julia> ([1,2]')[1,3]
ERROR: BoundsError: attempt to access 1×2 adjoint(::Vector{Int64}) with eltype Int64 at index [1, 3]
Stacktrace:
...

```

So in the end, this confusion is due to a clash of syntax between indexing and array literals that just happens to work out accidentally with the numbers in this example.

[Previous page](https://discourse.julialang.org/t/why-does-julia-have-multiple-ways-to-define-functions/102424.md?page=1)
