# Get default function arguments

**URL:** https://discourse.julialang.org/t/get-default-function-arguments/12612
**Category:** General Usage
**Tags:** question
**Created:** [July 23, 2018, 6:28pm UTC](https://discourse.julialang.org/t/get-default-function-arguments/12612 "2018-07-23T18:28:58Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![lewis500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis500/32/4729_2.png) [@lewis500](https://discourse.julialang.org/u/lewis500)
#### Post date: [July 23, 2018, 6:28pm UTC](https://discourse.julialang.org/t/get-default-function-arguments/12612/1 "2018-07-23T18:28:58Z")

</div>

If I have a function with optional arguments like

```julia
add(a=2;b=3)=a+b

```

is it possible to find out what the defaults are?

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 23, 2018, 7:07pm UTC](https://discourse.julialang.org/t/get-default-function-arguments/12612/2 "2018-07-23T19:07:00Z")

</div>

I don’t think so. Default arguments just result in Julia creating extra methods for your function. For example:

```julia
julia> f(x=5) = x + 1
f (generic function with 2 methods)

julia> methods(f)
# 2 methods for generic function "f":
f() in Main at REPL[1]:1
f(x) in Main at REPL[1]:1

```

Defining `f(x=5) = ...` actually defined two methods:

```julia
f(x) = ...
f() = f(5)

```

so inside the inner method (`f(x)`), there’s no indication whether the `x` it received was a default argument or not. Keyword arguments do something similar by creating an inner function without keyword arguments.

What are you actually trying to do? Perhaps there’s another way.

---

<div class="post-metadata">

### Author: ![lewis500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lewis500/32/4729_2.png) [@lewis500](https://discourse.julialang.org/u/lewis500)
#### Post date: [July 23, 2018, 7:15pm UTC](https://discourse.julialang.org/t/get-default-function-arguments/12612/3 "2018-07-23T19:15:58Z")

</div>

I have a simulation with certain default parameters. Then I want to double and halve these parameters and record the results. I can do this simply by putting all the parameters in a list but I wondered if I can also do it via writing something like

```julia
add(a=defaults(add).a*2)

```

or something.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 23, 2018, 7:54pm UTC](https://discourse.julialang.org/t/get-default-function-arguments/12612/4 "2018-07-23T19:54:32Z")

</div>

Ah, no, that’s not possible. After all, you could have arbitrarily many methods named `add`, so what would `defaults(add)` possibly return?

Instead, if you put your parameters in a `struct` and pass that struct to your function, manipulating the parameters as a group becomes much easier. Check out [https://github.com/mauro3/Parameters.jl](https://github.com/mauro3/Parameters.jl) for some macros to make constructing parameter structs easier.
