# Redefined functions do not pass correctly as arguments

**URL:** <https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852>\
**Category:** General Usage\
**Tags:** question\
**Created:** [February 2, 2017, 7:30pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852 "2017-02-02T19:30:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mago876](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mago876](https://discourse.julialang.org/u/mago876)\
**Post date:** [February 2, 2017, 7:30pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/1 "2017-02-02T19:30:33Z")

</div>

Hi.

I’m having trouble with passing redefined functions as arguments of other functions (in my case, constantly redefined objetive functions to a minimizing method). The following behavior is observed in kernels 0.5.0 and 0.6.0-dev but not in 0.4.7 (I’m running a jupyter notebook at [juliabox.com](http://juliabox.com)).

A minimal example is the following:  
`h(f::Function, n::Int) = f() + n # Define a function taking some arguments f() = 10 # Define a function to be passed as argument h(f,1) # This output 11 correctly`

But if now we redefine “f”:  
`f() = 20 h(f,5) # This output 15!`

That is, the function h takes n correctly but not the new function f (it should return 25).  
For now, my solution is working on 0.4.7 but I wonder if there is an explanation for this behaviour (I already read [this](https://github.com/JuliaPlots/Plots.jl/issues/508) for example, but there is not a clear response).

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 2, 2017, 7:41pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/2 "2017-02-02T19:41:30Z")

</div>

this is [http://github.com/julialang/julia/issues/265](http://github.com/julialang/julia/issues/265) fixed on master

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 2, 2017, 7:50pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/3 "2017-02-02T19:50:05Z")

</div>

On julia v0.5 you can work around issue #265 by making `f()` an anonymous function instead:

```julia
julia> h(f::Function, n::Int) = f() + n
h (generic function with 1 method)

julia> f = () -> 10
(::#1) (generic function with 1 method)

julia> h(f, 1)
11

julia> f = () -> 20
(::#3) (generic function with 1 method)

julia> h(f, 5)
25

```

---

<div class="post-metadata">

**Author:** ![mago876](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@mago876](https://discourse.julialang.org/u/mago876)\
**Post date:** [February 2, 2017, 10:09pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/4 "2017-02-02T22:09:14Z")

</div>

But what if f is a complicated function in which their definition requires non-inline computations?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 2, 2017, 10:16pm UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/5 "2017-02-02T22:16:42Z")

</div>

Anywhere you can place a single expression in Julia, you can also place a block of code using a `begin...end` block:

```julia
julia> f = () -> begin
       println("hello world")
       5
       end
(::#1) (generic function with 1 method)

julia> f()
hello world
5

```

Of course, the short answer is, as @yuyichao said, that this fixed in julia master, so this hack won’t be required in the future.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 3, 2017, 9:48am UTC](https://discourse.julialang.org/t/redefined-functions-do-not-pass-correctly-as-arguments/1852/6 "2017-02-03T09:48:06Z")

</div>

You can still use the `function` syntax:

```julia
julia> f = function(x)
         "..."
         return x + 1
       end
(::#1) (generic function with 1 method)

julia> f(1)
2

```
