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