# Using kwargs and non kwargs in a function call

**URL:** <https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518>\
**Category:** New to Julia\
**Tags:** kwargs\
**Created:** [April 13, 2020, 5:21pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518 "2020-04-13T17:21:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![birodaniel8](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@birodaniel8](https://discourse.julialang.org/u/birodaniel8)\
**Post date:** [April 13, 2020, 5:21pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/1 "2020-04-13T17:21:41Z")

</div>

Hi al,

I am new at programming in Julia, but I am struggling to implement and call a function in a particular way, which I was using a lot in Python. I have a function like:

```julia
function f(x, y=1, z=3; kwargs...)
    return x + y + z
end

```

however if I call it as f(5,6,z=10), it returns 14 as the ‘z=10’ is ignored and the default value is used. If I change the function having the semicolon after ‘x’, I still get the same results. What should I change to get 21 if I call it as f(5,6,z=10).

---

<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:** [April 13, 2020, 5:29pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/2 "2020-04-13T17:29:18Z")

</div>

Put the semicolon before `z`.

---

<div class="post-metadata">

**Author:** ![birodaniel8](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@birodaniel8](https://discourse.julialang.org/u/birodaniel8)\
**Post date:** [April 13, 2020, 6:03pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/3 "2020-04-13T18:03:53Z")

</div>

Hi Kristoffer,  
Thanks and that actually solves it for this particular case but my question would be how to do it more generally (but I know I did not formulate it that way).

Let’s suppose I have more inputs and I am calling the function in many different ways:

```julia

function f(x, y=1, z=1, w=1, q=1, kwargs...)
    return x + y + z + w + q
end

f(0,y=2,z=2,w=2,q=2)
f(0,2,z=2,w=2,q=2)
f(0,2,2,w=2,q=2)
f(0,2,2,2,q=2)
f(0,2,2,2,2)

```

What should I do to have “8” as output for all the above calls?

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [April 13, 2020, 6:46pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/4 "2020-04-13T18:46:55Z")

</div>

Unlike Python, Julia makes a distinction between positional and keyword arguments. You can’t pass the same argument either through position or through name. In particular, positional arguments participate in dispatch, selecting the specific method invoked, and keyword arguments do not.

However, if you have only one method, you can emulate this with something like

```julia
f(x, _y=1; y=_y) = (x,y)

```

or, if you want to disallow calls like `f(1,2,y=2)`

```julia
function f(x, _y=nothing; y=nothing)
    if isnothing(_y) && isnothing(y)
        y = 1
    elseif !isnothing(_y) && !isnothing(y)
        error("two values assigned to y")
    else
        y = something(y,_y)
    end
    (x,y)
end

```

You can also dispatch on the types of `(x,y)` using `f(x, _y=1; y=_y) = _f(x,y)` and doing the dispatch in `_f`.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [April 13, 2020, 7:22pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/5 "2020-04-13T19:22:21Z")

</div>

There is a discussion about this here: [Allow use of named-argument syntax for positional arguments? - #21 by DNF](https://discourse.julialang.org/t/allow-use-of-named-argument-syntax-for-positional-arguments/5287/21)

In my opinion allowing this would be a bad thing in general.

---

<div class="post-metadata">

**Author:** ![birodaniel8](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@birodaniel8](https://discourse.julialang.org/u/birodaniel8)\
**Post date:** [April 13, 2020, 8:33pm UTC](https://discourse.julialang.org/t/using-kwargs-and-non-kwargs-in-a-function-call/37518/6 "2020-04-13T20:33:51Z")

</div>

Thank you for the quick help!
