hi in this video we're going to talk about top-down design and decomposition to review we use functions in our programs to break down our program into smaller parts to avoid repeating code and to make our programs more readable in this lesson we're going to talk about a strategy that helps us figure out what functions we need to write to solve a problem so this is basically a problem solving strategy it's called top-down design with top-down design we take a big problem and we break that problem down into several smaller problems we can then break each of those problems down into even smaller problems we continue to do this until we have a problem that is simple and easy for us to solve so even though we start with this big problem that may seem complicated by breaking the problem down into smaller problems than solving each of those smaller problems we make our program much more manageable and our solution much more clear so for example if you were working on a movie well that's a big task you may not even know where to start there are several things we need to do we need to write a script we need to hire a cast film the movie Edit the movie but each of those are big problems as well so for filming the movie we might break that down into filming scene one filming scene two foaming scene three we might break the script down into similar tasks eventually we get to the point where the tasks are so simple that they can be solved with one function so why do we call it decomposition well decomposition means breaking your program down into small manageable parts that are easy to understand we decompose large messy functions into several small simple functions it's all about readability so remember this example from earlier the program had all of our commands and it was very hard to read and understand what was going on by breaking it down we decompose the different parts of this program that can become functions and we have a much more readable program that can look like this so with this program it's easy to see what's going on we have taken a problem of Karo going to pick up a stack of balls and broken it down we have a much more high-level view of our problem now instead of trying to solve the entire problem at once we by breaking it down we have smaller more manageable problems to solve we can go in write the pot go to pile function then write another function to pick up the pile and finally come back to the start each of those may need to be broken down into smaller parts as well but at a high level we've just solved our problem right here so why do we do top-down design or several reasons first it helps us to solve large complicated problems by breaking the problem into smaller more manageable chunks second it helps us make our code more readable when we have a program like this it's pretty easy to understand what's going on versus a program like this where we can't really tell exactly what's going on so finally top-down design allows us to collaborate with others by splitting the problem into subproblems that can be independently solved if I am trying to write a program with others it's much easier to split into sub functions so for example one person can write the function to go to the pile another writes that pickup pile function and a third can write the come back to the start function then we can combine them together to solve our problem so let's take a look at top-down design in the editor okay so let's take a look at top-down design in action so what we want to do here is we want to have care out run jump over these hurdles and then run to the finish line so how do we do that well if we think of it from a top-down design perspective it's pretty straightforward we're gonna say run to hurdle then Carol needs to jump the hurdle and then run to the next start also run to hurdle again and then jump the next start up so jump hurdle and then finally run to the finish line so we can see each of these tasks is pretty straightforward but if I run it now nothing happens we haven't defined these functions yet but if we can solve each of these functions each of these little problems then will actually solve our big problem so let's start by looking at how do we solve this run to hurdle so we're gonna define a function called run to hurdle and they run to a hurdle almost Carol has to do is move three times so we're just gonna say move move and move okay so that'll get Caro to the hurdle next we need to have Carol jump at the hurdle so we're going to fine jump hurdle okay and jumping the hodo is pretty straightforward as well we're gonna turn left move turn right move TURN RIGHT move turn left so we're gonna say turn left and then we're gonna say move then we're gonna say turn right and then we're gonna say move would do another turn right another move and then finally a turn left and we'll be ready to go okay so then we'll be back over the hurdle now we're ready to go to the next one now of course we haven't defined turn right yet and so what we have to do is kind of come up here and define turn right so we'll go do that so I say defined turn right and as we've seen turn right can just be three left turns so turn left turn left and turn left okay so now we've jumped the hurdle we've gone to the next hurdle we've jumped to the next one and notice we don't have to redefine our functions we can just reuse those functions so the last problem that we have to solve is the run to the finish line so when I say define run to finish okay and then bring to the finishes pretty straightforward he start Carol starts here so one two three four run four moves that we should be good to go so one two three and four all right so let's give that a try where we set our code and let's give it a try perfect Carol did it we made it to the finish and that's it so again this is our top-down design we've created a program that is really pretty simple right here and then we've gone through and solved each of those smaller pieces and that's helped us solve this problem really easily and now if we look at our code to read it it's pretty straightforward what perros doing to do this code just running jumping run jump and run to the finish so now it's your turn to play around