# Error in defining the function

**URL:** <https://discourse.julialang.org/t/error-in-defining-the-function/58540>\
**Category:** New to Julia\
**Created:** [April 4, 2021, 12:20am UTC](https://discourse.julialang.org/t/error-in-defining-the-function/58540 "2021-04-04T00:20:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manisha\_07](https://avatars.discourse-cdn.com/v4/letter/m/57b2e6/32.png) [@Manisha\_07](https://discourse.julialang.org/u/Manisha_07)\
**Post date:** [April 4, 2021, 12:20am UTC](https://discourse.julialang.org/t/error-in-defining-the-function/58540/1 "2021-04-04T00:20:27Z")

</div>

Hello  
When I am trying to define the function and execute, I am getting the following error. Please help me in solving this

"  
cannot define function f; it already has a value

Stacktrace:  
[1] top-level scope  
@ none:0  
[2] top-level scope  
@ In[18]:1  
[3] eval  
@ .\boot.jl:360 [inlined]  
[4] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1094

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 4, 2021, 12:34am UTC](https://discourse.julialang.org/t/error-in-defining-the-function/58540/2 "2021-04-04T00:34:47Z")

</div>

Your code probably creates a global variable `f` before defining the function. For example:

```julia
julia> f = 1
1

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

```

---

<div class="post-metadata">

**Author:** ![bertulli](https://avatars.discourse-cdn.com/v4/letter/b/ecc23a/32.png) [@bertulli](https://discourse.julialang.org/u/bertulli)\
**Post date:** [March 11, 2026, 12:57pm UTC](https://discourse.julialang.org/t/error-in-defining-the-function/58540/3 "2026-03-11T12:57:29Z")

</div>

For completeness, checking out [this other answer](https://discourse.julialang.org/t/why-can-i-not-add-methods-to-a-function-with-a-value/40819/4) you can redefine methods of global functions if you use the fully qualified name, i.e. prepending the name of the module:

```julia-auto
julia> f = 1
1

julia> f(x) = x
ERROR: cannot define function f; it already has a value
Stacktrace:
 [1] top-level scope
   @ REPL[233]:1

julia> Main.f(x) = x
# no prob!

```
