Page 106 - Learn To Program With Scratch
P. 106

try it out 4-2
                            What do you think will happen if you set the initial direction to 0° (up) instead of
                            90° (right)? Will the script work? If not, how could you fix it? Make this change
                            and run the script to test your answer .




              working with Procedures

                         Now that you know why it’s important to break your program down into
                         smaller parts and tackle them one at a time, let’s discuss how to perform
                         this division. Every problem is different, and there is no “one size fits all”
                         solution—but that’s what makes this a fun puzzle!
                             In this section, we’ll first explore the top-down process of dividing a large
                         program into modular pieces with a clear logical structure. We’ll then dis-
                         cuss another way of building complex programs: the bottom-up process of
                         combining existing procedures. Figure 4-23 shows a high-level view of these
                         two approaches.

                           Start  Top-Down                   End  Bottom-Up
                                Process                          Process








                           End                               Start

                         Figure 4-23: Illustrating top-down (left) and bottom-up (right) approaches

                             In both diagrams, the problem we want to solve is at the top, and the
                         individual steps that build our solution are at the bottom. You can start
                         from whichever level makes sense to you.

                         Breaking Programs Down into Procedures
                         The first step in solving any programming problem is to fully understand
                         the problem. After that, you can plan a general solution and divide it into
                         major tasks. There is no right or wrong way to go about dividing up any par-
                         ticular program, and with experience, you will get better at deciding what
                         “major” means. Working from the general solution down to its specifics
                         ensures that, at least, the overall logic of the program is correct.
               House .sb2    To demonstrate this problem-solving strategy, let’s consider how we
                         would draw a house similar to that shown in Figure 4-24.





           84   Chapter 4
                                           www.it-ebooks.info
   101   102   103   104   105   106   107   108   109   110   111