# Redefining constants in functions, confusing behavior

**URL:** https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538
**Category:** General Usage
**Tags:** question
**Created:** [August 14, 2019, 2:42pm UTC](https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538 "2019-08-14T14:42:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [August 14, 2019, 2:42pm UTC](https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538/1 "2019-08-14T14:42:56Z")

</div>

Hello all,

I know that redefining constants is discouraged and is only allowed as a convenience. However, I found this confusing:

```julia
julia> const x1=0;const x2=0;

julia> function f1()
       global x1+=1
       i->i+x1
       end
f1 (generic function with 1 method)

julia> function f2()
       i->(global x2+=1;i+x2)
       end
f2 (generic function with 1 method)

julia> f1()(0),f2()(0)
WARNING: redefining constant x1
WARNING: redefining constant x2
(1, 0)

julia> const x1=0;const x2=0;

julia> function f1()
       global x1+=1
       i->i+x1
       end
f1 (generic function with 1 method)

julia> function f2()
       i->(global x2+=1;i+x2)
       end
f2 (generic function with 1 method)

julia> f1()(0),f2()(0)
WARNING: redefining constant x1
WARNING: redefining constant x2
(1, 0)

julia> x1,x2
(1, 1)

```

Can someone please help me understand why the behavior of the two functions is different, or whether/why it is expected.

Cheers!

---

<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: [August 14, 2019, 2:51pm UTC](https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538/2 "2019-08-14T14:51:24Z")

</div>

> [@raminammour](#):
>
> I know that redefining constants is discouraged and is only allowed as a convenience.

The whole reason it is discouraged is because the resulting behavior is potentially confusing and you can’t really expect anything.

---

<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: [August 14, 2019, 2:54pm UTC](https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538/3 "2019-08-14T14:54:44Z")

</div>

This is undefined behavior so any results is reasonable. The exact reason is simply whatever the compiler want to respect your assignment or assume and the behavior is not guaranteed to be stable.

---

<div class="post-metadata">

### Author: ![raminammour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raminammour/32/13572_2.png) [@raminammour](https://discourse.julialang.org/u/raminammour)
#### Post date: [August 14, 2019, 2:59pm UTC](https://discourse.julialang.org/t/redefining-constants-in-functions-confusing-behavior/27538/4 "2019-08-14T14:59:16Z")

</div>

I was fishing for insight, but I take both your words for it, there may be none. Undefined behavior is a perfectly acceptable answer to me, I would mark both replies as solutions if i could, thanks 🙂
