# Functions and additional exclamation mark '!'

Hello …

While I was reading through some tutorials, I noticed a function call with an additional “!”. For example here (and also for my solved problem): MTH229 with Julia - 3  Graphing functions with Julia

“Graphs can be layered by using the `plot!` function (with an exclamation point indicating a current graph is begin modified).”

That’s actually clear to me.

But I have seen another function calls with “!”, for example in our numerical framework code. Can someone explain to me (maybe with an easy example) what the exact idea of ​​the “!” is?

Kind regards

`!` is a naming convention used for functions that mutate an argument. For example `rand!(v)` fills `v` with random numbers.

3 Likes

And what happens when I just use rand(v) without “!”?

It’s a different function entirely that can do something totally different. In the case of `rand(v)` where `v` isa vector, it randomly picks an element from it.

A more conventional pair is `sort!` vs `sort` — the former sorts a vector in-place, while the latter returns a brand new vector with the same elements in sorted order. If you’re familiar with Python, it’s the difference between `v.sort()` and `sorted(v)`.

The case with `plot!` vs `plot` is quite similar — the former adds an element to an existing plot, whereas the latter creates a brand new plot from scratch.

But the point is that these are all just different functions with different names. The convention about what the `!` means is just that — a convention. You can sometimes look at a pair of functions like `embiggen` and `embiggen!` and have a guess at what they would each do (if you know what it means to embiggen something, that is), but the real answer is to check the docs for each one independently.

6 Likes

I noticed it today with plot vs plot!: I wanted to plot the datas into a chart for each loop run.
Just plot: I just got a white chart with no data set
plot!, with “!”: I got a chart with all data sets

1 Like

https://docs.julialang.org/en/v1/manual/style-guide/

Append `!` to names of functions that modify their arguments

1 Like

Could the functions simply be named differently (without any “!”)?
If plot and plot! are different functions …
For example, could you have simply called plot! as “forest” (which makes no sense!)?

Yes, exactly! They’re just different functions with different names. In fact, you can even choose the names for these functions when you load Plots.jl:

``````julia> using Plots: plot as beginulate, plot! as blurst!!eleventy1!!

julia> beginulate(1:10, rand(10))

julia> blurst!!eleventy1!!(1:10, randn(10))
``````

5 Likes

Great! Thank you very much ;)!

1 Like