Immutable Parameters

This lesson explains how immutability can be achieved in the functions having immutable parameters.

Immutable parameters #

It is possible for functions to promise that they do not modify certain parameters they take, and the compiler will enforce this promise. Before seeing how this is achieved, let’s first see how functions can modify the elements of slices that are passed as arguments to those functions.

As you remember from the slices and other array features lesson, slices do not own elements but provide access to them. There may be more than one slice at a given time that provides access to the same elements.

Although the examples in this section focus only on slices, this topic is applicable to associative arrays and classes as well.
A slice that is passed as a function argument is not the slice that the function is called with. The argument is a copy of the actual slice:

Get hands-on with 1200+ tech skills courses.