1. Keyword Arguments
The keyword argument is the final type of argument we will look at. If this sounds familiar to a 'key' in a dictionary, you would be right!
2. Keyword arguments - overview
A keyword argument allows us to assign a keyword to an argument in a function, similar to how we can assign a name to a value in a NamedTuple or a key to a value in a dictionary. A name for an argument gives us a good understanding of what we are passing into our function, as well as allows us to pass arguments in any order - whereas with positional arguments, as we have seen, the position matters. In this example, we have a keyword argument passed into the person function. Let's now look at the syntax.
3. Keyword arguments - syntax
To declare a keyword argument in a function, we use the semicolon when declaring the function.
This semicolon tells Julia that all arguments to the right of the semicolon are keyword arguments. Other argument types, such as positional arguments, must be to the left of the semicolon.
Here, we have defined a function called "person" with only one keyword argument called location. When calling "person", we pass location as a keyword argument in the function call. Note that when calling the function, the semicolon that we placed in the function definition is not required.
4. Keyword arguments - mixing argument types
We might want to use a mixture of arguments - a positional argument and then a keyword argument. This is allowed, but keyword arguments must always come after positional arguments in a function declaration.
If we want to mix argument types, we place them to the left of the semicolon.
In this example, we have a positional argument called name, and our previous keyword argument called location. We have specified a positional argument, then used a semicolon to denote a keyword argument, and then specified the keyword argument location.
Calling the function is similar - we first pass the string "Anthony" to the positional argument and then pass the keyword argument. It gives the expected result.
5. Variable number of arguments
So far, we have had to define every argument that we want to pass into a function. If we write a function that accepts three arguments, we can only pass three arguments into that function and no more. But we might have situations where we need to pass more arguments in.
Fortunately, we can pass a variable number of arguments (commonly called varargs) to a function in Julia using the ellipsis operator, which is three periods.
In this example, we have defined a function names, which takes in a variable number of names in the name parameter. Calling the function, we have passed four arguments into this name parameter - four strings.
This function call returns the four strings. We could have passed in only one string or one hundred. Variable arguments give us this flexibility.
6. Variable number of argument types
It is also possible to mix other types of arguments, such as positional arguments, with varargs. Let's take our people function from earlier and add another parameter called education.
We add the education parameter before the keyword argument location, and we again use the ellipsis to indicate this parameter will accept a variable number of arguments.
Here, we have passed the name, three strings, and then our keyword argument. The three strings BE, BS, and MComm are our education varargs, and we can see that they are inside brackets, meaning that we can index into the varargs to access each one individually.
Here, we take the second index of our output, which returns the varargs, and then the first index of that, which returns our first education, the string BE.
7. Let's practice!
You've seen how to use keyword arguments and variable arguments in a function. Now let's practice.