1. Positional and Default Arguments, Type Declarations
Previously, we have seen and worked with functions in Julia. Now we're going to further our knowledge of functions and look at some of the different ways that we can pass arguments into a function.
2. Function arguments overview
Let's recap what we have seen so far. When declaring a function, we specify parameters. In this example, we have defined a function called my_function, with two parameters, param1 and param2.
When calling a function, we pass arguments into these parameters, shown as one and two in this example.
In our function definition, we returned the two parameters, meaning that in our function call, we would be returning the values of param1 and param2 that we have passed into the function.
3. Positional arguments
These function arguments that we have seen are called positional arguments. This means that the arguments need to be passed into the function in the same order that they were defined in.
Let's take our previous function, but instead of passing one and two as we previously did, we will instead pass two and one.
We have flipped the order in which we pass our arguments, and we get a completely different result. Previously, param1 was one, and param2 was two, but now we are mapping param1 as two, and param2 as one.
With a simple function like this, it might seem obvious. But when we work with more complex functions, the order can get confusing quickly, and it is easy to accidentally pass an argument incorrectly.
4. Default arguments
Default arguments are one way to improve our functions. They provide a default value to assign if we do not specify an argument for a parameter when calling our function.
Take our previous example function definition. For param2, we can see that we have assigned a default value of two using the equals sign in the function definition. This means that if we do not specify a value for param2 when calling the function, it will default to two.
We can see this in practice calling the function and only passing in one for param1 and leaving param2 blank. We get the output (1, 2) even though we did not pass 2 in from the function call.
5. Type declarations
Type declarations are a way to control the type of values that we pass as function arguments.
When defining a function, we have the ability to specify a data type for each parameter. This means that if we try to pass an argument to a function that is not of the correct data type, it will throw an error.
This allows us to safeguard our code and catch errors where the incorrect argument may be erroneously passed into a function, which could lead to larger issues as that incorrect value travels through our code.
Let's modify our function so that param1 can only take a String, and param2 can only take an Integer. We do this using the double colon syntax and specifying the data type after the double colon.
We've now tried to pass the same value as before into our function - an integer one into param1. However, param1 now only allows a String, so as expected, we get an error. Our type declaration of param1 is working!
6. Let's practice!
We've built on our knowledge of functions in this video. Now let's work on some examples together before moving on to more advanced functions.