# Defining a function that modifies its argument in place defines a new function instead

**URL:** <https://discourse.julialang.org/t/defining-a-function-that-modifies-its-argument-in-place-defines-a-new-function-instead/109462>\
**Category:** New to Julia\
**Tags:** argument, functions\
**Created:** [January 30, 2024, 5:20pm UTC](https://discourse.julialang.org/t/defining-a-function-that-modifies-its-argument-in-place-defines-a-new-function-instead/109462 "2024-01-30T17:20:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![keynescoefen](https://avatars.discourse-cdn.com/v4/letter/k/d2c977/32.png) [@keynescoefen](https://discourse.julialang.org/u/keynescoefen)\
**Post date:** [January 30, 2024, 5:20pm UTC](https://discourse.julialang.org/t/defining-a-function-that-modifies-its-argument-in-place-defines-a-new-function-instead/109462/1 "2024-01-30T17:20:59Z")

</div>

Hello! I have the following function

```julia
using SpecialFunctions
function CDFFoldedNormal(x,μ,σ)
    #follow the example: https://en.wikipedia.org/wiki/Folded_normal_distribution
    #Note that the folded normal is NOT the half normal (unless μ=0)

    term1 = erf((x+μ)/(sqrt(2)*σ));
    term2 = erf((x-μ)/(sqrt(2)*σ));

    return 0.5 * (term1 + term2);
end

```

which is part of the following function that is supposed to modify its argument:

```julia
function foo!(F,x) 
    F = CDFFoldedNormal(x,0.03,0.05) - 0.95;
end

```

When I try to evaluate the function that modifies its argument in-place, like so

```julia
F = 0.
foo!(F,0.105)

```

Instead of getting `F` as a modified argument. I get F as a redefined function and the error message `invalid redefinition of constant Main.F `

Could someone please help me rewrite the function `foo!` so that it modifies its argument in-place?

Thank you!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [January 30, 2024, 5:32pm UTC](https://discourse.julialang.org/t/defining-a-function-that-modifies-its-argument-in-place-defines-a-new-function-instead/109462/2 "2024-01-30T17:32:11Z")

</div>

> [@keynescoefen](#):
>
> invalid redefinition of constant Main.F

That’s probably related to some previous definition of `F` in your section. Maybe you can get rid of it by restarting Julia.

> [@keynescoefen](#):
>
> `foo!` so that it modifies its argument in-place?

If the output is a scalar, you cannot modify it “in place”, because it is immutable. Instead, you have to reassign the value bound to `F`:

```julia-repl
julia> F = 0.0
0.0

julia> F = foo!(F, 0.105)
-0.02027417507189877

julia> F
-0.02027417507189877

```

---

<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:** [January 30, 2024, 5:42pm UTC](https://discourse.julialang.org/t/defining-a-function-that-modifies-its-argument-in-place-defines-a-new-function-instead/109462/3 "2024-01-30T17:42:33Z")

</div>

> [@keynescoefen](#):
>
> ```julia
> F = 0.
> foo!(F,0.105)
> 
> ```
> 
> Instead of getting `F` as a modified argument. I get F as a redefined function and the error message `invalid redefinition of constant Main.F`

To be clear, this error is being thrown at `F = 0.` The `F = CDF...` assigns a local `F` in `foo!`, it doesn’t reassign `Main.F`. Reassignments of variables are different from in-place mutation of instances.
