# Inline functions with default parameters

**URL:** <https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548>\
**Category:** General Usage\
**Tags:** functions\
**Created:** [August 24, 2024, 4:02am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548 "2024-08-24T04:02:08Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martin\_sanchez](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/martin_sanchez/32/207525_2.png) [@martin\_sanchez](https://discourse.julialang.org/u/martin_sanchez)\
**Post date:** [August 24, 2024, 4:02am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/1 "2024-08-24T04:02:08Z")

</div>

I have a doubt about default arguments. In the [Functions documentation](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) it says that I can use optional arguments aswell as defining a function with a simplyfied syntax of  
y(x)=x\*\*2, but can’t I have such a function with optional parameters? For example this works

```julia
t=1:0.1:10
y(t,v_0=1,a=-9.8,y_0=0)=y_0+v_0*t+a/2*t^2 

```

If I call it with

```julia

y_vec=y.(t)

```

But not if I call it with

```julia

y_vec=y.(t,v_0=10)

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 24, 2024, 4:06am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/2 "2024-08-24T04:06:35Z")

</div>

You should include error messages:

```julia
julia> y.(t,v_0=10)
ERROR: MethodError: no method matching y(::Float64; v_0::Int64)

Closest candidates are:
  y(::Any, ::Any, ::Any, ::Any) got unsupported keyword argument "v_0"
   @ Main REPL[11]:1
  y(::Any, ::Any, ::Any) got unsupported keyword argument "v_0"
   @ Main REPL[11]:1
  y(::Any, ::Any) got unsupported keyword argument "v_0"
   @ Main REPL[11]:1
  ...

Stacktrace:
 [1] (::Base.Broadcast.var"#43#44"{@Kwargs{v_0::Int64}, typeof(y)})(args::Float64)
   @ Base.Broadcast .\broadcast.jl:1327
 [2] _broadcast_getindex_evalf
   @ .\broadcast.jl:709 [inlined]
 [3] _broadcast_getindex
   @ .\broadcast.jl:682 [inlined]
 [4] getindex
   @ .\broadcast.jl:636 [inlined]
 [5] copy
   @ .\broadcast.jl:942 [inlined]
 [6] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{1}, Nothing, Base.Broadcast.var"#43#44"{@Kwargs{…}, typeof(y)}, Tuple{StepRangeLen{…}}})
   @ Base.Broadcast .\broadcast.jl:903
 [7] top-level scope
   @ REPL[13]:1
Some type information was truncated. Use `show(err)` to see complete types.

```

Your last argument is call is treating `v_0` as if it were a keyword argument, instead of a positional argument. You should just do `y.(t, 10)`, or change the function to use keyword arguments in the definition.

---

<div class="post-metadata">

**Author:** ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)\
**Post date:** [August 24, 2024, 5:26am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/3 "2024-08-24T05:26:00Z")

</div>

Oh I fell for that as well recently. though there is a `,` in `y_vec=y.(t,v_0=10)` the `v_0=` makes Julia start the “keyword section”, so what you wrote is the same as `y_vec=y.(t; v_0=10)`. I felt accepting a `,` here as well is maybe misleading.

As Daniel wrote all your arguments are positional (optional) and no keywords. Besides that I am not 100% sure how keywords work together with your broadcasting, but that is maybe a next point to check then.

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 24, 2024, 5:41am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/4 "2024-08-24T05:41:36Z")

</div>

> [@kellertuer](#):
>
> Besides that I am not 100% sure how keywords work together with your broadcasting, but that is maybe a next point to check then.

The keyword arguments are not broadcasted over, as far as I can tell:

```julia-auto
julia> function f(x; v)
       @show x, v
       end
f (generic function with 2 methods)

julia> f.([1, 2, 3]; v = [1, 2, 3])
(x, v) = (1, [1, 2, 3])
(x, v) = (2, [1, 2, 3])
(x, v) = (3, [1, 2, 3])

julia> f.(1, v = [1, 2, 3])
(x, v) = (1, [1, 2, 3])
(1, [1, 2, 3])

```

This is handled in `Base.broadcasted_kwsyntax`:

```julia-auto
julia> Meta.@lower f.([1, 2, 3]; v = [1, 2, 3])
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = Base.vect(1, 2, 3)
│ %2 = Base.broadcasted_kwsyntax
│ %3 = Core.tuple(:v)
│ %4 = Core.apply_type(Core.NamedTuple, %3)
│ %5 = Base.vect(1, 2, 3)
│ %6 = Core.tuple(%5)
│ %7 = (%4)(%6)
│ %8 = Core.kwcall(%7, %2, f, %1)
│ %9 = Base.materialize(%8)
└── return %9
))))

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [August 24, 2024, 10:20am UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/6 "2024-08-24T10:20:17Z")

</div>

The NamedPositionals.jl package allows function calls closer to OP’s intent:

```julia
using NamedPositionals
y1 = (u->@np y(u, v_0=10;)).(t)
y2 = (u->@np y(u, v_0=5, a=-1.62;)).(t)
y3 = (u->@np y(u, v_0=1, a=-9.8, y_0=7.5;)).(t)

```

I could not get broadcasting to work with the @np macro, hence the broadcasting of anonymous functions above.

But difficult to see the advantage of this compared to the simple calls:

```julia
v_0 = 10
y1b = y.(t, v_0)

v_0 = 0.1; a = -1.62
y2b = y.(t, v_0, a)

v_0 = 1; a = -9.8; y_0 = 7.5
y3b = y.(t, v_0, a, y_0)

```

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [August 24, 2024, 1:37pm UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/7 "2024-08-24T13:37:47Z")

</div>

An orthogonal point to consider - I often find that when I’m writing function signatures like that, it’s probably time to make a struct that encodes a bunch of those parameters 🤷

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [August 24, 2024, 1:58pm UTC](https://discourse.julialang.org/t/inline-functions-with-default-parameters/118548/9 "2024-08-24T13:58:06Z")

</div>

See also [Keyword vs Optional Function Arguments? - #3 by digital\_carver](https://discourse.julialang.org/t/keyword-vs-optional-function-arguments/86065/3)
