[MUSIC] All right, let's now refactor our existing functions like show info for planet right? Let's refactor those to call this number of moons function, show number of moons function that we just made. So, here is my same old function that we've been using. I just called it something different, again, because all of our function have to have a unique name in their enclosing scope. And so I say function show default info of planet underscore refactored. Right? This helps me know which one I'm looking at for the sake of this class. All right, so it's going to take a planet of course of the expected dictionary type. It's also going to take our same old arguments, shows number of moons as a bull with default values and show number of moon names of a bull with a default value. So it could have default values or not those are totally optional, but we're just going to keep on enhancing the same function for these examples. Alright so now I just have a print statement at the beginning of this function to help us find the output in the console. So I know that whenever I call this function I should see in the console show default info of planet refactored. That will help me know where to look for, because this is all in one playground. And your playgrounds may get long, or your classes or strokes may be get pretty long and you may have a lot of print statements, they'll all appear together in the console. So it helps to know which method, or function, or object, is actually doing the printing out for you. So, let's look for this showDefaultInfoOfPlanet_Refactered in our console. So let me scroll to that area so that you're not confused. Now we have basically all of the same old logic, except for how we get the numberOfMoons. So if we do show the numberOfMoons, then I say let numberOfMoons = this result of our function we just made called numberOfMoonsOfPlanet p. So this function was given a planet, which we internally refer to as p, we just pass this into our new numberOfMoons function, and if a number of moons. Excuse me, the numberOfMoons function returns a non nil value. It will put that value into this numberOfMoons temporary variable using optional binding. And that's yet another reason why we decided to make the return type of this function an optional. So that we can use optional binding. If the return type of this function was just int, then we won't be able to use optional binding done here. Because optional binding requires that the right hand side to be optional, in other words that it might be nil. Okay, I'm just going to let this run, it'll take a few seconds because I want the error to display on the video. Okay. So we can see the error here. It says initializer for conditional binding must have optional type not int would, if it is optional you see int question mark. So, it's getting an int when it's expecting an int question mark, in other words. That's because I changed this return type object. So, let me return it to what it should be and that error will go away. But for now let's move on. Okay, so now that we've gone over how do we have a return type and why we may or may not want to have optional return types, let's go ahead and see the result of this function. So I'm going to call my new refactor function. Which we'll call the Number of Moons function that we created. So, functions inside, we can call other functions. That's totally fine, and very common. Here I call it Show Number, excuse me, Show Default Info of Planet Refactored for the Planet Venus, and if I go look down here. Here's the information you requested, you asked for about the planet Venus. Of course Venus has no moons, so there's not going to be any information available, but we see that our code worked, right, with only making the difference that. To get the number of moons, we call the function instead of looking for that value inside the planet dictionary in line here. Alright, now let's take a look at a function that returns multiple values. Swift supports returning tuple values. So we can have two or more values returned at the same time from a single function. So, it would be convenient to receive multiple pieces of information back about a planet from a single function. Luckily Swift does let us use those tuple values. Note here that we created all the planets so we know that they all have a name and order key in their dictionary. But, again, in a real life application the data contained any given object may not be guaranteed, or is not guaranteed. So consider using an optional return value in your implementation, not necessarily for this class or for homework, but just in general when you're working in the future. Okay. So, here I have a function called name end order. Which we expect will give us the name and order of a planet. Name and order of planet. And our planet will refer to it inside a function as p. And of course that's with our good old expected planet dictionary type. We close the parenthesis for our list of arguments. And then we have our arrow to let us know we're returning a value. So now, the difference is that this value is a tuple. We're actually going to return. Two values out of this function. We return that in a structure called a tuple. So the first value inside of that tuple is called a name and it will be of type string and the second value inside of that tuple is called order and it will be type int. So now I can say, let name equal the value at the key called Name, and make sure that comes back to us as a string, because that's our expected return type. And let order equal the value at the key called Order inside of our planet dictionary p, and make sure that comes again to us as an int, because, again, that's our expected return type. Now, how do we return a tuple value actually? It's very simple. I can say, return, open parenthesis, the first value in the tuple, comma, the second value in the tuple. And we only have two here, but if we had more we would just keep on listing them out with commas. And then finally, close the parenthesis of course. And that's it. So now we can call this function and ask for the appropriate value quote unquote by mean and get the return value here in the right hand side. So, I'm going to ask for the naming order of Planet Earth. And I want to see the name now. So the name, excuse me, the first value in our tuple in our return tuple has a name which is called name in this case. So I can just say name in order of planet. That will give me the return value, and then I ask for its dot name, and I get Gaia. Same thing with the next line, name an order of planet Earth. I can say dot order, and I get three because of course Earth is the third planet from the Sun. Alright, so that's how we can create functions with multiple return values and access them quote un-quote, by name using this dot syntax and the names of those values and the return type. Okay, we could have also written number of moons function as a nested function. This has the one disadvantage that it would only be usable inside of the outer function. I'll explain that in just a second. But it would have, it may help us inside of a maybe lengthy function that we can call this internal function multiple times. So let's look at this. I have a function here called showDefaultInfoOfPlanet_Nested, just because my function name needs to be unique. The only thing that's changed is, instead of making the call to the numberOfMoons Function inline, I've actually put the numberOfMoons function inside of this function. So, func numberOfMoons returns an optional int. This, I literally just copy pasted from when it was a standalone function up here. But now it's a nested function. So inside of the enclosing scope of this function, which in this case is this outer function, then I can call numberOfMoons, but I cannot call the numberOfMoons function From outside of its own closing scope. No, that won't work. Alright and so now I can just say if shows number of moons equals true and the function will continue as expected. [MUSIC]