WEBVTT

0
00:00.350 --> 00:05.810
The first concept I want to introduce you to is something called functions.

1
00:05.810 --> 00:12.710
And just as we perform many different functions could be mother, father, son, daughter,

2
00:12.710 --> 00:17.660
we can also get our code to perform different pieces of functionality.

3
00:18.110 --> 00:25.580
Now if we think back we've actually already come across functions, and I've been referring to them

4
00:25.580 --> 00:27.920
every time we've needed to use one.

5
00:28.220 --> 00:34.960
If you take a look at this link in the course resources, which takes you to the Python documentation,

6
00:34.960 --> 00:40.270
then you can see that Python has a whole bunch of built in functions that we've been using.

7
00:40.270 --> 00:47.740
For example, the len() function, which gives us the number of items in a collection, or the int() function,

8
00:47.740 --> 00:54.540
which turns something into an integer, or the print() function, the range() function, and other ones

9
00:54.540 --> 00:56.010
that we haven't seen yet.

10
00:56.640 --> 01:02.550
We can start typing one of the functions that we've been using a lot, which is the print function.

11
01:02.550 --> 01:09.270
And the reason why we know it's a function is because it's the name of a function followed by a set

12
01:09.270 --> 01:10.650
of parentheses.

13
01:10.650 --> 01:17.300
Now we know that if we put something inside these parentheses say for example ("Hello"),

14
01:17.300 --> 01:24.410
then when I run the code, whatever is inside those parentheses will get outputted over here in the

15
01:24.410 --> 01:25.220
console.

16
01:25.940 --> 01:29.720
Now all the functions work pretty much the same.

17
01:29.720 --> 01:36.320
So if I wanted to use the len() function to see, well, how many characters are there in this word

18
01:36.320 --> 01:43.510
"Hello", I again put my hello inside the parentheses and because of the parentheses, we know that this

19
01:43.510 --> 01:44.530
is a function.

20
01:44.530 --> 01:51.550
And then I can save the outcome of this length function to a variable.

21
01:51.550 --> 01:54.610
Say let's call it number of characters, num_char.

22
01:54.820 --> 01:59.650
And then I can go ahead and print it in order to view it inside the console.

23
01:59.650 --> 02:03.310
So let's print the num_char,

24
02:03.640 --> 02:09.120
and now you can see that this len() function gets to work, calculates the number of characters,

25
02:09.120 --> 02:11.460
it's 5, and then it gets printed.

26
02:11.460 --> 02:19.410
So all of this functionality has been achieved by these built-in functions from Python like print() and

27
02:19.410 --> 02:19.980
len().

28
02:20.940 --> 02:24.750
Now what if we wanted to make our own functions?

29
02:24.750 --> 02:26.610
How would we do that?

30
02:27.270 --> 02:35.600
Well, if we want to make our own function, we first start out with a keyword which is called, def,

31
02:35.600 --> 02:40.700
and this is because we're creating or defining our function.

32
02:41.360 --> 02:45.500
Now after the def keyword we can give our function a name.

33
02:45.500 --> 02:48.890
And I'm just going to call it my_function.

34
02:49.370 --> 02:55.260
Now the thing that differentiates a function from a variable however, is the parentheses.

35
02:55.260 --> 02:58.650
So after the name comes the parentheses.

36
02:58.650 --> 03:00.540
And now the final thing,

37
03:00.540 --> 03:07.080
the finishing touch to our function definition is a colon because that says everything that comes

38
03:07.080 --> 03:12.360
after that line and is indented belongs with the function.

39
03:12.870 --> 03:16.740
Let's make a really simple function that just has two lines of code.

40
03:16.740 --> 03:20.570
Maybe It prints "Hello", and then it prints.

41
03:20.960 --> 03:21.680
"Bye".

42
03:22.100 --> 03:25.040
So now I've created my_function.

43
03:25.040 --> 03:31.700
I've placed the lines of code that are associated with this function, all indented after the definition,

44
03:31.700 --> 03:37.820
but notice how if I go ahead and run my code, actually nothing will happen.

45
03:37.820 --> 03:42.640
And the reason is because we haven't yet executed the function.

46
03:42.730 --> 03:49.540
Now we can define lots and lots of functions ahead of time, and it's only when we actually need it

47
03:49.540 --> 03:51.730
do we go ahead and trigger it.

48
03:51.730 --> 03:57.310
And to trigger it, or in programming lingo, we would say to call the function,

49
03:57.310 --> 04:05.250
all we have to do is type the name of the function, which is my_function, and then to add the parentheses

50
04:05.250 --> 04:07.350
and any necessary inputs.

51
04:07.350 --> 04:14.160
In our case, our function doesn't require any inputs, so we can leave the parentheses blank again.

52
04:14.160 --> 04:21.120
And now if I go ahead and run my code once more, then you'll see that by the time the computer reaches

53
04:21.120 --> 04:24.780
Line 5, it's going to search for this thing called my function.

54
04:24.780 --> 04:31.200
It sees that it was defined right here on Line 1, and it goes through all of the content and executes

55
04:31.200 --> 04:33.690
them in turn, line by line.

56
04:33.690 --> 04:37.530
And that's why we get, Hello and Bye being printed here.

57
04:38.580 --> 04:43.350
So just to recap, this is how we create a function in Python.

58
04:43.350 --> 04:45.030
There's two steps to it.

59
04:45.030 --> 04:51.120
The first step is to actually define the function to specify what it should do,

60
04:51.120 --> 04:54.640
and we do that by first using the def keyword.

61
04:54.640 --> 04:57.400
And then we give our function a name,

62
04:57.400 --> 05:00.340
so for example in this case I called it my_function.

63
05:00.340 --> 05:04.450
And then comes a set of parentheses and a colon.

64
05:04.780 --> 05:10.570
And then after that we get to put the lines of code which will be included in this function.

65
05:10.570 --> 05:16.240
And remember that these lines of code which goes into the function which will be carried out when this

66
05:16.240 --> 05:20.370
function is triggered, must be indented, like this.

67
05:21.000 --> 05:24.810
So once you've defined the function, you've created the recipe,

68
05:24.810 --> 05:32.130
the next step is to actually use it, which in programming lingo we would say calling the function. And

69
05:32.130 --> 05:37.770
we call the function just by specifying the name and a set of parentheses.

70
05:37.770 --> 05:44.990
And once the computer sees this line of code, it'll know to go and carry out all the instructions inside

71
05:44.990 --> 05:46.730
where we defined our function.

72
05:47.360 --> 05:53.060
And to learn about functions, I'm going to introduce you to something that's very similar to Karel

73
05:53.060 --> 05:53.810
the Robot.

74
05:54.140 --> 06:01.430
The robot is going to be something that is just going to perform the tasks that we want it to, and

75
06:01.430 --> 06:03.170
no more and no less.

76
06:03.170 --> 06:11.680
So, for example, if we wanted a robot to go to the store and pick up some milk for us, we can't simply

77
06:11.680 --> 06:18.370
just tell it, "Oh yeah, just go and buy some milk," because it won't know how to do that unless we give

78
06:18.370 --> 06:25.540
it specific instructions such as leave the house, walk two blocks to the right, walk four blocks back,

79
06:25.540 --> 06:27.130
and then two blocks to the right.

80
06:27.130 --> 06:32.280
When you go to the store, give them some money and then take the milk and come back.

81
06:32.310 --> 06:35.430
We have to program each and every step.

82
06:35.430 --> 06:42.630
But say if this functionality of getting the robot to go and pick up some milk for us is needed every

83
06:42.630 --> 06:49.680
single day, then we have to write out all of those instructions day after day, day after day.

84
06:49.680 --> 06:52.740
And at some point our fingers are gonna hurt, right?

85
06:52.740 --> 06:58.850
We're typing so much code that's repeated because we don't have a way of bundling all those instructions

86
06:58.850 --> 06:59.510
together.

87
06:59.510 --> 07:01.460
And that's where functions come in.

88
07:01.460 --> 07:06.860
Functions will give us a way of referring to all those instructions at the same time.

89
07:06.860 --> 07:12.860
That way we can give our robot a single instruction, and it will carry out all of those little steps

90
07:12.860 --> 07:15.050
for us and get us some milk.

91
07:15.740 --> 07:22.870
Now to try this out yourself, I want you to head over to the Course Resources and click on the link

92
07:22.870 --> 07:25.390
that takes you to Reeborg's World.

93
07:25.390 --> 07:32.080
Now Reeborg's World is very similar to Karel the Robot that I showed you earlier on, but it allows

94
07:32.080 --> 07:36.700
us to write Python code and it's very easy to get started.

95
07:36.820 --> 07:41.400
Now we've got this area here which we're going to use to write our Python code,

96
07:41.490 --> 07:48.480
and then once we're done, we can click on play to run the Python code and see the instructions carried

97
07:48.480 --> 07:50.280
out by our Robot.

98
07:50.400 --> 07:57.210
Now if you click on Reebok's keyboard, you can see all of the functions that we can use to command

99
07:57.210 --> 08:02.520
our robot like move() or turn_left(), or build_wall, etc..

100
08:03.480 --> 08:06.730
Let's see if we go ahead and run this line of code here,

101
08:06.730 --> 08:07.780
move(),

102
08:08.380 --> 08:10.930
what happens? When I click Play▶️,

103
08:10.930 --> 08:17.080
you can see that our robot moves forward in the direction that it's facing by one step.

104
08:17.200 --> 08:23.740
So if we wanted it to move say for example three steps, all we have to do is to call the move() function

105
08:23.740 --> 08:24.880
three times.

106
08:25.030 --> 08:30.870
Now let's reset by clicking the Return button to return our robot back to the starting position.

107
08:30.900 --> 08:36.360
Now let's click Play again in order for our robot to carry out the new instructions.

108
08:36.960 --> 08:43.590
Notice how all the instructions were carried out very quickly. To execute each instruction, one step

109
08:43.590 --> 08:45.300
at a time, line by line,

110
08:45.300 --> 08:47.820
we can use this step-through button ⏯️.

111
08:47.820 --> 08:55.790
This button allows us to see how each instruction is executed and after each instruction, it pauses

112
08:55.790 --> 08:57.980
and highlights the next instruction.

113
08:58.010 --> 09:00.110
This is very helpful for debugging.

114
09:00.680 --> 09:03.620
So notice how we're highlighting Line 2.

115
09:03.650 --> 09:07.820
So if I click on step again it's going to carry out that function,

116
09:07.820 --> 09:10.250
and then it's going to carry out Line 3,

117
09:10.250 --> 09:12.920
and finally it reaches the end.

118
09:12.920 --> 09:16.220
And that's the end of our code.

119
09:17.560 --> 09:22.360
Now, in addition, we can use some of the other commands in the keyboard,

120
09:22.360 --> 09:24.550
for example, turn_left().

121
09:24.730 --> 09:31.690
If I decide to say move() three times, and then turn_left(), and then maybe move() three times again,

122
09:31.720 --> 09:35.320
can you predict which square the robot is going to end up in?

123
09:36.580 --> 09:37.480
Are you ready?

124
09:37.510 --> 09:39.340
Let's go ahead and run the code.

125
09:40.870 --> 09:41.730
There we go.

126
09:41.760 --> 09:44.370
We end up on the square 4, 4.

127
09:44.400 --> 09:48.660
It moves() three forwards, turns_left(), and then moves() three steps again,

128
09:48.660 --> 09:51.900
because that's what we told it to do with our Python code.

129
09:52.290 --> 09:54.570
So now here's a question,

130
09:54.570 --> 09:59.760
if you take a look inside this set of commands, you'll notice that there is move(), and there is turn_left(),

131
09:59.760 --> 10:04.140
but there is no turn_right(), and there is no turn_around.

132
10:04.140 --> 10:09.410
So how could we use a function to create those commands?

133
10:09.410 --> 10:17.690
Because of course, we could just simply write turn_left(), and then turn_left() again.

134
10:19.280 --> 10:24.200
And this would effectively turn the robot around, right?

135
10:24.380 --> 10:30.340
But if we wanted to do that many times, say for example, if we wanted to go to 3, turn around,

136
10:30.340 --> 10:33.130
go back to 1, turn around, go back to 3,

137
10:33.130 --> 10:36.760
then we would have to write code that looks a bit like this.

138
10:36.760 --> 10:45.520
We would have to say move() two times to get to square 3, turn_left(), turn_left(), and then move two

139
10:45.520 --> 10:49.540
times again to get to 1 and then turn_left(),

140
10:49.540 --> 10:52.300
turn_left() again in order to turn around.

141
10:52.600 --> 10:58.350
This is all the code we would have to write to achieve this kind of effect.

142
10:59.040 --> 11:04.080
But instead of all of this, we could just define a new function.

143
11:04.080 --> 11:10.440
So we use the def keyword to say that this code is defining a new function called turn-around().

144
11:11.460 --> 11:19.700
And this function is simply going to be turn_left() twice. And remember all the code that's going to go

145
11:19.700 --> 11:20.150
inside

146
11:20.150 --> 11:22.370
the function has to be indented.

147
11:22.400 --> 11:25.850
And now instead of writing turn_left(), turn_left(),

148
11:25.850 --> 11:32.000
we can simplify our code by telling it to call the function turn_around().

149
11:32.540 --> 11:34.220
And we can do that here.

150
11:34.220 --> 11:35.690
And we can do that here.

151
11:35.840 --> 11:41.590
So now when we run the code you'll see that it does exactly the same thing as before.

152
11:41.590 --> 11:45.250
But this time we've cut down two lines of code.

153
11:45.460 --> 11:51.490
So instead of eight lines of code for our instructions, we only now need six lines.

154
11:51.490 --> 11:57.910
And you can see that the more that you need to use this function, the more typing it will save you.

155
11:58.210 --> 12:05.080
But more important than reducing the number of lines of code is that our code has also become a lot

156
12:05.080 --> 12:12.420
more readable, so the turn_around() function is much more clear than having turn_left() twice.

157
12:12.420 --> 12:15.720
Now let's see what happens when we use the step through button.

158
12:15.720 --> 12:19.410
Look closely at how each line in our code is executed.

159
12:20.040 --> 12:24.900
So now we're going to carry out the move() by one, move() by one.

160
12:24.900 --> 12:28.320
And now it's going to call turn_around(), that function.

161
12:28.320 --> 12:35.000
And it jumps over to find this function and carries out the code inside in turn, from the top to the

162
12:35.000 --> 12:35.360
bottom.

163
12:35.360 --> 12:37.250
So turn_left(), turn_left().

164
12:37.250 --> 12:42.650
And now once that function is done, it goes back to where it was before and continues running,

165
12:42.650 --> 12:46.340
move(), move(), and then turn_around() again.

166
12:47.480 --> 12:49.790
So here's your challenge,

167
12:49.790 --> 12:55.240
try and create a separate function that turns our robot to the right.

168
12:55.480 --> 13:02.020
Pause the video, head over to the Course Resources and click on the link that takes you to Reeborg's

169
13:02.020 --> 13:02.590
World.

170
13:02.590 --> 13:07.090
And then go ahead and create your own function called turn_right().

171
13:09.040 --> 13:15.370
All right, so this should be pretty simple because all we have to do is to define a new function called

172
13:15.370 --> 13:16.390
turn_right().

173
13:16.570 --> 13:22.040
And this function is going to simply be turn_left() three times.

174
13:25.790 --> 13:31.070
Because turning left three times is going to effectively turn our robot, right.

175
13:31.070 --> 13:39.440
So even though we only have this one way of turning our robot, we can define our own functions to give

176
13:39.440 --> 13:43.000
it many, many other commands that it could use.

177
13:43.000 --> 13:51.850
So now using this turn_right function(), see if you can write some code that makes our robot draw a little

178
13:51.850 --> 13:57.040
square going from here to here, then to here and back to the beginning.

179
13:57.520 --> 14:02.320
Once you're done and you hit play, this is what should happen.

180
14:04.480 --> 14:08.280
So pause the video now and see if you can complete that challenge.

181
14:12.690 --> 14:15.600
All right so let's go from the beginning.

182
14:15.600 --> 14:19.800
We start out facing right at the position 1, 1.

183
14:19.860 --> 14:25.140
In order to draw the square starting from this way we have to first turn_left(),

184
14:26.220 --> 14:28.680
and then we have to move() one step.

185
14:28.680 --> 14:30.860
And that should take us to here.

186
14:31.010 --> 14:33.350
And now we have to turn_right(),

187
14:35.540 --> 14:37.400
move() one more step,

188
14:37.640 --> 14:48.740
turn_right() again, move() one more step, and finally turn_right() for the last time and move() one more step.

189
14:49.910 --> 14:56.380
So using our awesome function means that we don't have to write out turn_left() three times,

190
14:56.380 --> 15:04.360
each time we need to turn_right(). So our code can just be eight lines long instead of 14 lines long.

191
15:04.360 --> 15:08.680
And we're using our function to cut down on a lot of typing.

192
15:09.100 --> 15:15.340
But not only are there fewer lines of code, look at how much more readable our code has become thanks

193
15:15.340 --> 15:17.290
to our turn_right() function.

194
15:17.670 --> 15:24.780
It's now so much easier to read, turn_right(), and understand what it needs to do rather than seeing turn_left()

195
15:24.780 --> 15:26.820
three times in the code.

196
15:27.390 --> 15:32.370
So now it's easier to read and it's easier to follow the logic in our code.

197
15:32.730 --> 15:36.390
In the next lesson, I've got a challenge for you.

198
15:36.420 --> 15:41.250
Head over there and apply what you've learned about functions to try and solve it.

199
15:41.670 --> 15:42.630
I'll see you there.