# Optional args and the methods they imply

**URL:** <https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062>\
**Category:** General Usage\
**Created:** [June 2, 2017, 3:41pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062 "2017-06-02T15:41:30Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 3:41pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/1 "2017-06-02T15:41:30Z")

</div>

```julia
# https://docs.julialang.org/en/stable/manual/methods/#man-note-on-optional-and-keyword-arguments
# Optional args are implemented as syntax for multiple method definitions, e.g.
f(a=1,b=2) = a+2b 

# which translates into three methods
f(a,b) = a+2b # explicit/optional a,b args
f(a) = f(a,2) # explicit/optional a arg
f() = f(1,2) # no optional args, f() call equivalent to f(1,2) call, which invokes f(a,b) and results to 5 

```

I’m assuming method `f(b) = f(1,2b)` is implicit here? How do I call it? E.g. `f(,8)` didn’t work.  
@yuyichao @Michael_Eastwood

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 2, 2017, 3:54pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/2 "2017-06-02T15:54:09Z")

</div>

> [@hpoit](#):
>
> I’m assuming method f(b) = f(1,2b) is implicit here?

No.

> [@hpoit](#):
>
> How do I call it?

You can’t

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 4:16pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/3 "2017-06-02T16:16:27Z")

</div>

```julia
f(a=1,b=5,c=9,d=10) = a+3b+9c+8d

f(b,c) = f(1,3b,9c,10) # not possible either?

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 2, 2017, 4:31pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/4 "2017-06-02T16:31:48Z")

</div>

No. You are also writing the juxtapose syntax. The name of the position arguments has no significant at all. Use learn to do what you want

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 2, 2017, 4:34pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/5 "2017-06-02T16:34:00Z")

</div>

Or I should say it’s valid syntax, which you should just test yourself, but it’s unclear what you want to do other than overwriting a method

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 4:44pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/6 "2017-06-02T16:44:00Z")

</div>

Oh I wasn’t planning on overwriting anything, just trying to understand how optional args and their methods work. So optional args can only be called all at once, none at all, or from the first positional argument `a`. You can’t call more than one optional arg from say a tuple of five args. That’s my understanding and I don’t know the criteria for that decision.

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [June 2, 2017, 5:22pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/7 "2017-06-02T17:22:46Z")

</div>

> [@hpoit](#):
>
> That’s my understanding and I don’t know the criteria for that decision.

Presumably, it’s just a simple enough implementation of that concept. If you have more than 1 or 2 optional arguments, you should probably be using keyword arguments anyway.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 2, 2017, 5:23pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/8 "2017-06-02T17:23:44Z")

</div>

> [@hpoit](#):
>
> So optional args can only be called all at once, none at all, or from the first positional argument a. You can’t call more than one optional arg from say a tuple of five args. That’s my understanding and I don’t know the criteria for that decision.

Wrong. You can call it with any number of arguments. Missing arguments will use the default value.

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [June 2, 2017, 5:35pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/9 "2017-06-02T17:35:17Z")

</div>

Maybe an example would be helpful?

```julia
julia> f(a="default", b="default", c="default", d="default") = @show a b c d
f (generic function with 5 methods)

julia> f("not the default", "not the default", "not the default", "not the default");
a = "not the default"
b = "not the default"
c = "not the default"
d = "not the default"

julia> f("not the default", "not the default", "not the default");
a = "not the default"
b = "not the default"
c = "not the default"
d = "default"

julia> f("not the default", "not the default");
a = "not the default"
b = "not the default"
c = "default"
d = "default"

julia> f("not the default");
a = "not the default"
b = "default"
c = "default"
d = "default"

julia> f();
a = "default"
b = "default"
c = "default"
d = "default"

```

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 6:15pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/10 "2017-06-02T18:15:42Z")

</div>

This is what I wanted to know

```julia
julia> f(a="default",d="default"; b="default",c="default") = @show a b c d
f (generic function with 3 methods) # b and c are keyword args

julia> f(b="not default", c="not default");
a = "default"
b = "not default"
c = "not default"
d = "default"

```

Thanks!

---

<div class="post-metadata">

**Author:** ![vipstar](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@vipstar](https://discourse.julialang.org/u/vipstar)\
**Post date:** [June 2, 2017, 6:28pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/11 "2017-06-02T18:28:10Z")

</div>

You can only do that using keyword arguments. f uses only positional arguments and when using positional arguments, you can only give as input to the function the first k arguments with the last n-k arguments being the default values.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 6:36pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/12 "2017-06-02T18:36:30Z")

</div>

That’s even better, thanks! Then there’s no point in using keyword args. Just place all optionals at the beginning or end of list

```julia
julia> f( b="default",a="default", d="default",c="default") = @show a b c d
f (generic function with 5 methods)

julia> f(b="not default", c="not default");
a = "default"
b = "not default"
c = "not default"
d = "default"

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 2, 2017, 6:44pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/13 "2017-06-02T18:44:46Z")

</div>

Try restart Julia and do that again.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 2, 2017, 6:55pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/14 "2017-06-02T18:55:25Z")

</div>

> [@hpoit](#):
>
> That’s even better, thanks! Then there’s no point in using keyword args. Just place all optionals at the beginning or end of list

Please read and stick to the doc. kw args and positional args are totally different. You don’t need to find a syntax to make them the same.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 2, 2017, 7:00pm UTC](https://discourse.julialang.org/t/optional-args-and-the-methods-they-imply/4062/15 "2017-06-02T19:00:08Z")

</div>

Thanks Kris and Yichao, I’ll stick to keyword args to do what they were meant for.
