WEBVTT 0 00:00.240 --> 00:06.240 Now, as a challenge, I want you to write your own functions, but not inside the Chrome Developer Tools. 1 00:06.360 --> 00:09.470 I want to introduce you to something called Karel the robot. 2 00:09.570 --> 00:15.390 And Karel is a very simple robot that takes very simple instructions. 3 00:15.390 --> 00:22.860 So let's first change the world to a five by five to make it a little bit smaller and easier to work 4 00:22.860 --> 00:23.840 with. 5 00:23.850 --> 00:28.650 Now the next thing that you'll notice is that you have this thing called function main. 6 00:28.770 --> 00:35.470 And in this case everything that you put into here will get executed when you click the run button. 7 00:35.580 --> 00:42.690 And Karel the robot, as you can see here, is able to take a certain number of commands, including move, 8 00:42.810 --> 00:47.310 which moves it forward in the direction it's facing, turn left, 9 00:47.310 --> 00:50.190 put down a beeper, or pick up a beeper. 10 00:50.280 --> 00:52.670 So those are the basic commands that we're going to work with. 11 00:52.680 --> 00:54.990 And let me just demo some of these to you. 12 00:55.290 --> 01:03.510 So, if we call the function move on Karel, you can see it moves by a single space, and we can obviously 13 01:03.510 --> 01:05.580 repeat this a few times. 14 01:05.580 --> 01:14.190 So now if we reset and we hit run, Karel will move by three spaces, because we wrote the move command three 15 01:14.190 --> 01:15.080 times. 16 01:15.120 --> 01:20.250 So everything you place into this function main will get called when you hit run. 17 01:20.250 --> 01:22.890 But you can create more than one function. 18 01:22.890 --> 01:31.230 So, for example, I know that I can make Karel turn left by simply writing turn left, run, and she turns 19 01:31.230 --> 01:31.800 left. 20 01:31.800 --> 01:38.150 Now if I wanted her to go around in a circle then I can simply create a function called 21 01:38.190 --> 01:48.110 goInCircle, and inside this function I can say maybe move one step, 22 01:48.270 --> 01:58.870 turn left, then move, then turn left, then move. 23 01:58.870 --> 02:07.360 And now I can go into this function main and I can say goInCircle, calling that function that I just 24 02:07.360 --> 02:14.730 created down here, which of course will execute all of the code that's inside the curly braces. 25 02:14.740 --> 02:21.650 So now if I reset and I hit run you can see that Karel goes in a half circle. 26 02:21.700 --> 02:28.270 So if I want her all the way back to the beginning then I can simply call this function again, making 27 02:28.270 --> 02:34.630 sure that I spelt it exactly the same way as I did when I created that function. 28 02:34.630 --> 02:43.720 So now, if I reset and hit run, you can see that it turns a full circle, and I can write as many of these 29 02:43.720 --> 02:48.400 as I like in order for Karel to run my code repeatedly. 30 02:48.550 --> 02:57.040 So, as a challenge, I want you to write a little bit of code that moves Karel all the way to the right 31 02:57.130 --> 03:00.310 corner on a five by five world. 32 03:00.310 --> 03:05.790 So, inside the resources of this lesson, you'll find a link to the Stanford Karel. 33 03:05.800 --> 03:08.630 So I want you to set the world to five by five, 34 03:08.650 --> 03:14.740 delete what's currently inside function main, and to write some code that gets Karel from the bottom 35 03:14.740 --> 03:17.120 left corner to the top right corner, 36 03:17.320 --> 03:21.770 remembering that you can always take a look at the reference to see what commands 37 03:21.770 --> 03:24.100 Karel is able to use. 38 03:24.100 --> 03:27.450 So pause the video now and give that a go. 39 03:27.940 --> 03:28.290 All right. 40 03:28.300 --> 03:30.930 So that shouldn't have been too difficult. 41 03:31.030 --> 03:34.410 And there's many many ways of solving this challenge. 42 03:34.540 --> 03:42.190 So it doesn't matter which way you used as long as Karel ended up over here when you hit run. 43 03:42.190 --> 03:49.480 So, one way of doing this would be getting Karel to go one, two, three, four, four steps forward. 44 03:49.480 --> 03:52.420 So that would be move four times 45 03:56.990 --> 04:01.530 and then to turn left and then move four times again. 46 04:07.140 --> 04:12.670 So now if we hit run you can see Karel ends up in the top right corner. 47 04:12.690 --> 04:16.490 Now some of you might have realized that move is repeated here. 48 04:16.560 --> 04:22.470 So you might have created a function that's called maybe moveFourTimes, 49 04:25.670 --> 04:30.390 and inside this function you've placed four of these move commands. 50 04:30.410 --> 04:39.930 So now instead of all this repeated code, you can say moveFourTimes here, and also moveFourTimes 51 04:41.340 --> 04:42.250 here. 52 04:42.870 --> 04:49.560 So this does exactly the same, as we can see if we reset it, but it's now a little bit shorter and a little 53 04:49.560 --> 04:50.810 bit less repetitive. 54 04:50.940 --> 04:56.700 So using this principle of keeping your code dry we can use functions to do that. 55 04:56.700 --> 05:04.320 It allows us to remove repetition into these modules of code which makes our code shorter and allows 56 05:04.320 --> 05:06.370 us to identify problems 57 05:06.540 --> 05:07.670 if it does occur. 58 05:07.740 --> 05:14.820 So, for example, if we’ve accidentally written the code wrong and Karel only moved three steps each time, 59 05:15.240 --> 05:20.550 then we would know that we can identify the function that's meant to move four times, and figure out 60 05:20.550 --> 05:22.560 what the problem was. 61 05:22.560 --> 05:27.090 So the next thing I want you to use is the command putBeeper. 62 05:27.480 --> 05:34.220 So Karel is able to put down this thing called a beeper, which is kind of just like a square. 63 05:34.320 --> 05:41.650 And I want you to write some code that commands her to put down the beeper in a specific pattern. 64 05:41.790 --> 05:50.400 So the first slightly easier challenge is, can you get her to put a diagonal line of beepers all the 65 05:50.400 --> 05:54.580 way from the bottom left corner to the top right corner. 66 05:54.600 --> 06:01.440 So that should be five beepers in total and I want you to try and write your code in the least repetitive 67 06:01.500 --> 06:04.840 way possible using functions. 68 06:05.400 --> 06:09.500 So pause the video now and see if you can complete this challenge. 69 06:10.470 --> 06:10.870 OK. 70 06:10.880 --> 06:19.160 So let's break down the problem. Essentially, Karel will start in the bottom left corner and she can move 71 06:19.160 --> 06:22.590 forwards, turn left, move, 72 06:22.700 --> 06:23.760 put down beeper. 73 06:23.870 --> 06:35.060 So, one way of doing this would be writing move forwards, turn left, move forwards, put beeper, turn right, 74 06:36.820 --> 06:44.110 move forwards, turn left, move forwards, and put beeper. 75 06:44.110 --> 06:50.860 So, if we run our code as is, you can see that she's begun on her journey, but you might also notice that 76 06:50.890 --> 06:55.270 there's a lot of repeated code in here, especially something like, 77 06:58.360 --> 07:03.900 for example, move, turn left, move, put beeper, move, turn left, move, 78 07:03.910 --> 07:04.670 put beeper. 79 07:04.780 --> 07:07.560 So there's a lot of things that are repeated. 80 07:07.570 --> 07:17.690 So what if we could put all of that into a function? So, we can create a function diagonalMoveAndBeeper. 81 07:17.920 --> 07:22.900 Now of course you can name your functions whatever it is that makes sense to you but in this case I've 82 07:22.900 --> 07:28.030 just called it diagonalMoveAndBeeper, because I'm going to get it to move diagonally and then put 83 07:28.030 --> 07:29.020 down a beeper. 84 07:29.440 --> 07:36.640 And that involves moving, turning left, moving, putting down beeper, and turning right. 85 07:36.790 --> 07:44.680 So if I take this chunk of code and put it into my function then instead of calling all of these steps 86 07:44.770 --> 07:50.760 I can simply say diagonalMoveAndBeeper, diagonalMoveAndBeeper. 87 07:50.800 --> 07:57.580 And if we do this four times then you can see that Karel will go all the way to the end, and the only 88 07:57.580 --> 08:04.660 one that's missing is the one in the beginning, the putBeeper before we do any of these other diagonal 89 08:04.660 --> 08:05.710 moves. 90 08:05.770 --> 08:12.880 So if we hit run now we've managed to solve our solution by getting Karel to repeat the diagonal move 91 08:12.940 --> 08:14.720 and put down beeper function. 92 08:15.010 --> 08:20.830 Now you might have solved this challenge in a variety of different ways and that's fine as long as you 93 08:20.830 --> 08:24.320 got Karel to perform this functionality. 94 08:24.320 --> 08:27.480 Then you have succeeded in completing the challenge. 95 08:27.580 --> 08:30.870 Now for the final optional challenge, if you want, 96 08:31.030 --> 08:37.360 I'd like you to challenge yourself to see if you can get Karel to create a chessboard pattern like 97 08:37.360 --> 08:38.690 what we have here, 98 08:38.860 --> 08:43.100 so alternating tiles of beepers essentially. 99 08:43.240 --> 08:44.920 So this is completely optional. 100 08:44.920 --> 08:46.110 It's just a bit of fun 101 08:46.150 --> 08:52.150 and to test your understanding of functions. I will post the code of the solution in the resources section 102 08:52.210 --> 08:54.080 of this lesson, so you can check it 103 08:54.190 --> 08:59.650 once you're done trying out the challenge. In the next lesson I want to talk about a more advanced form 104 08:59.650 --> 09:05.200 of functions and those are functions that allow you to specify an input. 105 09:05.200 --> 09:08.530 So for all of that and more, I'll see you on the next lesson.