# Using the wrong function defintion, many old methods

**URL:** <https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693>\
**Category:** New to Julia\
**Created:** [January 16, 2019, 12:29pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693 "2019-01-16T12:29:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [January 16, 2019, 12:29pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/1 "2019-01-16T12:29:05Z")

</div>

Hello.

I was trying different ways to speed up my code and found something strange.  
Please, compare these two functions:

```julia
function f1(n)
    s = 0
 for i in 2:n
        s += sum(( ((j^2-1)/(j-1) ) for j in 2:i)
    end
    return s
end

```

> f1(5) gives 105

```julia

function f2(n)
    s = 0
 for i in 2:n
        s += sum( j+1 ) for j in 2:i)
    end
    return s
end

```

> f2(5) gives 40.

(j^2-1)/(j-1) should be the same than (j+1)

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 16, 2019, 12:36pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/2 "2019-01-16T12:36:06Z")

</div>

please post runnable code - `s += sum( j+1 ) for j in 2:i)` has an uneven number of brackets

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [January 16, 2019, 12:39pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/3 "2019-01-16T12:39:29Z")

</div>

After removing the offending bracket in `f2`, I get

```julia
julia> f1(5)
40.0

julia> f2(5)
40

```

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [January 16, 2019, 12:42pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/4 "2019-01-16T12:42:27Z")

</div>

Oh, my fault I’ve found the problem.

I defined f1 several times with slight changes and Julia created different methods for them.  
I guess when I executed f1(5) it was using an old method inadvertedly. I’m new to Julia.  
I’ve restarted Julia and now it’s OK.  
I’m going to change the title of the thread.

How can I delete an old function definition or be sure Julia is using the proper one?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 16, 2019, 12:47pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/5 "2019-01-16T12:47:38Z")

</div>

> [@Juan](#):
>
> I guess when I executed f1(5) it was using an old method inadvertedly.

Which version of Julia are you using? If you are redefining a method with the same type signature, it should automatically overwrite the old one.

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [January 16, 2019, 1:06pm UTC](https://discourse.julialang.org/t/using-the-wrong-function-defintion-many-old-methods/19693/6 "2019-01-16T13:06:00Z")

</div>

> [@Juan](#):
>
> How can I delete an old function definition

You can’t do that if it’s in Main’s global scope - it’s one of the cases where you need to restart Julia.  
You can do it by defining your function in a module and not exporting them though. Reloading module `MyModule` will then only have the methods defined for `MyModule.f1` that you specify.
