# How to pass in a function as an argument to a function?

**URL:** <https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816>\
**Category:** General Usage\
**Tags:** function\
**Created:** [February 23, 2021, 1:34am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816 "2021-02-23T01:34:16Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 23, 2021, 1:34am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/1 "2021-02-23T01:34:16Z")

</div>

Hello

So I have this function -

```julia
function late_time_value(slope, x_init::Float64, y_init::Float64, dx::Float64 = 0.01, tf::Float64 = 100)
    N = floor(Integer, tf/dt)
    sol_x = zeros(N)
    sol_y = zeros(N)
    
    sol_x[1] = x_init
    sol_y[1] = y_init
    
    for i in 1:N-1
        sol_x[i+1] = sol_x[i] + dx
        sol_y[i+1] = sol_y[i] + slope(sol_y[i]) * dx
    end
    
    return sol_x, sol_y
end

```

And for `slope` I passed in a function `slope` defined as `slope(x) = sin(x)`. But Julia throws this error -

```julia
julia> late_time_value(slope, 0., 0.5)
MethodError: no method matching late_time_value(::typeof(slope), ::Float64, ::Float64, ::Float64, ::Int64)
Closest candidates are:
  late_time_value(::Any, ::Float64, ::Float64, ::Float64) at In[5]:1
  late_time_value(::Any, ::Float64, ::Float64, ::Float64, !Matched::Float64) at In[5]:1
  late_time_value(::Any, ::Float64, ::Float64) at In[5]:1

Stacktrace:
 [1] late_time_value(slope::Function, x_init::Float64, y_init::Float64)
   @ Main ./In[5]:2
 [2] top-level scope
   @ In[8]:1
 [3] eval
   @ ./boot.jl:360 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1090

```

However when I do something a bit more simple it works.

```julia
julia> function f(s)
           return s()
       end
f (generic function with 1 method)

julia> g() = 2
g (generic function with 1 method)

julia> f(g)
2

```

What’s going on here? I would appreciate any help

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 23, 2021, 1:37am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/2 "2021-02-23T01:37:25Z")

</div>

The error message is a bit confusing.

You have `tf::Float64 = 100`, but it should be `tf::Float64 = 100.0`.

The error message gives a clue in the last argument: `late_time_value(::typeof(slope), ::Float64, ::Float64, ::Float64, ::Int64)`.

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 23, 2021, 1:48am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/3 "2021-02-23T01:48:32Z")

</div>

Yep, that was it. My mistake, thanks. After setting `df::Float64 = 100.` it runs successfully.  
Strange, I wouldn’t have expected that to affect anything, but I guess it makes sense with multiple dispatch.

Thank you for the help!

---

<div class="post-metadata">

**Author:** ![lstagner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lstagner/32/448_2.png) [@lstagner](https://discourse.julialang.org/u/lstagner)\
**Post date:** [February 23, 2021, 1:57am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/4 "2021-02-23T01:57:44Z")

</div>

You also may want to remove the type annotations all together. They don’t improve the performance and it makes the code less general. General rule of thumb I follow is to avoid type annotations unless your code specifically needs them in order to run correctly. In your example the only type annotation I would have is an annotation on the slope variable to constrain it to be a function e.g. `slope::Function`

I’m surprised “Don’t overuse type annotations” isn’t in the style guide.

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 23, 2021, 2:14am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/5 "2021-02-23T02:14:51Z")

</div>

Ah I wasn’t aware that they don’t improve performance. I was under the impression that Julia would use those type annotations to make further optimizations on compile time.

Does it also not matter for arrays either? Like is doing an operation on a `Vector{Float64}` and `Vector{Integer}` the same in terms of the machine code generated by Julia?

---

<div class="post-metadata">

**Author:** ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)\
**Post date:** [February 23, 2021, 2:19am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/6 "2021-02-23T02:19:03Z")

</div>

Julia will specialize for typing whether you annotate or not. Annotations help in eliminating type instabilities. Although you can have type stable code without them. You can check for such instabilities with [@code\_warntype](https://docs.julialang.org/en/v1/manual/performance-tips/#man-code-warntype).

---

<div class="post-metadata">

**Author:** ![WschW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wschw/32/6575_2.png) [@WschW](https://discourse.julialang.org/u/WschW)\
**Post date:** [February 23, 2021, 2:28am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/7 "2021-02-23T02:28:25Z")

</div>

For inputs to functions Julia can figure out the types and specialize on them while compiling.

For defining structs or calling constructors it does affect performance.

A `Vector` created with `Vector{Int}(undef, x)` will be faster then one created with `Vector{Integer}(undef, x)`. As in the latter case Julia has to be able to handle any type of Integer.

Likewise

```julia
struct MyStruct
  x
end

```

will be slower then

```julia
struct MyStruct{T}
  x::T
end

```

Also, I would avoid constraining to a function as that won’t work with most callable structs.

---

<div class="post-metadata">

**Author:** ![cocoa1231](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cocoa1231/32/22215_2.png) [@cocoa1231](https://discourse.julialang.org/u/cocoa1231)\
**Post date:** [February 23, 2021, 2:30am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/8 "2021-02-23T02:30:33Z")

</div>

That makes sense. Thank you for the help!!

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [February 23, 2021, 3:48am UTC](https://discourse.julialang.org/t/how-to-pass-in-a-function-as-an-argument-to-a-function/55816/9 "2021-02-23T03:48:10Z")

</div>

This is a good read: [Performance Tips · The Julia Language](https://docs.julialang.org/en/v1/manual/performance-tips/index.html)
