Get startedGet started for free

Mutating functions and multiple dispatch

1. Mutating functions and multiple dispatch

We need to go deeper to understand and make the most of Julia functions.

2. Mutating functions

Some of the functions you have used take an input, and modify that input. Functions like push, append, and pop extend or shorten an array.

3. Non-mutating functions

This is different than other functions like length, sort, or typeof which do not change the input values.

4. Mutating and non-mutating functions

Functions that change their inputs are called mutating functions. It is common style to choose a name for these functions which ends in an exclamation mark. The exclamation mark doesn't do anything special, it's just another character in the name. All functions have all lowercase letters and use underscores in the variable names when needed to make them easier to read.

5. Writing a mutating function

But how do we write mutating functions? Basic values like numbers, booleans, and characters cannot be mutated. We can mutate arrays, and some other data types we'll see in a later course. In this example, we change the first element of the input array to zero. When we pass in the array y and print it, we can see the array has been modified. Inside this version of the function, we assign a new array to x. This won't affect the value of y when we pass it in.

6. Writing a mutating function

In the function on the left we modify the input array. In the function on the right we just reassign the variable name x to a new value. The variable x doesn't exist outside the function.

7. Writing a mutating function

We can use the dot syntax to modify a whole array at once. If we use the dot-equals operator, we can set every element in the array to zero, for example. We can also assign different values to each element.

8. Writing a mutating function

In this example, we modify the array to subtract one from each element. Modifying arrays like this can make your calculations more efficient. Modifying the array, instead of creating and returning a new array, uses less memory and is faster.

9. Multiple dispatch

Multiple dispatch is another feature of Julia which makes it so powerful. Imagine we have this function named double. It works on integers, and floats, but not on strings. However, we would like it to work on strings.

10. Multiple dispatch

Multiple dispatch in Julia allows us to run a different method based on the argument type. We create a new function, also called double. We annotate the argument to say that it must be a string. We do this using colon-colon-String after the argument name. Inside the function's body, we define that to double a string is to repeat it. Now when we run this function on a string, the second double function is called. The first function is called for any input type which is not a string.

11. Multiple dispatch

We can extend this to create specific methods for more data types. Perhaps we believe that doubling the boolean values true or false should simply give true or false. The top function, which has no type annotation, is run whenever we use an input that isn't a string or boolean.

12. Multiple dispatch

If we didn't have this untyped function, then double can only be run on strings and booleans. If we try to pass in a float, an integer, or anything else, it will cause an error. This is a useful feature to help us avoid simple data type mistakes.

13. Multiple dispatch

But remember, these are optional features, and if we don't want to worry about types, we don't need to.

14. Let's practice!

These have been two more advanced topics in Julia. So let's master them in the exercises.