[MUSIC] So now that we know how to make a very basic function, let's take a look at functions that accept parameters. Often functions require information to be input in order for them to perform their task. These inputs are referred to parameters or arguments of the function. Let's look at some functions that accept parameters or some functions that accept arguments. Here's an example of a function that takes some arguments. So we have a function here called showMoonsOfPlanet, and the first argument is called planet and it is of type dictionary. And remember for dictionaries we have to specify the types of, or we can specify the types of, the keys that it accepts and the types of the values that it will accept. Let's take a look at this syntax more carefully. So, notice that the syntax of our argument declaration is inside the parentheses right. Our arguments will all appear inside of the parentheses after the function name. There is one argument named planet, and we just talked about the type. The name of the argument is lower case, followed by a colon, right? So the first argument, or the first perimeter, is called planet, and we follow it by a colon. Just like when we're declaring any other variable, a colon separates the name of the variable rom its type. All right, so let's take a look at what this function does. So in our previous example, it just said the moons of Neptune are, and it listed out the array. So this is okay, but it doesn't look like a very nice English sentence. So let's try to improve upon that with this new function. All right, so here we have showMoonsOfPlanet, it takes a planet, and keep in mind that at this point our planets exist as a dictionary of this type, right. So I'll say. I want to get the name of the planet so I can make a nice string that says something like, moons of planet so and so are called A, B, C, D, and G, or whatever the moon names are. So I can say, if that name equals the object in the planet dictionary for the key called name. And I want to make sure that comes back as a string. Remember that when we pull values out of a dictionary, our program doesn't really know what that type is necessarily. So consider carefully how you have to cast your value to work with it. Now if I have a name then I'll say if let moonsArray equal the value in the planet dictionary at the key called Moons. And I want to bring back the Moons as an Array. Keep in mind we created the planets and the moons dictionaries and arrays so we know that it actually come back as an array. But in real life, we may not be in control of the data type that we're receiving, so consider what you need for your implementation. So now, if I have an array of moons, then let the number of moons equal moonsArray.count. That should be pretty comfortable at this point. If the number of moons is greater than zero, then let's construct the nice properly formatted english list of moons. So, this is just code from last time that I'm placing into a function now. For, of our i equals zero, as long as i is less than the number of moons, then increment i, and that will let us loop over our moons array. So let moon equals moons array, the value of index i, bring that back as a string. If i equals zero, then it's the first moon in the array. Remember we want to put a space before the name of the moon so that we get a nice, proper English sentence with the moons of planet so and so are called colon, space and then the name of the moon, right? Otherwise, if I equals moonsArray.count minus 1. That means it's the lost array. And we subtract, excuse me, the last moon in the array and we subtract one because the count of minutes in the array is going to be one greater than the number value of the last index in array. That's always true everywhere, so we need to subtract one to get the last value. So if we have the last value then in English we have to add the word and before that value in a list, right, so that we get the moons of planet so and so are A, B, C, D and whatever the last value is. Otherwise we're some value in between the first and the last one, so we'll just add a comma space before the name of the moon. So that logic should be familiar to us by now. Now that I have the list of moons for our planet. I want to actually build the string that I'm going to print out into, in this case, our console, and to tell us about that planet. So I'm going to call that string infoString and I'm going to put that outside of this if statement because the value of infoString will be different under different conditions. So if number of moons equals one, then info string equals the moon, the only moon of planet so and so right is called colon, and then we put our moons list string right. Otherwise, the number of moons is the numberOfMoons is not equal to 1 so then we have infoString = the moons of planet so and so are called and then the list of those moons so that we have nice English sentences. What about the other case where it's there are no moons? Well, we've already taken care of that by, with this outer if statement, if numberOfMoons > 0. So that would fall into this other case here, if we don't even know about our moons. Excuse me, that would fall into this other case here of number of moons is not greater than zero, the planet so-and-so has so-and-so moons and that would say zero moons, right? Otherwise, if we weren't able to find the value at the key in the planet dictionary called moons, right? So I can double click this curly brace above the else, to see what was the condition in the previous case. Then, if let moonsArray equal planet value for key moons. So, if I didn't find the moons key in my dictionary, or if it wasn't castable to an array, then I would say, no information is available for the moons for planet so and so. All right. So, we let our playground automatically execute this function, right? Not quite. We actually have to call this function before it will be executed. So down here, we just say showMoonsOfPlanet. And we pass in the planet whose moons we want to receive info about. So, in this case I want to pass in the planet Neptune. And down here we see the moons of planet Neptune are called colon, a nice space, the list of all the moons separated by commas and spaces. And the last value, instead of having a comma and space, has the word and, with the spaces around it correctly. So that's how we can do things like build a nice properly formatted English sentence to display in the UI of a real, IOS, or MAC application. All right, and just to comparison, we can pass in any planet into our show moons of planet function, right? As long as the planet is of the correct type. So we know that all the planets that we created are of the same type, right? They're of type dictionary that accepts string for the keys in any optional, any object for the value, right? So I should be able to pass in Earth into our function. And indeed, I see now what goes into the correct case, and it says that the only moon of planet Gaia is called clone luna with a period at the end. So that's how we can create a function with a parameter and passing the parameter when we call the function. Excuse me, passing a value for that parameter when we call the function like we did here. [MUSIC]