# Redefining functions inside loops

**URL:** <https://discourse.julialang.org/t/redefining-functions-inside-loops/22772>\
**Category:** New to Julia\
**Created:** [April 4, 2019, 9:51pm UTC](https://discourse.julialang.org/t/redefining-functions-inside-loops/22772 "2019-04-04T21:51:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andre\_Mello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_mello/32/7333_2.png) [@Andre\_Mello](https://discourse.julialang.org/u/Andre_Mello)\
**Post date:** [April 4, 2019, 9:51pm UTC](https://discourse.julialang.org/t/redefining-functions-inside-loops/22772/1 "2019-04-04T21:51:41Z")

</div>

Defining a function `f` inside a loop, using it and then redefining it to behave some other way actually does not work as I thought it would/should.

For instance, this

```julia
for i in 1.0:2.0
	f(a) = 10.0*a
	println(f(i))

	f(a) = a^2
	println(f(i))
end

```

gives me

```julia
1.0
1.0
4.0
4.0

```

while I was hoping for

```julia
10.0
1.0
20.0
4.0

```

Only the last definition of `f` is used through the entire loop.  
So I’m intrigued, any reason for this?

---

<div class="post-metadata">

**Author:** ![Andre\_Mello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andre_mello/32/7333_2.png) [@Andre\_Mello](https://discourse.julialang.org/u/Andre_Mello)\
**Post date:** [April 4, 2019, 10:07pm UTC](https://discourse.julialang.org/t/redefining-functions-inside-loops/22772/2 "2019-04-04T22:07:12Z")

</div>

Ok so minutes after posting this I realized that this makes perfect sense considering Julia is a compiled language (JIT) and not an interpreted one.

Since `f` was last defined to behave as `a^2`, the first definition of `f` was overwritten during compilation, long before `10.0*a` could show up anywhere on my screen.

I love how Julia can be so interactive that I ended up forgetting it is actually compiled.
