# Redefinition of functions sometimes possible?

**URL:** <https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406>\
**Category:** General Usage\
**Tags:** functions\
**Created:** [June 4, 2021, 8:51pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406 "2021-06-04T20:51:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fedario](https://avatars.discourse-cdn.com/v4/letter/f/f14d63/32.png) [@fedario](https://discourse.julialang.org/u/fedario)\
**Post date:** [June 4, 2021, 8:51pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/1 "2021-06-04T20:51:48Z")

</div>

Hi!

I noticed some unexpected (at least for me) behavior when redefining functions.

```julia
julia> foo(x) = x + 1
foo (generic function with 1 method)

julia> foo(x) = x + 2
foo (generic function with 1 method)

julia> foo = x -> x + 1
ERROR: invalid redefinition of constant foo
Stacktrace:
 [1] top-level scope
   @ REPL[3]:100: 

```

and

```julia
julia> bar = x -> x + 1
#3 (generic function with 1 method)

julia> bar = x -> x + 2
#5 (generic function with 1 method)

julia> bar(x) = x + 1
ERROR: cannot define function bar; it already has a value
Stacktrace:
 [1] top-level scope
   @ none:0
 [2] top-level scope
   @ REPL[6]:1

```

Can someone explain why this happens? Are `f(x) = x + 1` and `f = x -> x + 1` fundamentally different?

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 4, 2021, 9:01pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/2 "2021-06-04T21:01:39Z")

</div>

> [@fedario](#):
>
> Are `f(x) = x + 1` and `f = x -> x + 1` fundamentally different?

Yes. The former is a function named `f`. The latter is a pointer to an anonymous function. In a sense, the two are akin to

```julia
g(x) = x+1

f(x) = g(x) # the first one
f = g # the second

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 4, 2021, 9:06pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/3 "2021-06-04T21:06:06Z")

</div>

In particular the most important difference for this question is that `f = x -> x + 1` creates a global variable biniding, whereas `f(x) = x + 1` creates a global constant binding.

If you make is a constant then you also can’t change it:

```julia
julia> const g = x -> x + 1
#4 (generic function with 1 method)

julia> const g = x -> x + 2
ERROR: invalid redefinition of constant g
Stacktrace:
 [1] top-level scope
   @ REPL[2]:1

```

---

<div class="post-metadata">

**Author:** ![fedario](https://avatars.discourse-cdn.com/v4/letter/f/f14d63/32.png) [@fedario](https://discourse.julialang.org/u/fedario)\
**Post date:** [June 4, 2021, 9:22pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/4 "2021-06-04T21:22:01Z")

</div>

Thank you both!

One follow up questions:  
Why can I change `f(x) = x + 1` to `f(x) = x + 2` when it is a constant variable? Are functions in some way mutable?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 4, 2021, 9:25pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/5 "2021-06-04T21:25:26Z")

</div>

`f(x) = x + 1` creates a constant binding from the variable `f` to a function. However, the function itself is a mutable bag of methods. What you did was actually overwrite one of it’s methods. We can do this same with your other example:

```julia
julia> const g = x -> x + 1
#4 (generic function with 1 method)

julia> (::typeof(g))(x) = x + 2

julia> g(1)
3

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 4, 2021, 9:31pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/6 "2021-06-04T21:31:39Z")

</div>

When you write

```julia
f(x) = x + 1

```

you can think of this as ‘special syntax’ for something more like

```julia
struct var"typeof(f)" end
const f = var"typeof(f)"()
(::typeof(f))(x) = x + 1

```

Here’s that in action:

```julia
julia> struct var"typeof(f)" end

julia> const f = var"typeof(f)"()
var"typeof(f)"()

julia> (::typeof(f))(x) = x + 1

julia> f(1)
2

```

In julia, methods can be attached to any object. Functions are just a type of singleton object that have special syntax to make it easier to add methods to them.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [June 4, 2021, 9:35pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/7 "2021-06-04T21:35:28Z")

</div>

What is displayed if you just write `g` after adding that method? (On my phone, can’t check)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 4, 2021, 9:36pm UTC](https://discourse.julialang.org/t/redefinition-of-functions-sometimes-possible/62406/8 "2021-06-04T21:36:04Z")

</div>

```julia
julia> g
#4 (generic function with 1 method)

```
