# Is it possible to define a default argument value based on the function's internal variables at the moment of return?

**URL:** https://discourse.julialang.org/t/is-it-possible-to-define-a-default-argument-value-based-on-the-functions-internal-variables-at-the-moment-of-return/48409
**Category:** Internals & Design
**Tags:** question
**Created:** [October 15, 2020, 5:55am UTC](https://discourse.julialang.org/t/is-it-possible-to-define-a-default-argument-value-based-on-the-functions-internal-variables-at-the-moment-of-return/48409 "2020-10-15T05:55:17Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![kapple](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kapple/32/218915_2.png) [@kapple](https://discourse.julialang.org/u/kapple)
#### Post date: [October 15, 2020, 5:55am UTC](https://discourse.julialang.org/t/is-it-possible-to-define-a-default-argument-value-based-on-the-functions-internal-variables-at-the-moment-of-return/48409/1 "2020-10-15T05:55:17Z")

</div>

Would it possible and appropriate for Julia to extend functionality of function definitions to include ones like this?

```julia
function f(x, z = y^2)
    y = 2x
    return x + y + z
end

```

It would need to evaluate the default value of `z` as the (second) last command for the function `f` since the value of its dependent variable `y` could get changed anytime during the function call.

Is it possible and appropriate to design such functionality? I feel like there may be potential clashes somehow (which I haven’t been able to think of yet, but maybe something like default interdependent variables e.g. `function g(x, z = 2y, y = 3z) ... end`) but those could be parsed as errors.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [October 15, 2020, 6:07am UTC](https://discourse.julialang.org/t/is-it-possible-to-define-a-default-argument-value-based-on-the-functions-internal-variables-at-the-moment-of-return/48409/2 "2020-10-15T06:07:28Z")

</div>

```julia
function f(x, z = nothing)
   y = 2x
   tmp = z === nothing ? y^2 : z
   return x + y + tmp
end

```

Because `y` only exists in the function body, it can’t be used in the default arguments. Default arguments can depend on other arguments though:

```julia
julia> f(a, b=2a) = b
f (generic function with 2 methods)

julia> f(1)
2

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 15, 2020, 2:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-define-a-default-argument-value-based-on-the-functions-internal-variables-at-the-moment-of-return/48409/4 "2020-10-15T14:46:26Z")

</div>

Also, if you are OK with keyword arguments and y visible to the user, you can do

```julia
f(x; y = 2*x, z = abs2(y)) = x + y + z
f(x)
f(x; z = 3)

```
