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
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
1
The first concept I want to introduce you to is something called functions.
2
And just as we perform many different functions could be mother, father,
3
son, daughter,
4
we can also get our code to perform different pieces of functionality.
5
Now, if we think back,
6
we've actually already come across functions and I've been referring to them
7
every time we've needed to use one.
8
If you take a look at this link in the course resources
9
which takes you to the Python documentation,
10
then you can see that Python has a whole bunch of built in functions that we've
11
been using. For example, the len function,
12
which gives us the number of items in a collection or the int function
13
which turns something into an integer or the print function,
14
the range function, and other ones that we haven't seen yet.
15
If you head over to the Day 6
16
starting Repl.it and go ahead and forward a copy of it.
17
Then we can start typing one of the functions that we've been using a lot,
18
which is the print function.
19
And the reason why we know it's a function is because it's the name of a
20
function followed by a set of parentheses.
21
Now we know that if we put something inside these parentheses,
22
say, for example, hello, then when I run the code,
23
whatever is inside those parentheses
24
will get outputted over here in the console.
25
Now all of the functions work pretty much the same.
26
So if I wanted to use the len function to see, well,
27
how many characters are there in this word Hello? I, again,
28
put my hello inside the parentheses because of the parentheses
29
we know that this is a function and then I can save the outcome of this length
30
function to a variable, say,
31
let's call it number of characters, num_char.
32
And then I can go ahead and print it in order to view it inside the console.
33
So let's print the num_char,
34
and now you can see that this len function gets to work, calculates the
35
number of characters, it's five, and then it gets printed.
36
So all of this functionality has been achieved by these built-in functions from
37
Python, like print and len.
38
Now what if we wanted to make our own functions?
39
How would we do that? Well, if we want to make our own function,
40
we first start out with a keyword,
41
which is called def.
42
And this is because we're creating or defining our function.
43
Now, after the def keyword,
44
we can give our function a name and I'm just going to call it my_function.
45
Now, the thing that differentiates a function from a variable, however,
46
is the parentheses. So after the name comes the parentheses,
47
and now the final thing,
48
the finishing touch to our function definition is a colon because that says
49
everything that comes after that line and is indented belongs with the
50
function.
51
Let's make a really simple function that just has two lines of code.
52
Maybe it prints Hello and then it prints Bye.
53
So now I've created my function,
54
I've placed the lines of code that are associated with this function all indented
55
after the definition. But notice how,
56
if I go ahead and run my code, actually nothing will happen.
57
And the reason is because we haven't yet executed the function.
58
Now we can define lots and lots of functions ahead of time.
59
And it's only when we actually need it
60
do we go ahead and trigger it and to trigger it or in programming lingo,
61
we would say to call the function,
62
all we have to do is type the name of the function,
63
which is my_function
64
and then to add the parentheses and any necessary inputs. In our case,
65
our function doesn't require any inputs so we can leave the parentheses blank
66
again. And now if I go ahead and run my code once more,
67
then you'll see that by the time the computer reaches line 5,
68
it's going to search for this thing called my_ function.
69
It sees that it was defined right here on line 1
70
and it goes through all of the content and executes them in turn, line by line.
71
And that's why we get hello and bye being printed here.
72
So just to recap, this is how we create a function in Python.
73
There's two steps to it. The first step is to actually define the function,
74
to specify what it should do.
75
And we do that by first using the def keyword and then we give our function a
76
name. So for example, in this case,
77
I called it my_function and then comes a set of parentheses and a colon.
78
And then after that, we get to put the lines of code
79
which will be included in this function. And remember that these lines of code
80
which goes into the function
81
which will be carried out when this function is triggered
82
must be indented like this.
83
So once you've defined the function, you've created the recipe,
84
the next step is to actually use it, which in programming lingo,
85
we would say calling the function. And we call the function just by specifying
86
the name and a set of parentheses.
87
And once the computer sees this line of code it'll know to go and carry out all
88
the instructions inside
89
where we defined our function. And to learn about functions,
90
I'm to introduce you to something that's very similar to Karel the robot.
91
The robot is going to be something that is just going to perform the tasks that
92
we want it to, and no more and no less.
93
So for example,
94
if we wanted a robot to go to the store and pick up some milk for us,
95
we can't simply just tell it, Oh yeah,
96
just go and buy some milk because it won't know how to do that
97
unless
98
we give it specific instructions such as leave the house,
99
walk two blocks to the right, walk four blocks back and then two blocks to the right.
100
When you go to the store,
101
give them some money and then take the milk and come back.
102
We have to program each and every step. But say,
103
if this functionality of getting the robot to go and pick up some milk for us is
104
needed every single day
105
then we have to write out all of those instructions day after day,
106
day after day. And at some point our fingers are going to hurt, right?
107
We're typing so much code that's repeated because we don't have a way of bundling
108
all of those instructions together. And that's where functions come in.
109
Functions will give us a way of referring to all those instructions
110
at the same time.
111
That way we can give our robot a single instruction and it will carry out all of
112
those little steps for us and get us some milk.
113
Now to try this out yourself,
114
I want you to head over to the course resources and click on the link that takes
115
you to Reeborg's World.
116
Now Reeborg's World is very similar to Karel the robot that I showed you
117
earlier on,
118
but it allows us to write Python code. And it's very easy to get started.
119
Now we've got this area here which we're going to use to write our Python code.
120
And then once we're done, we can click on play to
121
run the Python code and see the instructions carried out by our robot.
122
But if you click on Reeborg's keyboard,
123
you can see all of the functions that we can use to command our robot,
124
like move or turn left, or build a wall, et cetera.
125
Let's see if we go ahead and run this line of code here, move.
126
What happens? When I click play
127
you can see that our robot moves forward in the direction that it's facing by
128
one step. So if we wanted it to move, say for example, three steps,
129
all we have to do is the call the move function three times.
130
Now let's reset by clicking the return button to return our robot back to the
131
starting position.
132
Now let's click play again in order for our robot to carry out the new
133
instructions.
134
Notice how all the instructions were carried out very quickly. To execute each
135
instruction one step at a time, line by line,
136
we can use this step-through button.
137
This button allows us to see how each instruction is executed and after each
138
instruction, it pauses and highlights the next instruction.
139
This is very helpful for debugging.
140
So notice how we're highlighting line 2. So if I click on step again,
141
it's going to carry out that function and then it's going to carry out line
142
3 and finally it reaches the end.
143
And that's the end of our code. Now,
144
in addition, we can use some of the other commands in the keyboard
145
for example, turn left.
146
If I decide to say move three times and then turn left and then maybe move three
147
times again, can you predict which square the robot is going to end up in?
148
Are you ready? Let's go ahead and run the code. There we go.
149
We end up on the square 4,4.
150
It moves three forwards turns left and then moves three steps again,
151
because that's what we told it to do with our Python code.
152
So now here's a question. If you take a look inside the set of commands,
153
you'll notice that there is move and there is turn left. But there is no turn
154
right and there is no turn around.
155
So how could we use a function to create those commands?
156
Because, of course, we could just simply write,
157
turn left and then turn left again.
158
And this would effectively turn the robot around, right?
159
But if we want to do that many times, say for example,
160
if we wanted to go to three, turn around, go back to one, turn around,
161
go back to three, then we would have to write code that looks a bit like this.
162
We would have to say move
163
two times to get to square three, turn left, turn left,
164
and then move two times again to get to one and then turn left,
165
turn left again in order to turn around.
166
This is all the code we would have to write to achieve this kind of effect.
167
But instead of all of this, we could just define a new function.
168
So we use the def keyword to say that this code is defining a new function
169
called turn_around
170
and this function is simply going to be turn left twice. And remember, all the
171
code that's going to go inside the function has to be indented.
172
And now instead of writing, turn left, turn left,
173
we can simplify our code by telling it to call the function,
174
turn_around, and we can do that here and we can do that here.
175
So now when we run the code,
176
you'll see that it does exactly the same thing as before,
177
but this time we've cut down two lines of code.
178
So instead of eight lines of code for our instructions,
179
we only now need six lines.
180
And you can see that the more that you need to use this function,
181
the more typing it will save you. But more important than reducing
182
the number of lines of code is that our code has also become a lot more
183
readable.
184
So the turn_around function is much more clear than having turn left twice.
185
Now let's see what happens when we use the step through button.
186
Look closely at how each line in our code is executed.
187
So now what we're going to carry out the move by one, move by one
188
and now it's going to call turn_around, that function. And it jumps over to find
189
this function and carries out the code inside in turn from the top to the
190
bottom. So turn left, turn left. And now once that function's done,
191
it goes back to where it was before and continues running, move, move,
192
and then turn around again.
193
So here's your challenge.
194
Try and create a separate function that turns our robot to the right.
195
Pause the video,
196
head over to the course resources and click on the link that takes you to
197
Reeborg's World
198
and then go ahead and create your own function called a turn_right.
199
Right.
200
All right.
201
So this should be pretty simple because all we have to do is to define a new
202
function called turn_right
203
and this function is going to simply be turn left three times
204
Right?
205
because turning left three times is going to effectively turn our robot
206
right.
207
So even though we only have this one way of turning our robot,
208
we can define our own functions to give it many,
209
many other commands that it could use.
210
So now using this turn right function,
211
see if you can write some code that makes our robot draw a little square going
212
from here to here then to here and back to the beginning.
213
Once you're done and you hit play, this is what should happen.
214
So pause the video now and see if you can complete that challenge.
215
Right?
216
All right. So let's go from the beginning.
217
We start out facing right at the position 1,1
218
one in order to draw the square, starting from this way,
219
we have to first turn left
220
and then we have to move one step and that should take us to here.
221
And now we have to turn right, move
222
one more step, turn right again,
223
move one more step and finally turn right for the last time and move one more
224
step.
225
So using our awesome function means that we don't have to right out turn left
226
three times each time we need to turn right.
227
So our code can just be eight lines long instead of 14 lines long
228
and we're using our function to cut down on a lot of typing.
229
But not only are there fewer lines of code,
230
look how much more readable our code has become thanks to our turn right
231
function. It's now so much easier to read, turn right,
232
and understand what it needs to do rather than seeing turn left three times in
233
the code.
234
So now it's easier to read and it's easier to follow the logic in our code.
235
In the next lesson,
236
I've got a challenge for you. Head over there and apply what you've learned
237
about functions to try and solve it. I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.