# How to check if function is in-place

**URL:** https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570
**Category:** General Usage
**Created:** [September 22, 2021, 8:46am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570 "2021-09-22T08:46:39Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [September 22, 2021, 8:46am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/1 "2021-09-22T08:46:39Z")

</div>

Say I am given a function `f` and would like to know whether `f` will return a new array or simply write into its first argument. How do I do that?

I know DifferentialEquations.jl does this, for example. You just supply a function and it figures out if it’s in place or not.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [September 22, 2021, 9:09am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/2 "2021-09-22T09:09:25Z")

</div>

I think DiffEq checks whether the function has 3 or 4 input arguments. The former is non-mutating, the latter is mutating, by convention of DiffEq. I don’t think a general check on whether a function is mutating is possible in Julia (you should look into Rust ;-).

---

<div class="post-metadata">

### Author: ![woclass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/woclass/32/212699_2.png) [@woclass](https://discourse.julialang.org/u/woclass)
#### Post date: [September 22, 2021, 9:11am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/3 "2021-09-22T09:11:56Z")

</div>

They explicitly specify whether it is an in-place modified function or not in the function name and type.

> For example, on ODEs, we have that `f!(du,u,p,t)` is the in-place form which, as its output, mutates `du` . Whatever is returned is simply ignored.  
> Similarly, for OOP we have the form `du=f(u,p,t)` which uses the return.

> However, every constructor allows for manually specifying the in-placeness of the function. For example, this can be done at the problem level like:
> 
> ```julia
> ODEProblem{true}(f,u0,tspan,p)
> 
> ```

[https://diffeq.sciml.ai/stable/basics/problem/#In-place-vs-Out-of-Place-Function-Definition-Forms](https://diffeq.sciml.ai/stable/basics/problem/#In-place-vs-Out-of-Place-Function-Definition-Forms)

> <https://github.com/SciML/SciMLBase.jl/blob/9e5ba6f10a0d347d48ba4d2bae4a3c610a589bc1/src/problems/problem_traits.jl#L8-L23>

---

<div class="post-metadata">

### Author: ![fbanning](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fbanning/32/14972_2.png) [@fbanning](https://discourse.julialang.org/u/fbanning)
#### Post date: [September 22, 2021, 9:14am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/4 "2021-09-22T09:14:57Z")

</div>

As far as I’m aware there is no way to programmatically check whether a function mutates its arguments or if it allocates. However, there’s the general function naming convention that a mutating function should have an `!` appended at the end of its name. So mutating would be `f!` and allocating would be `f`. This is not enforced in any way but it’s generally followed by the community.

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [September 22, 2021, 9:43am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/5 "2021-09-22T09:43:49Z")

</div>

Alright, then I’ll give the user an argument to specify in-place-ness and default to checking the number of input args, too.

And yeah, Rust is cool.

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [September 22, 2021, 9:46am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/6 "2021-09-22T09:46:13Z")

</div>

I was thinking you could potentially check if the function has a method `f(out::AbstractArray, x::InputType1, y::InputType2)` or a method `f(x::InputType1, y::InputType2)`. Not sure how to do that though and if that’s the best way to do it.

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [September 22, 2021, 10:35am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/7 "2021-09-22T10:35:22Z")

</div>

It’s a julia convention to add a `!` at the of the name to signify that the function mutates one or more of it’s arguments. In your example the mutating version would be called `f!` and the other just `f`.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 22, 2021, 10:43am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/8 "2021-09-22T10:43:49Z")

</div>

“mutating” may not only refer to arrays - there are other kinds of containers that can be modified as well, like `Dict`, `Set` and similar, so checking for a given method signature also doesn’t work. The convention used by DiffEq.jl or explicitly specifying is best, since at that point it’s clear what the user intended.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [September 22, 2021, 11:19am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/9 "2021-09-22T11:19:19Z")

</div>

> [@gbaraldi](#):
>
> It’s a julia convention to add a `!` at the of the name

A problem occurs if your code doesn’t know the name of the function. It could simply be held in some variable.

Maybe functions could have a `Mutating` trait at some point?

---

<div class="post-metadata">

### Author: ![Luapulu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luapulu/32/17669_2.png) [@Luapulu](https://discourse.julialang.org/u/Luapulu)
#### Post date: [September 22, 2021, 11:37am UTC](https://discourse.julialang.org/t/how-to-check-if-function-is-in-place/68570/10 "2021-09-22T11:37:35Z")

</div>

I should add that I don’t need a general solution here. This is just for my little library, where I’d like to have efficient implementations for if the user specifies an in place or out of place function. Of course, I can give the user an argument to specify this themselves, but I thought it might be possible to have a more convenient interface by using the same concept as DifferentialEquations.jl.
