Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 Now, as a challenge, I want you to write your own functions, but not inside the Chrome Developer Tools. 1
I want to introduce you to something called Karel the robot. 2
And Karel is a very simple robot that takes very simple instructions. 3
So let's first change the world to a five by five to make it a little bit smaller and easier to work 4
with. 5
Now the next thing that you'll notice is that you have this thing called function main. 6
And in this case everything that you put into here will get executed when you click the run button. 7
And Karel the robot, as you can see here, is able to take a certain number of commands, including move, 8
which moves it forward in the direction it's facing, turn left, 9
put down a beeper, or pick up a beeper. 10
So those are the basic commands that we're going to work with. 11
And let me just demo some of these to you. 12
So, if we call the function move on Karel, you can see it moves by a single space, and we can obviously 13
repeat this a few times. 14
So now if we reset and we hit run, Karel will move by three spaces, because we wrote the move command three 15
times. 16
So everything you place into this function main will get called when you hit run. 17
But you can create more than one function. 18
So, for example, I know that I can make Karel turn left by simply writing turn left, run, and she turns 19
left. 20
Now if I wanted her to go around in a circle then I can simply create a function called 21
goInCircle, and inside this function I can say maybe move one step, 22
turn left, then move, then turn left, then move. 23
And now I can go into this function main and I can say goInCircle, calling that function that I just 24
created down here, which of course will execute all of the code that's inside the curly braces. 25
So now if I reset and I hit run you can see that Karel goes in a half circle. 26
So if I want her all the way back to the beginning then I can simply call this function again, making 27
sure that I spelt it exactly the same way as I did when I created that function. 28
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
as I like in order for Karel to run my code repeatedly. 30
So, as a challenge, I want you to write a little bit of code that moves Karel all the way to the right 31
corner on a five by five world. 32
So, inside the resources of this lesson, you'll find a link to the Stanford Karel. 33
So I want you to set the world to five by five, 34
delete what's currently inside function main, and to write some code that gets Karel from the bottom 35
left corner to the top right corner, 36
remembering that you can always take a look at the reference to see what commands 37
Karel is able to use. 38
So pause the video now and give that a go. 39
All right. 40
So that shouldn't have been too difficult. 41
And there's many many ways of solving this challenge. 42
So it doesn't matter which way you used as long as Karel ended up over here when you hit run. 43
So, one way of doing this would be getting Karel to go one, two, three, four, four steps forward. 44
So that would be move four times 45
and then to turn left and then move four times again. 46
So now if we hit run you can see Karel ends up in the top right corner. 47
Now some of you might have realized that move is repeated here. 48
So you might have created a function that's called maybe moveFourTimes, 49
and inside this function you've placed four of these move commands. 50
So now instead of all this repeated code, you can say moveFourTimes here, and also moveFourTimes 51
here. 52
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
bit less repetitive. 54
So using this principle of keeping your code dry we can use functions to do that. 55
It allows us to remove repetition into these modules of code which makes our code shorter and allows 56
us to identify problems 57
if it does occur. 58
So, for example, if weโve accidentally written the code wrong and Karel only moved three steps each time, 59
then we would know that we can identify the function that's meant to move four times, and figure out 60
what the problem was. 61
So the next thing I want you to use is the command putBeeper. 62
So Karel is able to put down this thing called a beeper, which is kind of just like a square. 63
And I want you to write some code that commands her to put down the beeper in a specific pattern. 64
So the first slightly easier challenge is, can you get her to put a diagonal line of beepers all the 65
way from the bottom left corner to the top right corner. 66
So that should be five beepers in total and I want you to try and write your code in the least repetitive 67
way possible using functions. 68
So pause the video now and see if you can complete this challenge. 69
OK. 70
So let's break down the problem. Essentially, Karel will start in the bottom left corner and she can move 71
forwards, turn left, move, 72
put down beeper. 73
So, one way of doing this would be writing move forwards, turn left, move forwards, put beeper, turn right, 74
move forwards, turn left, move forwards, and put beeper. 75
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
there's a lot of repeated code in here, especially something like, 77
for example, move, turn left, move, put beeper, move, turn left, move, 78
put beeper. 79
So there's a lot of things that are repeated. 80
So what if we could put all of that into a function? So, we can create a function diagonalMoveAndBeeper. 81
Now of course you can name your functions whatever it is that makes sense to you but in this case I've 82
just called it diagonalMoveAndBeeper, because I'm going to get it to move diagonally and then put 83
down a beeper. 84
And that involves moving, turning left, moving, putting down beeper, and turning right. 85
So if I take this chunk of code and put it into my function then instead of calling all of these steps 86
I can simply say diagonalMoveAndBeeper, diagonalMoveAndBeeper. 87
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
one that's missing is the one in the beginning, the putBeeper before we do any of these other diagonal 89
moves. 90
So if we hit run now we've managed to solve our solution by getting Karel to repeat the diagonal move 91
and put down beeper function. 92
Now you might have solved this challenge in a variety of different ways and that's fine as long as you 93
got Karel to perform this functionality. 94
Then you have succeeded in completing the challenge. 95
Now for the final optional challenge, if you want, 96
I'd like you to challenge yourself to see if you can get Karel to create a chessboard pattern like 97
what we have here, 98
so alternating tiles of beepers essentially. 99
So this is completely optional. 100
It's just a bit of fun 101
and to test your understanding of functions. I will post the code of the solution in the resources section 102
of this lesson, so you can check it 103
once you're done trying out the challenge. In the next lesson I want to talk about a more advanced form 104
of functions and those are functions that allow you to specify an input. 105
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.