45

I saw a function like this

function operator!(c::Matrix, out::Matrix)
    ......
end

What does ! mean here?

Cameron Bieganek
  • 7,208
  • 1
  • 23
  • 40
Fazeleh
  • 1,008
  • 2
  • 9
  • 24

1 Answers1

65

In Julia, it's a convention to append ! to names of functions that modify their arguments. The reason is Julia function arguments are passed-by-sharing, without this "bang" convention, it's not easy to know whether a function will change the content of input arguments or not.

DisabledWhale
  • 771
  • 1
  • 7
  • 15
Gnimuc
  • 8,098
  • 2
  • 36
  • 50
  • I'm reading this code, then why the programmer has added another copy of the function, without `!` after the definition of the function? Like `operator(V::Matrix; policy=false) = operator!(V, similar(V); policy=policy)` – Fazeleh Jul 30 '17 at 08:12
  • 2
    @ReD How does `operator!` being implemented? – Gnimuc Jul 30 '17 at 08:19
  • 3
    > why the programmer has added another copy of the function, without ! after the definition of the function? Because the function without the ! is not mutable - that is, it doesn't change any of its inputs. Notice that it creates a separate matrix (via similar(V)) which does get mutated in operator!(), but it does not return it. If you call operator(), none of your inputs will change. If you call operator!(), at least one of your inputs (namely, the second one) will be mutated. – sbromberger Jul 30 '17 at 17:47
  • 1
    And what it means in expression such as AAA[!,1] ? – skan Dec 10 '19 at 16:14
  • 1
    @skan, it's specific to dataframes and says that you want a reference to the underlying vector storing the data, rather than a copy of it (see [SO Ans](https://stackoverflow.com/a/60900196), [docs](https://dataframes.juliadata.org/stable/lib/indexing/#getindex-and-view-1)) – JSymons7 Feb 17 '23 at 21:27