# Setting default arguments for a function passed to another function

**URL:** https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712
**Category:** General Usage
**Created:** [June 22, 2023, 7:57pm UTC](https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712 "2023-06-22T19:57:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![dlcole3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlcole3/32/211441_2.png) [@dlcole3](https://discourse.julialang.org/u/dlcole3)
#### Post date: [June 22, 2023, 7:57pm UTC](https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712/1 "2023-06-22T19:57:02Z")

</div>

I am defining a function that takes a keyword argument. I am setting a default function for this keyword argument, but I would like to also pass a default keyword to that second function. I am not sure how to do this.

For example, I would like to have something like:

```julia
function foo(X; fn = Statistics.mean)
    fn(X)
end

```

However, I would like to also set as a default the keyword argument `dims = 1` for `Statistics.mean`. In other words, if the user calls `foo(X)`, I want it to return `Statistics.mean(x; dims = 1)`. As a user may pass a function that does not take a keyword argument `dims = 1`, I cannot add that directly to the line where `fn` is called. Is there a way to set a default argument of a default function on a keyword argument?

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [June 22, 2023, 8:13pm UTC](https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712/2 "2023-06-22T20:13:44Z")

</div>

First, welcome!  
You can just pass keyword arguments to the alias(?) name. You could also just trap the error if it is thrown if the user passes in a function that doesn’t have the needed keyword. Not necessarily a great way to do it, but if it absolutely needs to be designed this way …

```julia
function bar(X; baz)
    println("barring $X with $baz")
end

function zap(X)
    println("zapping $X")
end

function foo(X; fn=bar)
    try
        fn(X, baz="hi")
    catch e
        if isa(e, MethodError)
            @warn "you probably need to pass in a `fn` that has a `baz` keyword?"
        end
    end
end

foo(3)
foo(3, fn=zap)

```

prints:

```julia
barring 3 with hi
┌ Warning: you probably need to pass in a `fn` that has a `baz` keyword?
└ @ Main ~/Projects/Julia_C/C/stuff.jl:14

```

---

<div class="post-metadata">

### Author: ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)
#### Post date: [June 22, 2023, 8:35pm UTC](https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712/3 "2023-06-22T20:35:03Z")

</div>

If the default keyword argument only applies to the default function, just define that function accordingly, i.e.,

```julia
default_mean(x) = Statistics.mean(x; dims = 1)
function foo(X; fn = default_mean) fn(X) end

```

or simply

```julia
foo(X; fn = arg -> Statistics.mean(arg; dims = 1)) = fn(X)

```

---

<div class="post-metadata">

### Author: ![dlcole3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlcole3/32/211441_2.png) [@dlcole3](https://discourse.julialang.org/u/dlcole3)
#### Post date: [June 22, 2023, 8:43pm UTC](https://discourse.julialang.org/t/setting-default-arguments-for-a-function-passed-to-another-function/100712/4 "2023-06-22T20:43:55Z")

</div>

Thank you. That works great
