# Functions in function

**URL:** https://discourse.julialang.org/t/functions-in-function/86958
**Category:** General Usage
**Created:** [September 8, 2022, 6:56pm UTC](https://discourse.julialang.org/t/functions-in-function/86958 "2022-09-08T18:56:23Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 6:56pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/1 "2022-09-08T18:56:23Z")

</div>

I have 2 functions:

```julia
f1(a1; a2=1, a3="a") # a4, a5, ...
f2(b1; b2='k', b3=2.0) # b4, b5, ...

```

I want to write a function which includes both functions and accepts all parameters of functions. What is the best way to do this? Do I have to write all the arguments of f1 and f2 in the function?  
Thank you

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [September 8, 2022, 7:12pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/2 "2022-09-08T19:12:16Z")

</div>

Something like that?

```julia
function outerfunc(a1, a2, a3, b1, b2, b3)
      function f1(a1, a2 = a2, a3 = "a") 
            # a4 a5, ....
      end 

      function f2(b1, b2 = b2, b3 = b3) 
            # b4, b5, ....
      end 
 
      # possibly 
      # ... = f1(a1, a2 = a2, a3 = a3)
      # ... = f2(b1, b2 = b2, b3 = b3)
end

```

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 7:15pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/3 "2022-09-08T19:15:18Z")

</div>

f1 and f2 are defined outside.  
Inside outerfunc I call f1 and f2.  
I want outsidefunc accept parameters for f1 and f2

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [September 8, 2022, 7:17pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/4 "2022-09-08T19:17:53Z")

</div>

Does you outer function take function names as arguments or the names of the functions are exactly defined at runtime, e.g. f1 and f2?

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 7:53pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/5 "2022-09-08T19:53:31Z")

</div>

```julia
f1(a1; a2=1, a3="a") # a4, a5, ...
f2(b1; b2='k', b3=2.0) # b4, b5, ...

function f(x, y, z, a1, a2, a3, b1, b2, b3)
    # code
    f1(a1, a2=a2, a3=a3) 
    # code
    f2(b1, b2=b2, b3=b3)
    #code
end

```

It doesn’t look elegant and doesn’t scale if there are many functions.  
Is there a way to take parameters for functions f1, f2 without explicitly writing them in f?

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [September 8, 2022, 7:56pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/6 "2022-09-08T19:56:02Z")

</div>

maybe you can use three-dots like

```julia
function f(params...)
end

```

and set all of the arguments of `f1` and `f2` as optional.

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 8:00pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/7 "2022-09-08T20:00:03Z")

</div>

f1 and f2 are in a Pkg. I didn’t write them.  
I have to call them in a function but I want this function to accept the parameters for f1 and f2 as well.

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [September 8, 2022, 8:00pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/8 "2022-09-08T20:00:57Z")

</div>

you can always wrap foreign functions

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 8:04pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/9 "2022-09-08T20:04:44Z")

</div>

Can you give me an example?

I’d like to do something like this:

```julia
function f(x, y, z, a1, b1; params...)
    # code
    f1(a1; params...) 
    # code
    f2(b1; params...)
    #code
end

```

and functions choose only parameters that are compatible.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [September 8, 2022, 8:26pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/10 "2022-09-08T20:26:56Z")

</div>

That works fine if `f1` and `f2` are defined as

```julia
f1(a1; a2=1, a3="a", _...) # a4, a5, ...
f2(b1; b2='k', b3=2.0, _...) # b4, b5, ...

```

If they are defined outside your control your best bet is probably to write wrapper functions which slurp the unused keyword arguments.

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 8, 2022, 10:17pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/11 "2022-09-08T22:17:46Z")

</div>

This works:

```julia
# examples
f1(a1; a2=1, a3="a") = (a1, a2, a3)
f2(b1; b2='k', b3=2.0) = (b1, b2, b3) 

function namedArgumentsOf(func, args...)
    func_args = @which func(args...)
    Base.kwarg_decl(func_args)
end 

function f(a1, b1; params...)
    args_f1 = [i for i in params if i[1] in namedArgumentsOf(f1, 1)]
    args_f2 = [i for i in params if i[1] in namedArgumentsOf(f2, 1)]

    f1(a1; args_f1...) |> println
    f2(b1; args_f2...) |> println
    
end

f(3, 5; a2=4, a3=7, b2='e')

```

---

<div class="post-metadata">

### Author: ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)
#### Post date: [September 8, 2022, 10:38pm UTC](https://discourse.julialang.org/t/functions-in-function/86958/12 "2022-09-08T22:38:18Z")

</div>

Nice!

A tiny detail for multiple dispatch to work, probably is should be `namedArgumentsOf(f1, a1)` (and similar for `f2`) to ensure that it uses the right method depending on the type of `a1`.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [September 9, 2022, 5:09am UTC](https://discourse.julialang.org/t/functions-in-function/86958/13 "2022-09-09T05:09:21Z")

</div>

I would perhaps try

```julia
function f(x, y, z, a1, b1, tup1, tup2)
    f1(a1; tup1...) 
    f2(b1; tup2...)
end

f(x, y, a1, b1, (a2=4, a3=7), (b2='e', b3=11))

```

---

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)
#### Post date: [September 9, 2022, 8:54am UTC](https://discourse.julialang.org/t/functions-in-function/86958/14 "2022-09-09T08:54:00Z")

</div>

I like your solution. My final code is:

```julia
f1(a1; a2=1, a3="a") = (a1, a2, a3)
f2(b1; b2='k', b3=2.0) = (b1, b2, b3) 
function f(a1, b1; args_f1=(), args_f2=())
    f1(a1; args_f1...) |> println
    f2(b1; args_f2...) |> println    
end

f(3, 5; args_f1=(a2=4, a3=7), args_f2=(b3='e',)) 
f(3, 5; args_f1=(a2=4, a3=7))
f(3, 5; args_f2=(b3='e',))
f(3, 5; args_f2=())
f(3, 5)

```

* * *

The only problem is:

```julia
function f(a1, b1; args_f1=(a2=3, a3="b"), args_f2=()) # If I declare args different from the default ones of f1 (or f2)
    f1(a1; args_f1...) |> println
    f2(b1; args_f2...) |> println     
end
f(1, 2; args_f1=(a2=4,)) # I don't get a3 = "b"
f(1, 2; args_f1=(a2=4,a3="b")) # I have to write a3="b" again

```
