# Optional positional arguments at the begining

**URL:** https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925
**Category:** New to Julia
**Tags:** question, agents
**Created:** [September 26, 2024, 3:55pm UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925 "2024-09-26T15:55:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [September 26, 2024, 3:55pm UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925/1 "2024-09-26T15:55:45Z")

</div>

Hello guys I am not a computer scientist and I am new to Julia, I am using `Agents.jl` for agent-based simulations. I found this function that adds agents and there are multiple signatures for it to exploit the multi dispatch option.

```julia
add_agent!([pos,] model::ABM, args...) → newagent
add_agent!([pos,] model::ABM; kwargs...) → newagent

```

My problem with this is that the optional positional arguments should be at the end and here we see that they are at the beginning ? Any explanation please ?

---

<div class="post-metadata">

### Author: ![Datseris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/datseris/32/13406_2.png) [@Datseris](https://discourse.julialang.org/u/Datseris)
#### Post date: [September 26, 2024, 4:10pm UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925/2 "2024-09-26T16:10:52Z")

</div>

They are optional arguments in the sense that it is optional for the user whether it will provide these or not. Technically speaking they are not “optional positional arguments” in the sense of the function definition `f(a, b=0.2) = ...`.

These optional arguments are achieved via multiple dispatch, by utilizing the fact that the type of this first arugment is special if it is a position input. You can achieve this in julia with something like:

```julia
f(a::Real, b::Vector) = a .* b
f(b::Vector) = b

```

In this way `a` is an “optional argument” that scales `b`.

I admit this is a bit weird for a newcomer. Surely in my example above `a` is an optional argument and is specified by position. So it is an “optional positional argument”. I hope this helps… I don’t know myself how to explain it better 😛 Let’s hope another senior member attempts at an answer!

---

<div class="post-metadata">

### Author: ![Farouk](https://avatars.discourse-cdn.com/v4/letter/f/f17d59/32.png) [@Farouk](https://discourse.julialang.org/u/Farouk)
#### Post date: [September 26, 2024, 4:20pm UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925/4 "2024-09-26T16:20:01Z")

</div>

Thank you very much now I get it… You explained it very well 👍

---

<div class="post-metadata">

### Author: ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)
#### Post date: [September 26, 2024, 5:41pm UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925/5 "2024-09-26T17:41:51Z")

</div>

> [@Datseris](#):
>
> You can achieve this in julia with something like:
> 
> ```julia
> f(a::Real, b::Vector) = a .* b
> f(b::Vector) = b
> 
> ```
> 
> In this way `a` is an “optional argument” that scales `b`.

NB: a function definition with an optional argument, like `f(mandatory, optional=nothing) = g(mandatory, optional)`, is really just syntax sugar for:

```julia
f(mandatory, optional) = g(mandatory, optional)
f(mandatory) = f(mandatory, nothing)

```

So the only difference between optional positional arguments at the end versus at the beginning is availability of syntax sugar.

---

<div class="post-metadata">

### Author: ![tecosaur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tecosaur/32/23206_2.png) [@tecosaur](https://discourse.julialang.org/u/tecosaur)
#### Post date: [September 27, 2024, 8:55am UTC](https://discourse.julialang.org/t/optional-positional-arguments-at-the-begining/119925/6 "2024-09-27T08:55:53Z")

</div>

I do sometimes wish it was easier to write optional initial arguments.
