# ForwardDiff saving unwanted variable

**URL:** <https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928>\
**Category:** General Usage\
**Tags:** question, differentiation\
**Created:** [January 3, 2020, 3:11pm UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928 "2020-01-03T15:11:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Caio\_Machado](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caio_machado/32/10391_2.png) [@Caio\_Machado](https://discourse.julialang.org/u/Caio_Machado)\
**Post date:** [January 3, 2020, 3:11pm UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928/1 "2020-01-03T15:11:03Z")

</div>

Hi everyone,

I am migrating from Matlab to Julia and I have noticed some weird (perhaps unwanted?) behavior of the ForwardDiff package.

```julia
using ForwardDiff
function eq()
    
    function qfun(x)
        q = x[1] + x[2]
        return q
    end
   
    q = qfun([1.0, 2.0])
    
    derq(x) = ForwardDiff.gradient(qfun,x)
    dq = derq([1.0, 2.0])
    
    return q
end

a = eq()

```

This does not return 3.0 but instead:

```julia
Dual{ForwardDiff.Tag{var"#qfun#17",Float64}}(3.0,1.0,1.0)

```

However, if I change the name of q inside qfun I get what I expected (3.0 as output):

```julia
using ForwardDiff
function eq()
    
    function qfun(x)
        q2 = x[1] + x[2]
        return q2
    end
   
    q = qfun([1.0, 2.0])
    
    derq(x) = ForwardDiff.gradient(qfun,x)
    dq = derq([1.0, 2.0])
    
    return q
end

a = eq()

```

```julia
3.0

```

Also, if I run this code outside a function, I get an output of 3.0 to q:

```julia
using ForwardDiff
function qfun(x)
    q = x[1] + x[2]
    return q
end
   
q = qfun([1.0, 2.0])
    
derq(x) = ForwardDiff.gradient(qfun,x)
dq = derq([1.0, 2.0])
    
q

```

```julia
3.0

```

What is going on? Is this a bug or am I missing something from some expected behavior of ForwardDiff? It sounds super weird to me that the label used inside a function can affect the final output.

Best,

Caio

---

<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:** [January 3, 2020, 3:42pm UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928/2 "2020-01-03T15:42:58Z")

</div>

This isn’t a bug, and it doesn’t actually have anything to do with ForwardDiff. Here’s a simpler example of the same behavior:

```julia
julia> function outer()
         function inner()
           x = 1
         end
         
         x = 2
         inner()
         return x
       end
outer (generic function with 1 method)

julia> outer()
1

```

The call to `inner()` sets `x = 1`, and the `x` inside `inner()` is the same one in `outer()`.

You’re seeing this happen because `ForwardDiff.gradient(qfun, ...)` results in `qfun` being called, which has the side-effect of modifying the local variable `q`.

You are correct that this doesn’t happen at global scope:

```julia
julia> function inner()
         x = 1
       end
inner (generic function with 1 method)

julia> x = 2
2

julia> inner()
1

julia> x
2

```

Global scope is a bit different in Julia–if you want to re-assign a global variable from within a function or a loop, you need to be explicit and use the `global` keyword:

```julia
julia> function inner()
         global x
         x = 1
       end
inner (generic function with 1 method)                                                                                                                                                                                                                                                                                                
                                                                                                                                                                                                                                                                                                                                      
julia> x = 2
2

julia> inner()
1

julia> x
1

```

---

<div class="post-metadata">

**Author:** ![Caio\_Machado](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caio_machado/32/10391_2.png) [@Caio\_Machado](https://discourse.julialang.org/u/Caio_Machado)\
**Post date:** [January 3, 2020, 4:02pm UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928/3 "2020-01-03T16:02:18Z")

</div>

After checking the FAQ about the scope of variables and your answer I think I got it, thanks.

In fact, I would not like the variables to be reassigned when I call ForwarDiff to compute the derivatives (this is the only reason why qfun needs to be defined in my code). One option is to rename “q”, but this is costly, since in my actual code there are many variables being reassigned, and they appear many times in a formula. The best solution I could find was declare “q” local inside the function, which gives 3.0 as output.

```julia
using ForwardDiff
function eq()
    
    function qfun(x)
        local q
        q = x[1] + x[2]
        return q
    end
   
    q = qfun([1.0, 2.0])
    
    derq(x) = ForwardDiff.gradient(qfun,x)
    dq = derq([1.0, 2.0])
    
    return q
end

a = eq()

```

---

<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:** [January 3, 2020, 6:00pm UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928/4 "2020-01-03T18:00:40Z")

</div>

Does `qfun` actually have to live inside `eq()`? If not, then this problem is easy to avoid: you could simply do:

```julia
using ForwardDiff

function qfun(x)
    q = x[1] + x[2]
    return q
end

function eq()
    q = qfun([1.0, 2.0])
    
    derq(x) = ForwardDiff.gradient(qfun,x)
    dq = derq([1.0, 2.0])
    
    return q
end

a = eq()

```

---

<div class="post-metadata">

**Author:** ![Caio\_Machado](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/caio_machado/32/10391_2.png) [@Caio\_Machado](https://discourse.julialang.org/u/Caio_Machado)\
**Post date:** [January 4, 2020, 3:12am UTC](https://discourse.julialang.org/t/forwarddiff-saving-unwanted-variable/32928/5 "2020-01-04T03:12:51Z")

</div>

In my application it should be inside, since the inner function changes depending on some arguments of the outer function (which is not implied in my minimal working example). Otherwise what you propose would work too.
