Afrikaans
Akan
Albanian
Amharic
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
French
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
Persian
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
Now, as a challenge, I want you to write your own functions, but not inside the Chrome Developer Tools.
I want to introduce you to something called Karel the robot.
And Karel is a very simple robot that takes very simple instructions.
So let's first change the world to a five by five to make it a little bit smaller and easier to work
with.
Now the next thing that you'll notice is that you have this thing called function main.
And in this case everything that you put into here will get executed when you click the run button.
And Karel the robot, as you can see here, is able to take a certain number of commands, including move,
which moves it forward in the direction it's facing, turn left,
put down a beeper, or pick up a beeper.
So those are the basic commands that we're going to work with.
And let me just demo some of these to you.
So, if we call the function move on Karel, you can see it moves by a single space, and we can obviously
repeat this a few times.
So now if we reset and we hit run, Karel will move by three spaces, because we wrote the move command three
times.
So everything you place into this function main will get called when you hit run.
But you can create more than one function.
So, for example, I know that I can make Karel turn left by simply writing turn left, run, and she turns
left.
Now if I wanted her to go around in a circle then I can simply create a function called
goInCircle, and inside this function I can say maybe move one step,
turn left, then move, then turn left, then move.
And now I can go into this function main and I can say goInCircle, calling that function that I just
created down here, which of course will execute all of the code that's inside the curly braces.
So now if I reset and I hit run you can see that Karel goes in a half circle.
So if I want her all the way back to the beginning then I can simply call this function again, making
sure that I spelt it exactly the same way as I did when I created that function.
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
as I like in order for Karel to run my code repeatedly.
So, as a challenge, I want you to write a little bit of code that moves Karel all the way to the right
corner on a five by five world.
So, inside the resources of this lesson, you'll find a link to the Stanford Karel.
So I want you to set the world to five by five,
delete what's currently inside function main, and to write some code that gets Karel from the bottom
left corner to the top right corner,
remembering that you can always take a look at the reference to see what commands
Karel is able to use.
So pause the video now and give that a go.
All right.
So that shouldn't have been too difficult.
And there's many many ways of solving this challenge.
So it doesn't matter which way you used as long as Karel ended up over here when you hit run.
So, one way of doing this would be getting Karel to go one, two, three, four, four steps forward.
So that would be move four times
and then to turn left and then move four times again.
So now if we hit run you can see Karel ends up in the top right corner.
Now some of you might have realized that move is repeated here.
So you might have created a function that's called maybe moveFourTimes,
and inside this function you've placed four of these move commands.
So now instead of all this repeated code, you can say moveFourTimes here, and also moveFourTimes
here.
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
bit less repetitive.
So using this principle of keeping your code dry we can use functions to do that.
It allows us to remove repetition into these modules of code which makes our code shorter and allows
us to identify problems
if it does occur.
So, for example, if we’ve accidentally written the code wrong and Karel only moved three steps each time,
then we would know that we can identify the function that's meant to move four times, and figure out
what the problem was.
So the next thing I want you to use is the command putBeeper.
So Karel is able to put down this thing called a beeper, which is kind of just like a square.
And I want you to write some code that commands her to put down the beeper in a specific pattern.
So the first slightly easier challenge is, can you get her to put a diagonal line of beepers all the
way from the bottom left corner to the top right corner.
So that should be five beepers in total and I want you to try and write your code in the least repetitive
way possible using functions.
So pause the video now and see if you can complete this challenge.
OK.
So let's break down the problem. Essentially, Karel will start in the bottom left corner and she can move
forwards, turn left, move,
put down beeper.
So, one way of doing this would be writing move forwards, turn left, move forwards, put beeper, turn right,
move forwards, turn left, move forwards, and put beeper.
So, if we run our code as is, you can see that she's begun on her journey, but you might also notice that
there's a lot of repeated code in here, especially something like,
for example, move, turn left, move, put beeper, move, turn left, move,
put beeper.
So there's a lot of things that are repeated.
So what if we could put all of that into a function? So, we can create a function diagonalMoveAndBeeper.
Now of course you can name your functions whatever it is that makes sense to you but in this case I've
just called it diagonalMoveAndBeeper, because I'm going to get it to move diagonally and then put
down a beeper.
And that involves moving, turning left, moving, putting down beeper, and turning right.
So if I take this chunk of code and put it into my function then instead of calling all of these steps
I can simply say diagonalMoveAndBeeper, diagonalMoveAndBeeper.
And if we do this four times then you can see that Karel will go all the way to the end, and the only
one that's missing is the one in the beginning, the putBeeper before we do any of these other diagonal
moves.
So if we hit run now we've managed to solve our solution by getting Karel to repeat the diagonal move
and put down beeper function.
Now you might have solved this challenge in a variety of different ways and that's fine as long as you
got Karel to perform this functionality.
Then you have succeeded in completing the challenge.
Now for the final optional challenge, if you want,
I'd like you to challenge yourself to see if you can get Karel to create a chessboard pattern like
what we have here,
so alternating tiles of beepers essentially.
So this is completely optional.
It's just a bit of fun
and to test your understanding of functions. I will post the code of the solution in the resources section
of this lesson, so you can check it
once you're done trying out the challenge. In the next lesson I want to talk about a more advanced form
of functions and those are functions that allow you to specify an input.
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.