# Can a function know if it called with a "!" in the name?

**URL:** <https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343>\
**Category:** New to Julia\
**Created:** [September 13, 2025, 10:49am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343 "2025-09-13T10:49:56Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dankelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dankelley/32/33539_2.png) [@dankelley](https://discourse.julialang.org/u/dankelley)\
**Post date:** [September 13, 2025, 10:49am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/1 "2025-09-13T10:49:56Z")

</div>

I’m writing a specialized plotting function (let’s call it `A()`, say) that does quite a lot of logic, and then calls `plot()` to generate a new plot. Is there a way that I can make a paired function (let’s call it `A!()`, say) without repeating all the logic in the second version? The only difference would be that `A()` would call `plot()` and `A!()` would call `plot!()`, with the same logic before the plot call and the same arguments in the plot call.

I’m guessing that this sort of thing comes up quite a bit. Is the best practice to put the logic that goes before the plot calls into a new function and have both `A()` and `A!()` call that?

Many thanks for any advice.

Dan Kelley

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [September 13, 2025, 10:57am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/2 "2025-09-13T10:57:04Z")

</div>

> [@dankelley](#):
>
> I’m guessing that this sort of thing comes up quite a bit. Is the best practice to put the logic that goes before the plot calls into a new function and have both `A()` and `A!()` call that?

Hi dankelley, I think so.

```julia
B() = begin
	A()
	plot()
end

B!() = begin
	A()
	plot!()
end

```

---

<div class="post-metadata">

**Author:** ![Gesee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gesee/32/217235_2.png) [@Gesee](https://discourse.julialang.org/u/Gesee)\
**Post date:** [September 13, 2025, 11:06am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/3 "2025-09-13T11:06:58Z")

</div>

If you can’t separate the logic that come before the call to `plot` you can use a macro instead (just saying, it’s not the best solutions)

```julia
const PLOT_FUNC = (:plot, :plot!)
const FUNCS = (:A, :A!)

for i in Base.OneTo(2)
    func = FUNCS[i]
    plot = PLOT_FUNC[i]
    eval(quote
        function $func(args...)
            # function body
            $plot(args...)
        end
     end )
end

```

It’s just a way to achieve what you want, just a proposition

---

<div class="post-metadata">

**Author:** ![dankelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dankelley/32/33539_2.png) [@dankelley](https://discourse.julialang.org/u/dankelley)\
**Post date:** [September 13, 2025, 11:16am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/4 "2025-09-13T11:16:19Z")

</div>

Thanks. I’ll go with this, since I’m too much of a newbie to use macros, as another helpful person suggested.

---

<div class="post-metadata">

**Author:** ![dankelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dankelley/32/33539_2.png) [@dankelley](https://discourse.julialang.org/u/dankelley)\
**Post date:** [September 13, 2025, 11:16am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/5 "2025-09-13T11:16:43Z")

</div>

Thanks. I’ll bookmark this for future reference.

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [September 13, 2025, 11:19am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/6 "2025-09-13T11:19:17Z")

</div>

In addition to factoring out the common code to a separate function, like @karei suggested, another common solution would be, to include an additional argument (possibly with default value) `should_mutate::Bool=false` and branch only for the call that differs.

Of course you could also pass the function (`plot` or `plot!`) as argument, but this might be overkill for two choices.

---

<div class="post-metadata">

**Author:** ![karei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karei/32/214809_2.png) [@karei](https://discourse.julialang.org/u/karei)\
**Post date:** [September 13, 2025, 11:23am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/7 "2025-09-13T11:23:57Z")

</div>

I’d like to supplement a code example for the answer above.

“Manual dispatch”:

```julia
function A(a,b;should_mutate::Bool=false)
	...
	if should_mutate
		plot!()
	else
		plot()
	end
end

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [September 13, 2025, 11:44am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/8 "2025-09-13T11:44:22Z")

</div>

Can you define the mutating method first and define the non-mutating one based on it? This is a very common pattern for mutating. Something like:

```julia
function A!(p, arg1, arg2) 
  #logic logic
  return(plot!(p, etc))
end

function A(arg1, arg2)
  p = plot(somearguments) #maybe create the axis or something initial
  A!(p, arg1, arg2)
end

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [September 13, 2025, 11:51am UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/9 "2025-09-13T11:51:58Z")

</div>

Yes of course. In fact this is a very common pattern.  
E.g. in LinearAlgebra.jl you’ll find a lot code like

```julia-auto
function eigen!(matrix)
    # implementation
end

function eigen(matrix)
    return eigen!(copy(matrix))
end

```

---

<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 13, 2025, 2:21pm UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/10 "2025-09-13T14:21:53Z")

</div>

> [@\_bernhard](#):
>
> should\_mutate::Bool=false

But then you’re not following the convention that mutating functions ought to have a name ending with `!`

---

<div class="post-metadata">

**Author:** ![\_bernhard](https://avatars.discourse-cdn.com/v4/letter/_/bc79bd/32.png) [@\_bernhard](https://discourse.julialang.org/u/_bernhard)\
**Post date:** [September 13, 2025, 2:32pm UTC](https://discourse.julialang.org/t/can-a-function-know-if-it-called-with-a-in-the-name/132343/11 "2025-09-13T14:32:35Z")

</div>

True, in fact it’s more geared towards two different functions in general.
