# A question about redefining a function in Julia

**URL:** <https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579>\
**Category:** New to Julia\
**Tags:** question, functions\
**Created:** [October 31, 2025, 3:15pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579 "2025-10-31T15:15:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![quantum2025](https://avatars.discourse-cdn.com/v4/letter/q/90ced4/32.png) [@quantum2025](https://discourse.julialang.org/u/quantum2025)\
**Post date:** [October 31, 2025, 3:15pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/1 "2025-10-31T15:15:16Z")

</div>

Hello. I just tested the following small code on Julia, and found that the value g() is changed after redefining the function f, but c is unchanged. I was wondering the reason behind this difference, and how to keep g() unchanged as well. Thanks.

```julia-auto
f(x)=x+1
c=f(1) #c=2
g()=f(1) #g=2
f(x)=x+2
c #c=2 unchanged
g() #g=3 changed

```

---

<div class="post-metadata">

**Author:** ![oscarvdvelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscarvdvelde/32/202157_2.png) [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Post date:** [October 31, 2025, 3:26pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/2 "2025-10-31T15:26:06Z")

</div>

g() calls the function f with 1 as argument.  
If you change f to output argument + 2 instead of argument + 1, g() will use your changed function f.

---

<div class="post-metadata">

**Author:** ![quantum2025](https://avatars.discourse-cdn.com/v4/letter/q/90ced4/32.png) [@quantum2025](https://discourse.julialang.org/u/quantum2025)\
**Post date:** [October 31, 2025, 3:29pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/3 "2025-10-31T15:29:31Z")

</div>

Yes, I understand. But why doesn’t the value of c change as well?

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [October 31, 2025, 3:33pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/4 "2025-10-31T15:33:17Z")

</div>

I think it’s just because `c` refers to the value 2, which was computed by evaluating the expression (as it was defined at that time) `f(1)`, while `g` is looking up whatever the current definition of `f` is when you call it, and then executing.

---

<div class="post-metadata">

**Author:** ![oscarvdvelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscarvdvelde/32/202157_2.png) [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Post date:** [October 31, 2025, 3:33pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/5 "2025-10-31T15:33:25Z")

</div>

`c` does not change because it took the value of `f(1)` before you changed the meaning of function `f`. `g` does change, because it calls function `f`. This allows you to have all sorts of functions and not having to worry to recompile all functions that use your changed `f` down the line, just recompile `f`.

---

<div class="post-metadata">

**Author:** ![quantum2025](https://avatars.discourse-cdn.com/v4/letter/q/90ced4/32.png) [@quantum2025](https://discourse.julialang.org/u/quantum2025)\
**Post date:** [October 31, 2025, 3:44pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/6 "2025-10-31T15:44:14Z")

</div>

I think so. But why c took the value of f at the time it was defined while g needed to look up the definition of f? c and g() were defined in the same way. Any mechanism behind it?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [October 31, 2025, 3:46pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/7 "2025-10-31T15:46:01Z")

</div>

Functions _run code_. Assignments simply give names to values.

While `f() = x` and `f = x` look similar, they are wildly different things. The former defines a function that _can be run later_, whereas the latter gives another name to whatever value `x` is at that point.

---

<div class="post-metadata">

**Author:** ![quantum2025](https://avatars.discourse-cdn.com/v4/letter/q/90ced4/32.png) [@quantum2025](https://discourse.julialang.org/u/quantum2025)\
**Post date:** [October 31, 2025, 3:48pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/8 "2025-10-31T15:48:20Z")

</div>

Yes, but c and g() were defined in the same way, why do they behave differently when calling their values? Any mechanism about it?

---

<div class="post-metadata">

**Author:** ![oscarvdvelde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscarvdvelde/32/202157_2.png) [@oscarvdvelde](https://discourse.julialang.org/u/oscarvdvelde)\
**Post date:** [October 31, 2025, 3:51pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/9 "2025-10-31T15:51:35Z")

</div>

explained by @mbauman just before you asked 🙂

If you want your function be be more flexible, instead of redifining it every time, you could define it as follows:

```julia-auto
f(x,y) = x + y
g(x) = f(3,x+2)   
# example, note the x is local to the function, 
# so the y in f becomes the value of x+2

julia> c = f(1,1) # c=2
julia> c = f(1,2) # c=3
julia> c = g(2) # c=7 

```

although I would not bother if the function is not more than one `+` etc. operator.

---

<div class="post-metadata">

**Author:** ![quantum2025](https://avatars.discourse-cdn.com/v4/letter/q/90ced4/32.png) [@quantum2025](https://discourse.julialang.org/u/quantum2025)\
**Post date:** [October 31, 2025, 3:56pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/10 "2025-10-31T15:56:39Z")

</div>

Thanks. This clarifies my confusion. By the way, is there any documentation related to the delayed run of functions?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [October 31, 2025, 3:59pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/11 "2025-10-31T15:59:16Z")

</div>

Yes, the Manual page titled Functions. It’s also how functions work in programming generally, by whatever name in specific languages.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [November 1, 2025, 2:20am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/12 "2025-11-01T02:20:42Z")

</div>

I return to this blog post often, and recommend it a lot. Somehow, it just really cemented understanding/intuition of the way things work

> **[Values vs. Bindings: The Map is Not the Territory | juliabloggers.com](https://www.juliabloggers.com/values-vs-bindings-the-map-is-not-the-territory-3/)**

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 1, 2025, 2:17pm UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/13 "2025-11-01T14:17:24Z")

</div>

> [@quantum2025](#):
>
> Thanks. This clarifies my confusion. By the way, is there any documentation related to the delayed run of functions?

There is really not much to say about that. A function runs its content when it is called, not when it’s defined. Take e.g. the definition:

```julia-auto
function myprint(x) 
    print("DEBUG: ")
    println(x)
end

```

The whole point with functions is that the text `DEBUG: ` and the textual representation of the value of `x` is printed whenever you exectute a `myprint(whatever)` in your program. Not when the function `myprint` is defined.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [November 2, 2025, 4:56am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/14 "2025-11-02T04:56:48Z")

</div>

> [@sgaure](#):
>
> A function runs its content when it is called, not when it’s defined.

I feel like the kwarg (not belongs to content) concerns its defining scope

```julia-auto
julia> function f(; x = x)
           println(x)
       end;

julia> x = 0;

julia> function test()
           x = 1
           f()
       end;

julia> test()
0

```

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [November 2, 2025, 10:34am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/15 "2025-11-02T10:34:32Z")

</div>

> [@WalterMadelim](#):
>
> I feel like the kwarg (not belongs to content) concerns its defining scope

The manual is a bit vague on which scope the default values are evaluated in. With a bit of good will it can be surmised that the scope consists of the previous arguments and the scope where the function was defined.

It wouldn’t really make sense to use the caller’s scope. The writer of the function would lose all control, and the caller could inadvertently change the defaults by defining a variable which happens to be used in the defaults.

```julia-auto
julia> function f(; x = x/2, y = x)
           @show x y    
       end
f (generic function with 1 method)

julia> f();
ERROR: UndefVarError: `x` not defined in `Main`

julia> x = 1;

julia> f();
x = 0.5
y = 0.5

julia> f(x=2);
x = 2
y = 2

```

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [November 2, 2025, 11:32am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/16 "2025-11-02T11:32:05Z")

</div>

I totally don’t understand this behavior:

First I think `f(x=2)` is awful grammar which doesn’t make sense (I’m surprised it is supported). It would be clear to write `f(; x=2)`.

Second, I think the intention of `function f(; y = x)` is:

- the inner `y` depends on the `x` in the outer scope.

So if the outer `x` is `1`, then the outcome of the last line should be `y = 1`.

But, whatever, I probably won’t write code like that…

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 3, 2025, 12:53am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/17 "2025-11-03T00:53:32Z")

</div>

I’m very confused about what is confusing here. Keyword defaults and positional defaults work exactly the same way. Default expressions, like everything else about a function definition, are evaluated in the scope where the function is defined.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [November 3, 2025, 1:44am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/18 "2025-11-03T01:44:26Z")

</div>

I think the confusion is about why `g()=f(1)` do not store the definition of `f` at the time of evaluation, just like `c=f(1)` does. `g()` is affected by redefining `f`, `c` does not.

---

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [November 3, 2025, 2:12am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/19 "2025-11-03T02:12:14Z")

</div>

> [@josePereiro](#):
>
> I think the confusion is about why `g()=f(1)` do not store the definition of `f` at the time of evaluation

It shouldn’t store, because that’s excessive work that would slow our program.

```julia-auto
function f(x)
    x + 1
end
function g()
    f(1)
end

```

This style merely says that `g` captures the name `f`.  
But then we redefines

```julia-auto
function f(x)
    x + 2
end

```

so the name `f` is re-bind to a new function “add 2”. So the final outcome is

```julia-auto
julia> g()
3

```

* * *

> [@StefanKarpinski](#):
>
> in the scope where the function is defined

I’m unaware of the existance of such a scope, to be honest.

I think writing expressions at the default value position of a kwarg is obfuscating. So I mentioned “I probably won’t write code like that”.

---

<div class="post-metadata">

**Author:** ![josePereiro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josepereiro/32/17322_2.png) [@josePereiro](https://discourse.julialang.org/u/josePereiro)\
**Post date:** [November 3, 2025, 2:17am UTC](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579/20 "2025-11-03T02:17:16Z")

</div>

Yes, I agree with the current design, I was just talking about the confusion about the confusion.

In addition, having several versions of functions creates harder to follow programs.

[Next page](https://discourse.julialang.org/t/a-question-about-redefining-a-function-in-julia/133579.md?page=2)
