WEBVTT

1
00:00:00.278 --> 00:00:02.200
<v ->Hi guys, and welcome back.</v>

2
00:00:02.200 --> 00:00:05.030
In this video, we're going to learn about functions.

3
00:00:05.030 --> 00:00:06.630
We've been using some functions

4
00:00:06.630 --> 00:00:08.900
such as a print and line and sum,

5
00:00:08.900 --> 00:00:11.500
but now we're going to be defining our own.

6
00:00:11.500 --> 00:00:14.590
And that means that we can put some bits of code,

7
00:00:14.590 --> 00:00:16.900
and give them a name, so that we can re-use

8
00:00:16.900 --> 00:00:19.130
those bits of code whenever we want.

9
00:00:19.130 --> 00:00:21.030
Let's learn more about them.

10
00:00:21.030 --> 00:00:22.780
I'm going to create my first function,

11
00:00:22.780 --> 00:00:24.800
and I'm going to call it hello.

12
00:00:24.800 --> 00:00:27.700
So you start off by using the def keyword,

13
00:00:27.700 --> 00:00:29.350
and then you type out the name

14
00:00:29.350 --> 00:00:31.740
of the function you want to define.

15
00:00:31.740 --> 00:00:35.040
Then you put a pair of brackets, and a colon,

16
00:00:35.040 --> 00:00:38.120
and that begins the function body.

17
00:00:38.120 --> 00:00:40.930
Here we're going to put print, hello.

18
00:00:40.930 --> 00:00:45.470
And what happens now is that we have a function created.

19
00:00:45.470 --> 00:00:49.170
When Python reaches a def keyword, it then says, okay,

20
00:00:49.170 --> 00:00:52.700
we've got a hello variable that we are creating.

21
00:00:52.700 --> 00:00:57.010
Only this variable is not a normal value variable

22
00:00:57.010 --> 00:01:01.770
like a string or an integer, this is a callable variable.

23
00:01:01.770 --> 00:01:05.740
That means that it's a variable whose value is a function.

24
00:01:05.740 --> 00:01:08.730
And a function you can call or execute.

25
00:01:08.730 --> 00:01:12.230
Calling, running, or executing a function are all synonyms,

26
00:01:12.230 --> 00:01:14.070
so they mean the same thing.

27
00:01:14.070 --> 00:01:16.760
So when you define a function,

28
00:01:16.760 --> 00:01:19.960
you create a callable variable.

29
00:01:19.960 --> 00:01:22.530
Now creating the function or defining the function

30
00:01:22.530 --> 00:01:24.870
does not run the function body.

31
00:01:24.870 --> 00:01:29.130
It just tells Python that a variable called hello exists.

32
00:01:29.130 --> 00:01:32.810
So if you run this code, nothing actually happens.

33
00:01:32.810 --> 00:01:36.060
As you can see down here, nothing is printed out,

34
00:01:36.060 --> 00:01:38.410
because the function has been defined,

35
00:01:38.410 --> 00:01:41.640
but the function has not been called.

36
00:01:41.640 --> 00:01:43.440
So in order to call the function,

37
00:01:43.440 --> 00:01:46.270
you have to type out the variable name, which is hello,

38
00:01:46.270 --> 00:01:48.250
and then the brackets.

39
00:01:48.250 --> 00:01:51.720
And that, these two brackets after a variable name,

40
00:01:51.720 --> 00:01:55.600
is what actually calls the function and executes it.

41
00:01:55.600 --> 00:01:58.520
The order of running these lines in Python is as follows.

42
00:01:58.520 --> 00:02:00.400
First line one runs.

43
00:02:00.400 --> 00:02:03.180
And it tells Python to create this hello variable

44
00:02:03.180 --> 00:02:05.010
that is a callable.

45
00:02:05.010 --> 00:02:07.333
Then, line two does not run.

46
00:02:08.290 --> 00:02:10.020
Line three runs, but it's empty.

47
00:02:10.020 --> 00:02:11.270
And then line four runs,

48
00:02:11.270 --> 00:02:13.490
which tells Python to execute this function.

49
00:02:13.490 --> 00:02:17.640
So Python jumps back up, and then runs line number two.

50
00:02:17.640 --> 00:02:19.430
Notice that Python knows line number two

51
00:02:19.430 --> 00:02:21.140
is part of the function body,

52
00:02:21.140 --> 00:02:23.220
because of course it is indented,

53
00:02:23.220 --> 00:02:24.940
so it's got these four spaces in front.

54
00:02:24.940 --> 00:02:26.770
You can put more lines in a function,

55
00:02:26.770 --> 00:02:29.510
as long as they are also indented, if you want.

56
00:02:29.510 --> 00:02:31.030
Let's define another function.

57
00:02:31.030 --> 00:02:35.090
Here I've created a callable variable, user age in seconds.

58
00:02:35.090 --> 00:02:36.800
And it is a function.

59
00:02:36.800 --> 00:02:39.190
And now, we can put some code inside of it.

60
00:02:39.190 --> 00:02:40.760
So we're going to implement the first app

61
00:02:40.760 --> 00:02:42.490
that we coded earlier on.

62
00:02:42.490 --> 00:02:46.370
So user age is going be int of input, enter your age.

63
00:02:46.370 --> 00:02:48.750
Here we ask the user for their age,

64
00:02:48.750 --> 00:02:50.860
and then we're going to turn it into seconds,

65
00:02:50.860 --> 00:02:52.340
so that we can print it out.

66
00:02:52.340 --> 00:02:54.720
So age in seconds is going to be user age,

67
00:02:54.720 --> 00:02:58.330
times 365, times 24, times 60 times 60.

68
00:02:58.330 --> 00:03:01.290
That is years, multiplied by days,

69
00:03:01.290 --> 00:03:03.860
hours, minutes, and seconds.

70
00:03:03.860 --> 00:03:05.710
And then we're going to print it out.

71
00:03:07.540 --> 00:03:08.780
Just like that.

72
00:03:08.780 --> 00:03:10.300
Of course, just to reiterate,

73
00:03:10.300 --> 00:03:12.960
if we run this, nothing happens.

74
00:03:12.960 --> 00:03:17.820
You do have to call the function, user age in seconds.

75
00:03:17.820 --> 00:03:21.480
And if you do that, you can actually type down here.

76
00:03:21.480 --> 00:03:23.720
Let me repeat at this point that

77
00:03:23.720 --> 00:03:25.290
sometimes I'm pressing the play button,

78
00:03:25.290 --> 00:03:27.730
and other times I'm running the code in a terminal,

79
00:03:27.730 --> 00:03:30.870
because pressing the play button does not allow you to type.

80
00:03:30.870 --> 00:03:32.480
So you have to run this code in a terminal

81
00:03:32.480 --> 00:03:33.840
if you want to type.

82
00:03:33.840 --> 00:03:35.720
Let's enter the age, such as 30,

83
00:03:35.720 --> 00:03:39.050
and then we get your age in seconds is that.

84
00:03:39.050 --> 00:03:41.240
Now one of the benefits of doing this

85
00:03:41.240 --> 00:03:44.550
is that what you have now are two

86
00:03:44.550 --> 00:03:46.630
reasonably separate bits of code.

87
00:03:46.630 --> 00:03:50.670
One, which is what the code does, and the other one,

88
00:03:50.670 --> 00:03:53.460
which is the code that we'll actually run.

89
00:03:53.460 --> 00:03:57.410
Now this is very much a conceptual separation.

90
00:03:57.410 --> 00:03:59.990
So the code is really just a single file,

91
00:03:59.990 --> 00:04:02.280
which runs line by line and so on.

92
00:04:02.280 --> 00:04:04.260
But conceptually, as a person,

93
00:04:04.260 --> 00:04:06.870
you can think of it as running the code,

94
00:04:06.870 --> 00:04:09.090
and defining the code.

95
00:04:09.090 --> 00:04:12.760
So it can help you understand and read through your code

96
00:04:12.760 --> 00:04:16.946
a bit better if you make that conceptual separation.

97
00:04:16.946 --> 00:04:19.120
For example here we're running the code,

98
00:04:19.120 --> 00:04:21.513
so we could actually add some more things.

99
00:04:24.720 --> 00:04:25.970
Such as that.

100
00:04:25.970 --> 00:04:30.660
Now if we run this again, you get a nice message.

101
00:04:30.660 --> 00:04:32.600
And then you get your goodbye there.

102
00:04:32.600 --> 00:04:33.740
But as I said earlier,

103
00:04:33.740 --> 00:04:37.280
these lines here are just running from top to bottom.

104
00:04:37.280 --> 00:04:41.949
So you can move that line to the top if you want.

105
00:04:41.949 --> 00:04:45.550
And you can put this line anywhere you like.

106
00:04:45.550 --> 00:04:49.620
And Python will still print out the exact same stuff.

107
00:04:49.620 --> 00:04:51.070
Welcome to the age in seconds programme

108
00:04:51.070 --> 00:04:53.210
will be printed out first.

109
00:04:53.210 --> 00:04:55.760
Then the user age in seconds will be printed out next,

110
00:04:55.760 --> 00:04:57.510
which runs through this function,

111
00:04:57.510 --> 00:05:00.060
and finally goodbye will be printed out after.

112
00:05:00.060 --> 00:05:05.060
However, by treating these two as slightly separated

113
00:05:05.130 --> 00:05:07.720
parts of code, it can actually be much easier

114
00:05:07.720 --> 00:05:10.110
to reason about, much easier to read,

115
00:05:10.110 --> 00:05:11.870
and much easier to think about.

116
00:05:11.870 --> 00:05:15.063
So that is just a tip there from me to you.

117
00:05:17.640 --> 00:05:19.590
A couple of things that you should not do

118
00:05:19.590 --> 00:05:23.390
when you're working with functions is re-use names.

119
00:05:23.390 --> 00:05:26.060
So if you define a function called print,

120
00:05:26.060 --> 00:05:28.440
and then you try to print hello world,

121
00:05:28.440 --> 00:05:30.530
you're going to get a problem.

122
00:05:30.530 --> 00:05:33.280
Because when you define the print function,

123
00:05:33.280 --> 00:05:36.080
that creates this print variable that you can use,

124
00:05:36.080 --> 00:05:38.110
and then you're actually using it, here.

125
00:05:38.110 --> 00:05:41.650
This is no longer the Python built in print function

126
00:05:41.650 --> 00:05:45.130
that we're calling, this is calling your own function.

127
00:05:45.130 --> 00:05:46.880
And this is almost never what you want,

128
00:05:46.880 --> 00:05:49.023
although it is valid Python code,

129
00:05:49.023 --> 00:05:51.320
it's almost never what you want.

130
00:05:51.320 --> 00:05:54.200
And another reason why you get an error by doing this,

131
00:05:54.200 --> 00:05:57.830
is because you're putting in a string inside the brackets,

132
00:05:57.830 --> 00:06:00.640
but this function cannot take in a string.

133
00:06:00.640 --> 00:06:03.210
We're going to learn how to do that in a future video.

134
00:06:03.210 --> 00:06:06.720
So if you run that, you're going to get an error.

135
00:06:06.720 --> 00:06:09.330
It says, print takes zero positional arguments,

136
00:06:09.330 --> 00:06:10.950
but one was given.

137
00:06:10.950 --> 00:06:12.720
That means that we've put a string in there,

138
00:06:12.720 --> 00:06:15.210
but our function does not accept a string.

139
00:06:15.210 --> 00:06:16.810
Another thing that you may want to avoid,

140
00:06:16.810 --> 00:06:20.350
is imagine you've got a list of your friend names,

141
00:06:20.350 --> 00:06:22.993
and then you have a function that adds a friend.

142
00:06:23.900 --> 00:06:27.253
Then you take in the friend name from the user's input.

143
00:06:30.210 --> 00:06:31.360
And then here's something

144
00:06:31.360 --> 00:06:33.640
that you almost definitely want to avoid,

145
00:06:33.640 --> 00:06:38.560
and that is to say friends equal friends plus friend name.

146
00:06:38.560 --> 00:06:39.480
What's happening here

147
00:06:39.480 --> 00:06:43.990
is you're re-using the friends variable

148
00:06:43.990 --> 00:06:45.930
in two different places.

149
00:06:45.930 --> 00:06:48.962
And because you are inside a function,

150
00:06:48.962 --> 00:06:52.080
Python actually creates a new space for variables

151
00:06:52.080 --> 00:06:54.410
that is separate from the global space,

152
00:06:54.410 --> 00:06:57.450
so functions have their own little space for your variables.

153
00:06:57.450 --> 00:06:59.920
And when you do friends equal,

154
00:06:59.920 --> 00:07:02.693
Python is creating a new variable called friends

155
00:07:02.693 --> 00:07:06.430
that only exists inside the function.

156
00:07:06.430 --> 00:07:08.620
The same thing with friend name.

157
00:07:08.620 --> 00:07:10.750
So, you now have two variables.

158
00:07:10.750 --> 00:07:13.690
Friends, in the global scope of the file,

159
00:07:13.690 --> 00:07:15.880
and friends in the function scope

160
00:07:15.880 --> 00:07:17.980
of this add friend function.

161
00:07:17.980 --> 00:07:20.510
This is almost never something you want to do.

162
00:07:20.510 --> 00:07:25.095
Because even though you are using a variable called friends,

163
00:07:25.095 --> 00:07:27.880
you are also defining a new variable.

164
00:07:27.880 --> 00:07:31.820
So let me run this here.

165
00:07:31.820 --> 00:07:35.493
So let me run that, and you can see that if I enter Rolf,

166
00:07:36.540 --> 00:07:38.210
you get your error here.

167
00:07:38.210 --> 00:07:41.787
And what's happening is that this variable exists.

168
00:07:41.787 --> 00:07:45.640
And if you call this something like F,

169
00:07:45.640 --> 00:07:47.180
then this code is fine.

170
00:07:47.180 --> 00:07:49.770
Because Python will see the friends variable.

171
00:07:49.770 --> 00:07:51.950
It will check whether it exists inside the function,

172
00:07:51.950 --> 00:07:55.500
but it doesn't, so it uses the one outside the function.

173
00:07:55.500 --> 00:07:56.620
So that's fine.

174
00:07:56.620 --> 00:07:57.453
But as soon as you do

175
00:07:57.453 --> 00:07:59.637
friends equal friends plus friend name,

176
00:07:59.637 --> 00:08:03.068
Python is going to look at this friends variable,

177
00:08:03.068 --> 00:08:07.510
and notice that it's being defined inside the function.

178
00:08:07.510 --> 00:08:11.280
And you can't do that, because you can't use a variable

179
00:08:11.280 --> 00:08:13.070
in the same line where you're defining it,

180
00:08:13.070 --> 00:08:14.690
it doesn't makes sense.

181
00:08:14.690 --> 00:08:16.460
So it will give you this error.

182
00:08:16.460 --> 00:08:19.630
It's akin to doing X equal X plus five,

183
00:08:19.630 --> 00:08:21.340
but X doesn't exist.

184
00:08:21.340 --> 00:08:23.940
So Python will also be very confused if you do that.

185
00:08:24.890 --> 00:08:27.510
Alright, so almost never will you want

186
00:08:27.510 --> 00:08:31.170
to shadow a variable name from an outer scope.

187
00:08:31.170 --> 00:08:33.670
That is a technical definition for what we're doing here.

188
00:08:33.670 --> 00:08:36.600
We are shadowing the friends variable

189
00:08:36.600 --> 00:08:39.790
by redefining it inside a function

190
00:08:39.790 --> 00:08:42.880
where it already exists on the outside.

191
00:08:42.880 --> 00:08:45.420
Again this is valid Python code.

192
00:08:45.420 --> 00:08:46.330
Of course this is not,

193
00:08:46.330 --> 00:08:47.620
because you're using the variable

194
00:08:47.620 --> 00:08:48.980
in the same line you're defining it.

195
00:08:48.980 --> 00:08:52.233
But generally, shadowing a variable is valid Python code.

196
00:08:52.233 --> 00:08:55.283
It's just something that you usually don't want to do.

197
00:08:56.860 --> 00:08:58.510
Something else you can't do with functions

198
00:08:58.510 --> 00:09:00.840
is use them before they get defined.

199
00:09:00.840 --> 00:09:03.850
Maybe this is obvious, but here we're calling the function,

200
00:09:03.850 --> 00:09:05.430
say hello, and then we're defining it

201
00:09:05.430 --> 00:09:06.670
down here in line four.

202
00:09:06.670 --> 00:09:09.200
Python won't let you do this, if you try to do that,

203
00:09:09.200 --> 00:09:10.600
you get an error saying

204
00:09:10.600 --> 00:09:13.263
that the say hello name is not defined.

205
00:09:14.470 --> 00:09:16.230
Here we've got a final example

206
00:09:16.230 --> 00:09:18.250
where we have an add friend function

207
00:09:18.250 --> 00:09:21.330
that takes in a global variable, friends,

208
00:09:21.330 --> 00:09:24.290
and appends Rolf to it.

209
00:09:24.290 --> 00:09:27.940
Then we define the friends variable in line five

210
00:09:27.940 --> 00:09:29.640
after the function's been created,

211
00:09:29.640 --> 00:09:31.640
then we call the function add friend,

212
00:09:31.640 --> 00:09:33.490
and finally we print friends.

213
00:09:33.490 --> 00:09:37.790
So this may look like an error, because here in line two,

214
00:09:37.790 --> 00:09:40.780
the friends variable has not yet been defined.

215
00:09:40.780 --> 00:09:44.010
But actually remember that this line of code only runs

216
00:09:44.010 --> 00:09:45.640
when line six runs.

217
00:09:45.640 --> 00:09:46.690
So this is fine.

218
00:09:46.690 --> 00:09:48.090
Although a bit confusing.

219
00:09:48.090 --> 00:09:50.250
The friends variable that this uses

220
00:09:50.250 --> 00:09:53.280
will be defined by the time this line is ran.

221
00:09:53.280 --> 00:09:55.570
So that's why this will work.

222
00:09:55.570 --> 00:09:58.690
Although it will be quite confusing as I said earlier.

223
00:09:58.690 --> 00:10:02.250
Usually you'll want to put your global variables at the top,

224
00:10:02.250 --> 00:10:04.750
so that it's a bit clearer what's going on.

225
00:10:04.750 --> 00:10:09.750
Also this line here is modifying the friends list.

226
00:10:09.980 --> 00:10:13.190
So if you call this function multiple times,

227
00:10:13.190 --> 00:10:16.540
it's going to append Rolf to this list multiple times.

228
00:10:16.540 --> 00:10:19.490
So that's something interesting about this function here,

229
00:10:19.490 --> 00:10:22.230
that you are appending to this list,

230
00:10:22.230 --> 00:10:24.210
so you'll end up with multiple elements,

231
00:10:24.210 --> 00:10:26.370
if you run the function multiple times.

232
00:10:26.370 --> 00:10:28.020
Alright that's everything for this video,

233
00:10:28.020 --> 00:10:29.910
I just wanted to show you

234
00:10:29.910 --> 00:10:32.760
what functions look like in Python, how you define them,

235
00:10:32.760 --> 00:10:35.607
how you use them, and some things that you don't want to do

236
00:10:35.607 --> 00:10:37.950
when you're working with functions.

237
00:10:37.950 --> 00:10:39.300
Thanks for joining me in this one,

238
00:10:39.300 --> 00:10:40.950
and I'll see you in the next one.

