# How to define a function with more than one optional arguments in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784>\
**Category:** General Usage\
**Tags:** question, functions\
**Created:** [October 15, 2022, 6:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784 "2022-10-15T18:23:29Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [October 15, 2022, 6:23pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/1 "2022-10-15T18:23:30Z")

</div>

I am trying to understand the mechanism to define functions in Julia.  
For example

```julia
function xyzpos(x, y, z=0)
    println("$x, $y, $z")
end

```

Note that

```julia
xyzpos(1,2)
> 1, 2, 0

```

and

```julia
xyzpos(1,2,z=3)
> 1, 2, 3

```

Up to here, it’s ok. Now, note that

```julia
xyzpos(x,y=9,z=3)
> MethodError: no method matching xyzpos(::Int64; y=9, z=3)

```

So, I’m trying to fix this with

```julia
function xyzpos2(x, y=0, z=0)
   println("$x, $y, $z")
end

```

But, note that

```julia
xyzpos2(5,y=9,z=3)
> MethodError: no method matching xyzpos2(::Int64, ::Int64; y=9, z=3)

```

I’m not understanding the logic of the mechanism when defining functions

Help

---

<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:** [October 15, 2022, 7:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/2 "2022-10-15T19:01:02Z")

</div>

```julia
julia> xyzpos(x, y, z=0) = x,y,z
xyzpos (generic function with 2 methods)

julia> xyzpos(1,2,z=3)
ERROR: MethodError: no method matching xyzpos(::Int64, ::Int64; z=3)
Closest candidates are:

```

this shouldn’t work, if you don’t have `;`, you simply don’t have keyword argument, so what you want is:

```julia
function xyzpos(; x, y, z=0)
    println("$x, $y, $z")
end

```

notice the `;`

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [October 15, 2022, 7:37pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/3 "2022-10-15T19:37:19Z")

</div>

For me, It works:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/3/639093e02010249126858edb0b35204c72497698.png)

---

<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:** [October 15, 2022, 7:39pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/4 "2022-10-15T19:39:20Z")

</div>

what Julia version are you on? ~~that looks like an old bug or~~ your kernel is in bad state (cell 410 and 411? can you restart kernel)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 15, 2022, 7:41pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/5 "2022-10-15T19:41:09Z")

</div>

I think you’re confusing [optional arguments](https://docs.julialang.org/en/v1/manual/functions/#Optional-Arguments) with [keyword arguments](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments).

(In Python, these are essentially the same thing, but in Julia they are distinct because positional arguments are used for dispatch.)

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [October 15, 2022, 7:44pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/6 "2022-10-15T19:44:20Z")

</div>

```julia
julia> versioninfo() 
Julia Version 1.6.4 Commit 35f0c911f4 (2021-11-19 03:54 UTC) Platform Info:

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 15, 2022, 7:44pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/7 "2022-10-15T19:44:22Z")

</div>

> [@maoc](#):
>
> For me, It works:

You must have defined another method earlier that used a keyword `z`, e.g.:

```julia
julia> xyzpos(x, y; z=0) = "$x, $y and keyword $z"
xyzpos (generic function with 1 method)

julia> xyzpos(x, y, z=0) = "$x, $y, $z"
xyzpos (generic function with 2 methods)

julia> xyzpos(1,2,z=3)
"1, 2 and keyword 3"

```

This is the peril of experimenting in a long-running session; you may have other method definitions lurking that you’ve forgotten about.

---

<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:** [October 15, 2022, 7:50pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/8 "2022-10-15T19:50:36Z")

</div>

```julia
  | | |_| | | | (_| | | Version 1.6.4 (2021-11-19)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |

julia> xyzpos(x, y, z=0) = x,y,z
xyzpos (generic function with 2 methods)

julia> xyzpos(1,2,z=3)
ERROR: MethodError: no method matching xyzpos(::Int64, ::Int64; z=3)
Closest candidates are:
  xyzpos(::Any, ::Any) at REPL[1]:1 got unsupported keyword argument "z"
  xyzpos(::Any, ::Any, ::Any) at REPL[1]:1 got unsupported keyword argument "z"
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [October 15, 2022, 7:55pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/9 "2022-10-15T19:55:21Z")

</div>

I restarted and it didn’t work. Thanks!

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [October 15, 2022, 8:00pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/10 "2022-10-15T20:00:24Z")

</div>

You may consider consulting the section “function arguments” of the course “[Introduction to scientific programming and machine learning with Julia](https://sylvaticus.github.io/SPMLJ/stable/index.html)”:

- script and exercises: [0104 - Control flow and functions · SPMLJ](https://sylvaticus.github.io/SPMLJ/stable/01_-_JULIA1_-_Basic_Julia_programming/0104_-_Control_flow_and_functions.html#Function-arguments)
- video : [SPMLJ 01 JULIA1 4C Control flow and functions Part C Functions - YouTube](https://youtu.be/oejR6LKvFXY)

---

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [October 15, 2022, 8:42pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/11 "2022-10-15T20:42:25Z")

</div>

Thank you for your answer. I have a doubt.

After “;” Why did you leave the y argument with no value and the z = 0 argument? I thought that after the “;” necessarily we had to put “y= some value”

```julia

```

---

<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:** [October 15, 2022, 8:43pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/12 "2022-10-15T20:43:52Z")

</div>

> [@maoc](#):
>
> necessarily we had to put “y= some value”

no, if you put value it’s “optional keyword argument”, if not, it’s “keyword argument”

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [October 15, 2022, 8:47pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/13 "2022-10-15T20:47:59Z")

</div>

> [@maoc](#):
>
> I thought that after the “;” necessarily we had to put “y= some value”

From [the manual](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments):

> If a keyword argument is not assigned a default value in the method definition, then it is _required_: an [`UndefKeywordError`](https://docs.julialang.org/en/v1/base/base/#Core.UndefKeywordError) exception will be thrown if the caller does not assign it a value

(I actually [added this feature in Julia 0.7](https://github.com/JuliaLang/julia/pull/25830).)

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 15, 2022, 11:41pm UTC](https://discourse.julialang.org/t/how-to-define-a-function-with-more-than-one-optional-arguments-in-julia/88784/14 "2022-10-15T23:41:30Z")

</div>

Basically arguments can be:

- Positional OR Keyword
- Optional OR Mandatory

Those are two distinct ways of classifying them, and they’re independent of one another:

Positional arguments - those that come before the `;` - can be either optional (given a default value) or mandatory.

Same for keyword arguments - ones after the `;` in the function definition - those can be optional or mandatory too.
