# Best practice for ! functions

**URL:** <https://discourse.julialang.org/t/best-practice-for-functions/18565>\
**Category:** New to Julia\
**Created:** [December 11, 2018, 4:33pm UTC](https://discourse.julialang.org/t/best-practice-for-functions/18565 "2018-12-11T16:33:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![zymbuzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zymbuzz/32/5482_2.png) [@zymbuzz](https://discourse.julialang.org/u/zymbuzz)\
**Post date:** [December 11, 2018, 4:33pm UTC](https://discourse.julialang.org/t/best-practice-for-functions/18565/1 "2018-12-11T16:33:58Z")

</div>

I often try to write mutating functions to avoid unnecessary allocation. But sometimes I have to use the same function and allocate a new array. This usually means that I have to create a new function with almost the same content.

```julia
A = randn(3)
function foo!(A)
    A .= A ./ 2
    return nothing
end

function foo()
    A = randn(3)
    A .= A ./ 2
    return A
end

```

Is there a good practise to avoid copy/paste of mutating functions into normal functions? Would this example is considered a right approach?

```julia
function foo2()
    A = randn(3)
    foo!(A)
    return A
end

```

Thanks!

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [December 11, 2018, 4:36pm UTC](https://discourse.julialang.org/t/best-practice-for-functions/18565/2 "2018-12-11T16:36:30Z")

</div>

Yes, that’s a good pattern to use. You can express it more concisely like this:

```julia
function foo!(A)
    A .= A ./ 2
    return A
end

foo() = foo!(randn(3))

```

---

<div class="post-metadata">

**Author:** ![zymbuzz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zymbuzz/32/5482_2.png) [@zymbuzz](https://discourse.julialang.org/u/zymbuzz)\
**Post date:** [December 11, 2018, 4:48pm UTC](https://discourse.julialang.org/t/best-practice-for-functions/18565/3 "2018-12-11T16:48:48Z")

</div>

thanks! what if I have multiple function arguments, for which I also would like to specify types?

```julia
C=0.5
function foo(C::Float64)
    A = randn(3)
    A .= A ./ 2 .+ C
    return A
end

A = randn(3)
function foo!(A,C::Float64)
    A .= A ./ 2 .+ C
    return nothing
end

foo2(C::Float64) = foo!(randn(3),C::Float64)

```

the last expression does not work

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [December 11, 2018, 5:06pm UTC](https://discourse.julialang.org/t/best-practice-for-functions/18565/4 "2018-12-11T17:06:30Z")

</div>

It does work — it’s just that your `foo!` function returns `nothing`. I think you want it to return `A` instead.

Note that you don’t need to annotate C with a type when calling `foo`;

```julia
foo2(C::Float64) = foo!(randn(3), C)

```

will work just as well.
