# How to skip arguments when using a function

**URL:** https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621
**Category:** General Usage
**Tags:** question, diffeq, matlab, function, functions
**Created:** [September 23, 2021, 9:38am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621 "2021-09-23T09:38:00Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Giulio\_Benedetti](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Giulio\_Benedetti](https://discourse.julialang.org/u/Giulio_Benedetti)
#### Post date: [September 23, 2021, 9:38am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/1 "2021-09-23T09:38:00Z")

</div>

Hi there!

Is there a way in Julia to skip arguments when using a function? I am looking for something like the tilde in MATLAB:

```julia
# MATLAB
MyFunction(a, ~, c)

```

Thanks in advance!

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [September 23, 2021, 10:33am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/2 "2021-09-23T10:33:41Z")

</div>

So are we assuming that the second argument has some default value, and you want this default value to be used, but you still want to set the third value? I don’t think this exists in julia, and would say that this seems like a case where you should either define additional method signatures for the call to dispatch to or you should use keywords arguments instead of arguments.

Do you have control over the function you want to call, or is it a function from some library?

From googling a little it seems this does not exist for using functions in matlab, only for defining functions. So I’m not sure if I misunderstood you question, or maybe google wronged me?

---

<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: [September 23, 2021, 10:58am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/3 "2021-09-23T10:58:12Z")

</div>

Ciao Giulio, you can “skip” an argument in a function call (if this is what you are looking for…) if this argument is provided by a default value in the function definition. For positional arguments only the last parameters can have a default value, so say, you can’t skip the second out of three arguments. However, you can set any keyword argument to have a default argument and hence you can “skip” it.

For example I can define the function `foo(a,b=2,c=3;A=4,B,C=6) = a+10*b+100*c+1000*A+10000*B+100000*C` and then call it with, at minimum, `foo(1,B=5)` to obtain `654321`, or by using the whole set of arguments as `foo(1,2,3,A=4,B=5,C=6)`.

When I develop an API indeed, I tend to use keyword argument mostly for parameters that I expect to be rarely used outside their default value, so most people will not bother to use them.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [September 23, 2021, 11:29am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/4 "2021-09-23T11:29:03Z")

</div>

> [@Giulio\_Benedetti](#):
>
> ```julia
> # MATLAB
> MyFunction(a, ~, c)
> 
> ```

Can you provide a complete working example in Matlab? It would help a lot to understand what you want and find the best solution in Julia.

(My guess is that you want to ignore the input in the function definition, which you can do using `_` in Julia).

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 23, 2021, 11:56am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/5 "2021-09-23T11:56:34Z")

</div>

You can do the same thing using `_` in Julia:

```julia
julia> f(x,_,y) = 5x + 2y
f (generic function with 1 method)

julia> f(2,3,10)
30

```

---

<div class="post-metadata">

### Author: ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)
#### Post date: [September 24, 2021, 2:37am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/6 "2021-09-24T02:37:07Z")

</div>

That works, but why would one want to do it? What’s the use case?

---

<div class="post-metadata">

### Author: ![Giulio\_Benedetti](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Giulio\_Benedetti](https://discourse.julialang.org/u/Giulio_Benedetti)
#### Post date: [September 24, 2021, 8:10am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/7 "2021-09-24T08:10:01Z")

</div>

Hello, thanks for your reply!

Actually, I am defining a function with two methods:

```julia
# method 1
FDEsolver(F, tSpan, y0, β, par...)

```

```julia
method 2
FDEsolver(F, tSpan, y0, β, JacobF, par...)

```

But because they both accept a vararg as last positional parameter, the method with fewer parameters is getting overwritten by the second method. In other words, when I use method 1 with the vararg `par...`, instead of using method 1 it uses method 2 with `JacobF` in place of `par...`.

So I thought maybe I could define Jacob in both methods and skip it when I want to use method 1. I hope it helps to understand a little bit.

---

<div class="post-metadata">

### Author: ![Giulio\_Benedetti](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Giulio\_Benedetti](https://discourse.julialang.org/u/Giulio_Benedetti)
#### Post date: [September 24, 2021, 8:17am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/8 "2021-09-24T08:17:52Z")

</div>

Thanks for replying! So could I define two methods like this?

```julia
f(a, b, c, d) = a + b + c + d

```

```julia
f(a, _, _, d) = a + d

```

I know it’s a dumb example and it would be easier to define one method with 2 arguments and another with 4, but that is giving me troubles because my two methods both can accept a vararg and the one with 3 args plus vararg gets overwritten by the second method with 4 args plus vararg when this does not have vararg.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [September 24, 2021, 8:55am UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/9 "2021-09-24T08:55:09Z")

</div>

Your second definition replaces the first (the names don’t matter, only the types so here in both cases you have “4 generic arguments”).

Here are some possibilities:

1. Users can give `nothing` for `JacobF`:

2. Make the second method more specific by giving a type for `JacobF`, for example:

3. Get rid of the vararg. What are these parameters? maybe there’s a better way to pass them than using a vararg?

4. Pass `JacobF` as an optional keyword argument:

---

<div class="post-metadata">

### Author: ![Giulio\_Benedetti](https://avatars.discourse-cdn.com/v4/letter/g/f0a364/32.png) [@Giulio\_Benedetti](https://discourse.julialang.org/u/Giulio_Benedetti)
#### Post date: [September 24, 2021, 5:51pm UTC](https://discourse.julialang.org/t/how-to-skip-arguments-when-using-a-function/68621/10 "2021-09-24T17:51:56Z")

</div>

Hello! Thanks a lot for the help.

Solution 1 worked out and now I am trying to advance it to solution 4.
