The problem is that you are modifying variable outside of the scope of the gradient, which is something AD is not happy with. If possible, I would rewrite the code as
f(x) = x^2
r, gs = Flux.withgradient(f, 5)
result +=r
Specifically, you are modifying a global variable. This should work, but because it is a global you need you qualify it with global before modifying it in f. You can see this if you try to run f directly: it doesn’t work!
julia> f(1)
ERROR: UndefVarError: result not defined
Stacktrace:
[1] f(x::Int64)
@ Main ./REPL[3]:3
[2] top-level scope
@ REPL[4]:1
julia> f(x) = begin # you have to add the global keyword like this:
a = x^2
global result += a
a
end
f (generic function with 1 method)
julia> f(1)
1
julia> f(x) = begin # or this
a = x^2
global result
result += a
a
end
f (generic function with 1 method)
julia> f(1)
1
All this to say that Zygote can’t make functions with errors suddenly work