# Varying function calls in higher order functions

**URL:** <https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391>\
**Category:** New to Julia\
**Created:** [June 4, 2021, 1:27pm UTC](https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391 "2021-06-04T13:27:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luis/32/25789_2.png) [@Luis](https://discourse.julialang.org/u/Luis)\
**Post date:** [June 4, 2021, 1:27pm UTC](https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391/1 "2021-06-04T13:27:01Z")

</div>

Hello everyone,

I’m relatively new to Julia but came across a recurring problem in several projects now.

It often happens that I want to use the same general algorithm but with different functions inside. For example here I have an Euler Algorighm that integrates a function, which gets passed as an argument:

```julia
function euler(f,t0,tn,Δt,x₀,par)
	T = t0:Δt:tn
	F = Vector{typeof(x₀)}(undef,length(T))
	F[1] = x₀

	for (n,t) ∈ enumerate(T[2:end])
		F[n+1] = F[n] .+ f(F[n],par) .* Δt
	end
	
	return F
end

```

Different functions can have different numbers of parameters. Thats why I pass the “static” parameter as tuple. But there are cases, where the function that gets integrated needs also t as an argument, or the hole history of the process F or other “dynamical” parameters. Therefore or, I write the function call, and therefore all functions that I want to use the algorithm on in the most general way, which then becomes unreadable and hard to modify or I copy the outer algorithm - the euler function - and paste every time a different loop where I sometimes call `f(F[n],par)`, or` f(F,n,par)` or `f(t,F[n],par)` ect. But this code is replicative which I usually try to avoid.

Does anybody know an elegant solution to the general problem of? I hope I could explain myself. Please let me know if one does not understand the problem.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 4, 2021, 2:19pm UTC](https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391/2 "2021-06-04T14:19:59Z")

</div>

The static parameters can be more cleanly passed with closures:

```julia
const a = 5
g(x,a) = a*x
f(func,x) = func(x)

#call with
f(x -> g(x,a), x)

```

For `func` to have an arbitrary number of parameters I think you have to use something like:

```julia
const a = 5
g(x,y,a) = a*x*y

f(func,args...) = func(args...)

#call with
f((x,y)->g(x,y,a),x,y)

```

For the later I am not sure if I have the most common an clean solution though.

**add on:**

One intermediate alternative to the “args…” is to use an inner call with all possible parameters (if that is known), and then use a closure if less parameters are used:

```julia
julia> const a = 5
5

julia> g(x,a) = a*x
g (generic function with 1 method)

julia> function f(func,x) 
         t = 1
         func(x,t)
       end
f (generic function with 1 method)

julia> f((x,t) -> g(x,a), 2)
10

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [June 6, 2021, 11:31pm UTC](https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391/3 "2021-06-06T23:31:21Z")

</div>

EDIT: I should have read Leandro’s answer more carefully. What I suggest is basically his “add-on” suggestion.

> [@Luis](#):
>
> sometimes call `f(F[n],par)` , or ` f(F,n,par)` or `f(t,F[n],par)` ect. But this code is replicative which I usually try to avoid.

I think that you should have a single version of the `euler` function in which you call `f` passing everything you may need to pass, and then, if necessary, you wrap the function `f` that you pass to `euler` as a parameter in an anonymous function that discards the unused parameters.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 7, 2021, 1:23am UTC](https://discourse.julialang.org/t/varying-function-calls-in-higher-order-functions/62391/4 "2021-06-07T01:23:47Z")

</div>

Another possible solution is inversion of control: rather than having `euler()` both specify how to go from one time step to the next and do the time integration loop, you can have a function

```julia
euler_step(f,t,dt,x0) = x0 + f(t,x0) * dt

```

and then one or more functions

```julia
function integrate(step,f,t0,tn,dt,x0)
    x = x0
    for t in t0:dt:tn
        x = step(f,t,dt,x)
    end
    return x 
end

```

which do the actual time integration. This allows you to have multiple `integrate()` functions depending on e.g. whether or not you want to keep track of the history, but on the other hand doesn’t require duplicating the time stepping logic.
