In the introductory reading for this module we talked about the complexity of programs, and I encouraged you to look at the screen in front of you and imagine how many commands must be required to instruct the computer to draw all that you can see. Human beings are quite simple creatures and it is hard for us to handle huge numbers of things; in this case this huge number of commands. But we are very creative and we often come up with ways of handling all of that complexity in our lives and we should start to explore one of those just now to tame the complexity of large programs. As an example, take a look at this winter seen that my six-year-old drew. I thought that we could write a turtle program to draw more or less the same picture. Here's the code. You can see there's a lot of it even for a simple drawing. It's up to about 130 lines nearly. Let's have a look at what it actually draws as well, so I'll shift to the execution screen. There we go. I'm just going to run that code and there it is. It's not exactly the same but you can see the relationship between my daughter's drawing and see the code; sorry there it is, and the drawing from turtle code. Well, how are we going to make this code simpler? Well, we're going to break it into pieces typically at sensible division points in relation to the task that is being solved. Remember, the task aspect of the code. We're going to make each piece into a function. That's an important word to remember; a function, and that's part of the machine side. It's a programming language concept. Well, what are these sensible divisions that I talked about for this task? While at the high-level there's the hedge there with the gate in it, and there's the tree, and then there's the sun as well. Why don't we start with those? Let's switch back to the code. We'll start off with the sun because that's the smallest piece of code here. To make a function, to break up a large piece of code like this into functions, we're going to create what's called a function definition. We start that with a keyword function and then we give the function a name, so we can refer to this piece of code and we're going to call this one sun because it's to help us draw some of the sun. Then when we want to package up a piece of code into one lump, we use the curly brackets and we've seen that before. For example, in this little for loop here, it's the forward and the right are the pieces that are going to get repeated and we've put them together inside the open and closed curly brackets. In just the same way some code that we want to package up we're going to put that inside curly brackets as well, and then what we're going to do just to make this look neat. We don't absolutely have to do this but making code look neat is very important for readability, so we're just going to tab in each one of these lines; all of these lines here, and that makes it very clear to be able to see which code is inside sun and which code is outside. I'm going to do that for the other functions for the tree and the hedge, and I'm going to just pause for a moment while I do that. Here we are and we've got a function for the hedge and the gate, you can see that's all been indented and then we've got a function for the tree and then what we did just a minute ago we've got a function for the sun. To reiterate we've broken the code into pieces that are called functions, and we've given each piece a name that's known as the function name. The function name is specified in the function header; that's the piece with the keyword function, and then the piece of code itself is called the function body. Well, now we've split the code into three pieces and named each one That's great. Let's see what it all looks like. I'll just shift over to the execution screen, that's this one. I'll clear what we had before, so I'll reset the turtle there, and I'll set the code running. I'm just about to press a button. There we go. That little blue on the Console Output shows that the program was running but nothing seems to have happened. Let's just try that again. I'll reset the turtle. We've got the turtle in screen, and then I'll run it. Nothing has happened again. Can you tell why that is? I wonder what's going on? Well, that's because a function doesn't do anything until it's called. You know all about this already because the turtle operations we've been using are just functions as well. We don't see the function definitions. We just used them or called them, so to you the calling bit is really nothing new and so let's go ahead and add the calls in just now. There's a little bit of code right at the top here outside that just starts us up; the reset() and penup(). We'll take those from here at the beginning and we'll put them down at the end. We've got our three function definitions, but what we're going to do is create a little piece of code that is often called the Main program. It's the master plan, and it will make sure all those other bits get done as well. We've put in the reset( ) and the penup( ) from the top of the program. Now we can just call the three functions that we've created, so reminder name hedgeAndGate( ), and then tree( ), and then sun( ). Let's do that hedge, hedgeAndGate( ), tree( ), and sun( ). Effectively, this little bit of Main program here, that's like our master plan. When this whole program now gets executed remember that a program gets executed from top to bottom and each instruction as a computer finds it gets executed unless there's something like a loop. But when there's functions in the program something slightly different happens. When a computer sees that we've got a function definition as we have here, it just remembers that it's got this definition and it remembers the code that's associated with that name hedgeAndGate( ) here. It's just going to remember all of this code coming afterwards; all this code down here. It's not actually going to execute that so it jumps past this whole piece. It jumps through to 57 here; line 57 and then it sees another function definition at 59. It goes, "Okay, well I'll remember that there's a function called tree( ) and it's got the code from 60 right through to wherever that is; 60-119 there." It remembers that so now it's got hedgeAndGate( ) and it's got tree( ). It still hasn't executed anything at all yet just like we discovered. Then it's got another function called sun( ) and it remembers this piece of code as well. Then finally it actually gets to the code that's not in a function and a function definition, and it can get on and it can execute these pieces of code. What will happen here is it'll do the reset( ) and the penup( ), and then when it sees hedgeAndGate( ) It says if. It jumps from line 137 here; it's a code of hedgeAndGate() because of the open close bracket piece and that's crucial. It's a code of this function and so it jumps from 137 and it goes at backup, and finds the hedgeAndGate( ) code and then starts executing up here from line 3 and then it starts doing 5, 6, 7, and so on and that's when the actual hedgeAndGate( ) get drawn. When it's gone through all of that code and it's got to line 56, it goes, "Okay, well that's the end of hedgeAndGate( ), I'll jump back down to where I was called from." I was called from here; line 137, and then it goes on to 138. At that point it sees it tree( ), so it jumps up to wherever tree( ) is which is here at 59 and it executes the code from 60 through to 119. Then it jumps back down to here where tree( ) what's called from and then it called sun(). The sun(), it jumps up to 122, executes through to 131, jumps back down to 132. It jumps back down and then hides a turtle, and we're done. Let's just see that in action just to convince yourself so that it really does work. I'm pressing the wrong button now and there we are, then we can see our tree now. Just to show that something would happen, it would look different if I change the code up here. Let's just say I comment out //sun(), so it's going to be a bit of a wintry winter day. Without any sun, I'm just going to reset the turtle there. Let's go back to the run screen. There we go. I'm just going to run that again now and you can see we didn't get same thing, but now there's no sun. Whoa, it's a bit cold. We could now of course break down the hedge code further. If you look at the actual hedge code itself up here, it's quite a big piece of code with the left-hand side of the hedgeAndGate( ) and then the right-hand side of the hedge. In fact, we could break this down further into functions for a hedge section like that green piece and then the gate itself. We can have two functions, one for a hedge section and one for the gate. Then we could in fact call a hedge section twice just make sure the turtle is in the right place each time and it would do the same thing because it's exactly the same. Isn't it? It's a thick green line basically, that's what the hedge section is. We could call that hedge section twice given that those two hedge sections are identical. That's a second way of reducing complexity by describing an action just once, but then using it or in fact reusing it many times. Reuse is an important concept. Of course you've been doing that whenever you've been using the turtle functions, you've reused them many times. They've been written just once, but you've used in many times. We'll look at that reuse in the worked example that comes up next after this video and then you can practice breaking code up into functions and you can practice those reuse part that we're talking about. Well to reiterate, a function is a mini program or a fragment of code that's packaged up for use within the program you're writing it in or for use with another programs. A function has a header in which a name for the function is defined and it has a body which contains the code to be executed when it's used. A combination of their function header and a function body together is known as a function definition. To use a function from another section of the program or from another program altogether, we use the same function calling technique that we've used with the turtle operations or technically the turtle functions. That is, we write down the function name along with the open and closed brackets; that's the function called syntax. Then we can call a function definition where it can be used many times in the program which is defined or in other programs. Now, you can go ahead and try the tutorial that comes after this with a worked example that will help you practicing with functions.