2

I need to create a function like append! I think ! Like a pointer and can change the function of the argument an and make changes to the main variable without return argument

deVOID
  • 155
  • 7

1 Answers1

4

There's nothing special about ! at the language level - it's just a convention that the Julia community decided upon to mark functions that mutate their argument.

I think ! Like a pointer and can change the function of the argument

Functions with ! at the end get the same argument as any other function, no pointers or any other special changes involved.

julia> set2to2!(a::Array) = a[2] = 2
set2to2! (generic function with 1 method)

This is a mutating function that sets the second index of its argument to 2.

julia> set2to2(a::Array) = a[2] = 2
set2to2 (generic function with 1 method)

This is also a mutating function, that does the same thing, even though it doesn't end in a !. There's no difference in what it has access to, and how it works.

The ! just lets the caller know that your function is going to be changing the value of the argument they pass in, and so is considered good practice for program design.

Sundar R
  • 13,776
  • 6
  • 49
  • 76