The name of this video is functions lesson. So congratulations, you made it all the way to functions, and this is our last part of our control flow. It's really the last part of our series of lectures that relate specifically to Python-oriented structures within the code. And functions are actually something that we have already been using all along, and I've been referencing things as functions both in Rhinoscript and in Python. Up to this point within the course, you sort of can't do anything in the coding without the use of functions. But they actually require more explanation. Because we can now start to create our own functions and use functions in a different way within the code, to structure it and control the flow of data in more specific ways throughout the code, and that's what functions are going to allow us to do. Now, looking at this line of code, so this is a function from RhinoScript that we are already familiar with. So AddLine, which creates a straight line segment between two points in Rhino. This is a function, and it takes two arguments. And so the arguments in a function are defined between its parentheses, and also separated by commas if I have multiple arguments. So those are really the spaces for the arguments. And then I'm assigning it two point values. And so that nomenclature is important, that the slots themselves are the arguments, and what I do is assign data to those slots. A function also, we always return something. When I call a function, which is what I do when I write it out, it's going to return something whether I choose to save that thing, it's returning in a variable, or in a list, or whatever, it's still returning something. So in this case, AddLine returns an idea of a line, which I can then see if I use my print function. So there are a lot of functions in Rhinoscript. We've been using a lot of them, and they take all different types of data, and that data type changes depending on the function. And we've looked a lot in the Rhinoscript help menu, when we look under parameters, we're looking for what a function needs when we call it, what type of data does it need? In what format? Does it need a tuple? Does it need a string? Is it looking for an ID? So all those things are very specific. And we've been using those in a lot of different ways, and we'll continue to use them. We've been using a number of functions in Python also. So the function len, which is a very simple but useful function, right, returns the length of a list. And I could save that integer that it returns in a variable, and then print it out. Print is also a function in Python we've been using quite a bit, it's in some ways a very unique function, in the fact that I can give it any number of arguments. I could give it one argument, and I could give it quite a high number of argument, it's separated by a comma, so I could print out a number of things. The other thing that makes it very unique is that it doesn't require parentheses. It's one of the only Python functions that doesn't require a parentheses, with parentheses, it will work without parentheses. And that's just based on the version of Python that we're using in our Rhino editor. Now, let's think about what this function does a little bit. Go back to this, when I call this function, and I give it the data which are these two points, it goes and it does something in the background. Now, there's a whole background code that's going on behind the scenes. And that background code creates the line in my Rhino space, and then it returns to my Python space, the ID, the reference for that line. And we don't see any of that background code. What we're seeing is the kind of foreground operational code that we see through the editor. However, when we start to create our own functions, what we're really doing is creating our own background code, and this is the format for creating our own function. So it starts in line 1, we write D-E-F, followed by a space, and then we write the name of our function. And that name of the function has to adhere to all of the rules and constraints, the conventions that we've used for naming variables, up until this point. So that name is then followed by a close parentheses. And then a colon which we've become very familiar with, which then produces the indent. So anything that's indented exists within that function. And then I unindent here, so here I'm outside of that function. So I call that function from outside, I can't call a function from inside. We're probably not going to do that in this course, that deals with recursion, that's another level of programming. So I'm typically calling a function from outside of it, which I do just by writing its name with the close parentheses. So I call that function, and it runs whatever's in the function. And then my return here is returning, where is it returning to? Well, it's returning to wherever I called that function from. So it's going to return whatever data that I've said to return here in that variable string statement. So it returns that and then it prints it out. When I create a function, I can also within those parentheses define arguments. So I've defined two arguments, and again, I'm just making those names up. But what it does is when I create those arguments, it creates two slots. So when I call the function here, I can put data into those two slots. And that data is then assigned to whatever I've named my arguments here. Those arguments then become variable names, that carry that data through into my function. So when I multiply 2 and 5 together, again, I'm returning here the results of that, that's returned to here, which is then printed out. And it prints out 10. So that's very important, these arguments, they're creating essentially slots of space that I'm moving data through, I move data into the function through those arguments. And I can set my call of my function. In this case, I set the call of my function equal to a variable, which allows me to save what's being returned from that function. Here I'm setting up three arguments, 1, 2, 3, and I'm giving it three pieces of data. Now, I have to give it the same exact number pieces of data that I've set up for arguments. If I try to give this two, I will get an error that's saying I'm not giving it enough. And if I give it four, I'm going to get an error which is saying I'm giving it too much. So I need to give it for exactly what I've set it up for. And there's nothing new here in terms of what I'm doing with the data. I'm converting an integer to a string, and then I'm catenating those things together, and then I'm returning then how many holds whatever is returned, and then I'm using the print function here to print out that result. Now, if I assign a variable inside of a function, I can use that variable inside the function, I can also return it. However, I can't use that variable outside. So this is outside of the function. It's not going to know what that is. It'll know this will return x to here because I've called that function here, but it's not going to know what x is because I'm asking it for x completely outside of the function. And it doesn't understand what I'm asking for, it says it's not defined. If I define that x outside of the function, and actually outside of any function, I'm creating what's called a global variable, and then it's available to pass into, here I'm passing it into this function. And this is a kind of funny thing, I'm just passing it into, just to pass it out to make a point. But here, 5 is going through here, this is returning x, which is 5 goes in through here, and then it's just returning it back to this which is also returning 5, so 5 times 5 is 25. Now, typically when I'm doing global variables, I won't just write them anywhere within the code. If I'm writing a variable that I really truly want to use as a global variable, which is actually kind of rare, then I would probably write it in this way. I would write it at the top of my code, and I would probably put a comment there that says global variables, and makes it accessible anywhere within the code. Now, we're going to see when we start to write functions within the code, we're going to write them in a complete sort of function structure. So all of the code is going to be broken up into separate functions. We're going to have something called the main, which sort of starts my code running, and it's where all my inputs are going to go through. And we're going to go over that within the software tutorials. So why would we choose to use functions? Well, part of what I described in this lesson was the fact that not only were functions little tools that we could use within Rhinoscript and Python that we've been using all along actually, but functions are little tools that we can actually invent ourselves. So we can create our own tools that we could use in not only one code, but in multiple codes. And so they allow us to make certain parts of the code mobile and useful in other codes. A very important reason to use functions is as our programs get longer and we write them in a linear fashion, they get much harder to work on, they get more complex. And so using functions allows us to chunk the code into a bunch of separate digestible parts, and to work on them separately. With that same thing in mind, it makes things like debugging easier because I can turn off specific parts of the code to work on other parts of the code. This chunking also allows us to work on the code, let's say within a team of people. And so one person could work on a function to do one thing, another person could work on another part of the code, another function to do something else. And so that's very useful with working large parts of code. The other thing is, is that it allows us to control the flow of the code in a nonlinear way. And so up until this point, we've sort of been writing these linear codes from top to bottom, which is useful for understanding it at an early stage. But now if we can write things as functions, we can access different parts of the code in different ways. And then actually, it's a little bit difficult to get used to it at first. People have difficulties with using functions, but once you get used to it, you're not going to want to write a code in any other way. So that's functions, and we're going to take a look at a lot of different examples, and we're going to write all of our code within a function structure from now on.