[MUSIC] Hello and welcome to part four of Collections and Functions. We've finally arrived at the lesson on functions. So let's look at, what we'll cover. We'll talk about what is a Function, learn how to make a simple Function, and then some more interesting functions that take parameters, or another way of saying that is functions that take arguments. We can look at Functions with Single or Multiple Return Values. And talk a little bit about scope and how variables are able to be within an enclosing scope. We'll also look at finally Protecting Functions from Improper Conditions, and then I will just have a Word about Function types. So, what is a function? Functions are named blocks of code or named chunks of code or groups of code that you can create to fulfill some kind of a purpose. So it's called function for a reason that it serves a function or it serves a purpose. The name of well-named functions describes that purpose. So something like, my function doesn't really help to know what that function does. But, something like. Something like display names of my friends, which we saw in a previous example, or show names of my friends. That tells me very clearly, what that function is going to do. Some, but not all functions, require input data in order to perform their function order, in order to fulfill their purpose. So we'll look at functions that take incorrect parameters or another way to say that is functions that take arguments. A function may produce or we also say a function may return a value or values. And in order to make the functions, the code with inside of a functions execute. We have should not have to call the function, so, up until now, we've been working with the playgrounds and playgrounds just automatically execute all of our codes is and when you tape it for the functions, the function exists, sort of in a little bit of a vacuum, if you will. It doesn't automatically run, until somebody tells it to run. So that object or we often use the words we or I or self, because of the way that we type our code, but really it's an object in our code. So, when I say somebody does something, I often use that to say, to mean an object in our code does something. But some object would be referred to as the caller, actually has to invoke the function. Or we say, call the function before it will run. All right, so, we'll see this in the examples. So, let's make a basic function. Look for the syntax that declares to our compiler, hey this is a function. Look for the syntax that denotes the function signature, and it's body. So, that the function signature contains the name of the function, and if there are any arguments. Or primaries of the function, we use those words interchangeable, then it will also list out, what those arguments are and what the expected data types for the values of those arguments should be. And the function body is where we put our implementation, so. The function signature may be something simple like, show list of friends, we don't need to, we may pass in a list of friends, we may not need to pass in a list of friends, it depends on your implementation. But it doesn't know how to just show all this difference by itself. We have to make that implementation, so we have to code that out. And that goes in an area called the body. All right, so let's look at how to make a simple function. Here, keep in mind that when we have a new playground, we need to import foundations, so that we can work with the basic types. I have up here at the top of my playground, some data that we used in our previous examples. But to prevent the playground from becoming one really, really long file, I broke up the code into these different sections for each lesson. And so, we just need to bring over that data. So this should be nothing new. If you've seen the sections on a raisin dictionaries. All right. So, here we have a few planets. They're existing as a dictionary and we have different key value pairs to hold information about those planets. And some of that information is the list of moons, a planet may have. So that's the data that we're going to be working with. All right, so let's make a basic function. Here's our function called show moons of Neptune. And, look at the syntax here. So notice that the func keyword, declares that we are writing a function. The parenthesis enclose any arguments that the function may have, even if there are no arguments, you still need these open and closed parenthesis after the name of your function. Here's the name of my function. It comes out after the func keyword, and it's just called showmans of Neptune. And you'll notice, this is a very descriptive function name, right. It tells me exactly what this function is going to do. All right. The curly braces, which are these here open and close curly brace, enclose the body of the function. So what the function show moons of Neptune does is going to be expressed in code between those two curly braces. And it's a very simple function, all it does is make a print statement and say, The moons of Neptune are, and we'll use some string interpolation to pass in the moons of Neptune, which we declared up here. All right, so let me run the playground, and you may come into an experience just as a little side kick, If you have a very long playground it might take awhile to execute. You can actually make your playground, not run automatically, by clicking and holding on this icon down here. So I'll let it automatically run. Okay, and it'll take a little bit of time to run, depending on how long your file is. So we, our code finished running now. It took a little while, but the code finished running and we see here, print the moons of Neptune are, and then our moons of Neptune array. So here, we get the moons of Neptune are etc.