# Defining functions which use other functions

**URL:** <https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208>\
**Category:** New to Julia\
**Tags:** functions\
**Created:** [January 26, 2022, 1:24am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208 "2022-01-26T01:24:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [January 26, 2022, 1:24am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/1 "2022-01-26T01:24:43Z")

</div>

```julia
ΔK(ϕ,λ,Δσ,a,c,Q,f) = λ(ϕ,a,c) * Δσ * sqrt(π * a / Q(a,c)) * f(ϕ)
dadN(λ,Δσ,a,c,Q,f) = ΔK(ϕ=90, ...)

```

1. Should I include all functions, function inputs, and raw inputs in a higher level function signature, or is it better to just inherit some of these input values from scope?  
a. What’s the cutoff? Just have the function inherit all variables?
2. Is the second line the right way to define a subset of some function?
3. Is there a better way to name derivative variables like \frac{da}{dN}? Maybe a unicode fraction symbol that won’t be evaluated?

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 26, 2022, 1:36am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/2 "2022-01-26T01:36:59Z")

</div>

Looks good to me. For your second line, you’ll want `args...` instead of just `...` to avoid a syntax error.

Also, you can avoid `Q` being an input, unless you want it to be a placeholder to pass different functions that take `a, c` as inputs. Same goes for `f`.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 26, 2022, 6:37am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/3 "2022-01-26T06:37:13Z")

</div>

Maybe I’m misunderstanding, but let’s make a simpler example:

```julia
julia> f(a, b) = a/(a+b)

julia> g(a, b) = f(a, b)^2

julia> h(p, a, b) = p(a, b)^2

julia> f(1, 2)
0.3333333

julia> g(1, 2)
0.1111111

julia> h(f, 1, 2)
0.11111111

julia> h(+, 1, 2)
9

```

A function can simply use another function that is defined outside it (or indeed inside it but that’s for later). That’s what happens for `g`, it can just use `f`, no declarations needed.

If you need to be able to change which function is called inside your function, you can give it as an argument, like `h` does here. Putting the function argument first is a bit of a convention.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 26, 2022, 6:44am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/4 "2022-01-26T06:44:23Z")

</div>

When it comes to inheriting variables from scope, this is called global variables and it’s a bit of a code smell, it will probably lower your performance. The exception is `const`ants (like indeed those functions). If you notice you’re sending some constant into every function you may want to declare it a constant and remove it from the arguments.

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [January 26, 2022, 6:53am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/5 "2022-01-26T06:53:10Z")

</div>

Isn’t it the case that for functions that take other functions as arguments, they should be defined annotating the types?

```julia
function z(p::F, a, b) where F
    p(a, b)^2
end

```

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [January 26, 2022, 6:56am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/6 "2022-01-26T06:56:14Z")

</div>

If you _always_ want to specialize on the given function then yes. In your concrete example however I believe that Julia will anyway specialize on `p`, since you directly call `p` in the function body. See the performance docs for more info.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [January 26, 2022, 7:03am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/7 "2022-01-26T07:03:43Z")

</div>

Functions are automatically constants??

Also, won’t moving the functions to the front of the signature ruin the ability to use line 2?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [January 26, 2022, 7:11am UTC](https://discourse.julialang.org/t/defining-functions-which-use-other-functions/75208/8 "2022-01-26T07:11:39Z")

</div>

Yes, `f(x) = 3x` is roughly equivalent to `const f = x -> 3x`.

I’m not sure what you mean by line 2. You can of course ignore the convention if you have some reason to do something else, the reason it exists is that there’s do-block syntax.  
[https://docs.julialang.org/en/v1/manual/functions/#Do-Block-Syntax-for-Function-Arguments](https://docs.julialang.org/en/v1/manual/functions/#Do-Block-Syntax-for-Function-Arguments)
