[MUSIC] Let's take a look at functions that return a value or perhaps multiple values. A function may return a value in order to tell the caller usually whether it was successful and performing its task or returning some value maybe this whole purpose of the function. For example, we see it's a lot with math functions, right? We want to add a plus b together and return c to whoever called the function. All right, so let's look at some functions that have a return type. That's another way of saying that a function returns a value. Look for syntax denoting that a function returns a value and the type of the value. And also look for syntax denoting that a function returns multiple values and how to access those multiple values. Also be sure to look for syntax denoting that a function might not produce a value, in other words it might return nill to us. This is different than a function that doesn't have a return type, right? So don't be confused. A function can still have a return type, but not return a value. In other words, it would return nill to us, so look out for that difference. And when you work on your own, try omitting the return, and I'll show you how to return something out of a function in the play ground, but try omitting the line of code that says return something. On a function that does expect to return type and see what happens. You'll get an error and that is the compiler helping you to complete the implementation of your function based on the function signature. Okay. Let's go to the playground. So let's look at these functions that have a return type. Now up here we have some come code that was calculating the number of moons. And we rewrote that code over and over and over again, right? So that looked like this. If Let numberOfMoons = planet.count. And if numberOfMoons = > 0, do this, otherwise do that, etc. We kept on writing that code over and over again. So in a real implementation, it's wasteful to keep on retyping that code. And it's in fact could be actually dangerous to constantly write that code over and over again because you may want to change the behavior, change the implementation of that calculation, how it's calculated. But if you have it typed up in multiple locations in your project or in your code file your single class file what have you. If you change it one place you have to make the decision if you need to change it in the other place. It would be safer to have one function encapsulate that calculation and then make different objecst call that one function. That way if you have to change how that calculation is made you just do it once and everybody gets the benefits of that. So let's now create a function that will give us back the number of moons of a planet. Look at the syntax here. This function will return a value to its caller. Functions with return types are declared with an arrow, which is a hyphen and then a greater than sign, right? So this makes the arrow. And after the arrow we have the expected type of the return value. So in this case, we're going to return an int. Now keep in mind that a planet might not have any moons in which case the ends may be zero. But in our dictionary the way that they're structured we just wouldn't have that key value pair, so we wouldn't be able to even find anything about the moons and will return nil. So that's why this is declared as an optional int, all right? So it says we may return an int, but we may also return nil. That's different then if we just didn't have a return type here, right? Okay so let's look at this inside if this function. Var number of moons of type an option int declaring a variable as an optional type will produce a nil. And so if we don't find the value at the key moons inside of our planet dictionary, the number of moons will remain nil by the time that we return it which is what we do down here. Return number of moons. So again, that's another reason why we've decided to make the return type be an optional int. Okay, this part of the code we should be familiar with, right? We look in the planet p, look in the dictionary for the key moons, and grab that value out, we use optional binding. To assign that to the moonsArray temporary variable. And now we can just say numberOfMoons = moonsArray.count, so now that we've set moonsArray if we didn't have some moons, we return that number on the last line of code in the function here. So we have the return keyword, and followed by the value we're trying to return. This works out because number of moons was declared to be optional int, and the expected return type is optional int. So the signature up here of the return type. Matters if the value you return is not of the same type, you'll get a compiler error and you won't be able to build. [MUSIC]