1
00:00:01,589 --> 00:00:07,860
Hey, in this video, you will learn one of the most important elements of Python, and that is custom

2
00:00:07,860 --> 00:00:08,700
functions.

3
00:00:10,220 --> 00:00:14,840
So let's look at our code and see how custom functions.

4
00:00:16,260 --> 00:00:25,230
Will improve our program because currently we have a problem with the program and the problem is redundancy.

5
00:00:25,950 --> 00:00:28,320
So we have repetitive codes.

6
00:00:29,410 --> 00:00:30,910
In our code base.

7
00:00:34,890 --> 00:00:39,270
These two lines here are repeated in here.

8
00:00:41,610 --> 00:00:43,080
And here as well.

9
00:00:45,440 --> 00:00:46,470
And here too.

10
00:00:46,490 --> 00:00:49,370
So four times we have the same code.

11
00:00:51,260 --> 00:00:56,870
There certainly should be a better way not to have to write repetitive code.

12
00:00:57,320 --> 00:00:59,480
And the answer is custom.

13
00:00:59,480 --> 00:01:00,380
Some functions.

14
00:01:01,390 --> 00:01:10,420
So now I'll go ahead and create a custom function and you'll see how a custom function becomes beneficial

15
00:01:11,080 --> 00:01:13,480
in avoiding redundant code.

16
00:01:15,680 --> 00:01:21,770
Custom functions are usually created on top of your code, so outside of the loop.

17
00:01:23,180 --> 00:01:32,480
So it costs and function is like a slot where you put some codes and then you just call that custom

18
00:01:32,480 --> 00:01:33,320
function.

19
00:01:35,240 --> 00:01:38,330
So the slot must have a name.

20
00:01:38,450 --> 00:01:41,870
So let's call this get to dos.

21
00:01:43,070 --> 00:01:44,340
And that's the syntax.

22
00:01:44,360 --> 00:01:45,220
That's how you start.

23
00:01:45,230 --> 00:01:51,380
So you start with the def keyword and then you specify a name, whatever you like.

24
00:01:52,400 --> 00:01:59,330
The rules of naming it are the same as naming variables, so you shouldn't start with a number.

25
00:02:01,050 --> 00:02:03,180
You shouldn't have spaces in the name.

26
00:02:03,180 --> 00:02:05,550
You can have underscores.

27
00:02:06,410 --> 00:02:07,310
That's fine.

28
00:02:10,690 --> 00:02:12,160
So that's the first line.

29
00:02:12,160 --> 00:02:15,670
In the next line, then you put the code.

30
00:02:17,350 --> 00:02:20,410
So in our case, it would be these two lines.

31
00:02:20,470 --> 00:02:25,200
So I'm going to copy them and paste them in here.

32
00:02:25,210 --> 00:02:28,750
But we need indentation.

33
00:02:28,750 --> 00:02:30,640
So here you want to paste.

34
00:02:31,510 --> 00:02:32,080
That's code.

35
00:02:32,080 --> 00:02:35,620
So make sure it's for spaces.

36
00:02:35,620 --> 00:02:37,840
For spaces is the recommendation.

37
00:02:39,460 --> 00:02:41,020
So I have four spaces here.

38
00:02:41,050 --> 00:02:43,760
One, two, three, four and eight here.

39
00:02:43,780 --> 00:02:45,010
One, two, three, four.

40
00:02:45,940 --> 00:02:47,290
One, two, three, four.

41
00:02:50,230 --> 00:02:57,010
So this indentation here is because we have this width context manager and this indentation here is

42
00:02:57,010 --> 00:02:58,480
part of the function.

43
00:02:59,950 --> 00:03:00,830
Make some space.

44
00:03:00,850 --> 00:03:06,220
It is recommended to have two spaces between a function and other parts of the code.

45
00:03:06,850 --> 00:03:08,440
So two break lines.

46
00:03:09,300 --> 00:03:12,320
So that's the code that is repetitive.

47
00:03:12,330 --> 00:03:20,300
We put it in a function and then we want to add another line at the end which starts with return.

48
00:03:20,310 --> 00:03:24,230
So return is a statement just like we have other keywords.

49
00:03:24,230 --> 00:03:25,740
So this is also a keywords.

50
00:03:25,830 --> 00:03:28,470
So a return to dos.

51
00:03:30,690 --> 00:03:33,930
So that there is the function definition.

52
00:03:35,820 --> 00:03:44,370
Once you have defined the function, then you can remove these lines and replace them with the function

53
00:03:44,370 --> 00:03:45,150
call.

54
00:03:50,330 --> 00:03:54,380
So that's like the other functions we have been working on.

55
00:03:54,380 --> 00:03:58,520
Like you see open input print.

56
00:04:00,670 --> 00:04:03,220
So this also has the same syntax.

57
00:04:03,460 --> 00:04:05,340
Now it doesn't get any argument.

58
00:04:05,350 --> 00:04:08,830
You'll see how to create functions, which also get arguments.

59
00:04:08,980 --> 00:04:13,150
For this lecture, let's focus on functions which don't get any arguments.

60
00:04:13,150 --> 00:04:14,460
So this gets to do.

61
00:04:14,620 --> 00:04:16,720
Function will return something.

62
00:04:16,720 --> 00:04:17,230
Right.

63
00:04:17,620 --> 00:04:20,230
That is the list.

64
00:04:24,000 --> 00:04:27,390
Now we need to store that into a variable.

65
00:04:28,590 --> 00:04:31,890
The variable is normally this one to dos.

66
00:04:32,310 --> 00:04:41,100
So we store that list in that variable there and then we append to dos to that variable.

67
00:04:44,560 --> 00:04:48,310
And we can do the same in the other parts of the code.

68
00:04:48,310 --> 00:04:52,780
So to do is equal to get to deuce.

69
00:04:55,100 --> 00:05:01,020
So whatever you have this function in reads mode, write, don't confuse it with write mode.

70
00:05:01,040 --> 00:05:02,690
We will work on that later.

71
00:05:02,930 --> 00:05:05,540
That's not the same code, correct?

72
00:05:05,570 --> 00:05:07,310
We should be clear about that.

73
00:05:07,850 --> 00:05:16,430
So here is another open function with the read modes to do is equal to get to dos with parentheses.

74
00:05:16,460 --> 00:05:20,240
Don't forget the parentheses because that is a function called.

75
00:05:24,130 --> 00:05:30,050
And the last one to do so is equal to get to deuce.

76
00:05:33,900 --> 00:05:34,920
And that's it.

77
00:05:38,500 --> 00:05:41,800
So let's run the codes to make sure it's working.

78
00:05:44,190 --> 00:05:45,960
Let's add say to do

79
00:05:49,500 --> 00:05:50,610
make soup

80
00:05:53,430 --> 00:05:58,680
make pizza, enter show and everything seems to be working fine.

81
00:06:01,520 --> 00:06:08,960
So we tried the show condition where we have this function and everything is working fine.

82
00:06:08,960 --> 00:06:09,530
So.

83
00:06:11,770 --> 00:06:18,820
You know, that we had these built in functions in Python, such as open and input.

84
00:06:19,240 --> 00:06:26,130
So I've been referring to these as functions, but these are actually function calls.

85
00:06:26,140 --> 00:06:28,070
So here we call the function.

86
00:06:28,090 --> 00:06:31,810
The function itself is written somewhere else.

87
00:06:33,170 --> 00:06:36,290
So just like here, this is not the function.

88
00:06:36,290 --> 00:06:37,940
This is the function call.

89
00:06:38,060 --> 00:06:40,970
The function itself is written in here.

90
00:06:41,090 --> 00:06:44,390
So that's the instructions on what this function does.

91
00:06:44,420 --> 00:06:47,390
Now, where are these functions written?

92
00:06:47,420 --> 00:06:55,970
Well, if you are on patch or you can right click here and you can go to go to and go to implementations.

93
00:06:55,970 --> 00:06:59,700
If you are in another ID, then just watch the video.

94
00:06:59,720 --> 00:07:02,030
You don't have to perform the same.

95
00:07:02,030 --> 00:07:04,340
So just see what I'll explain here.

96
00:07:04,790 --> 00:07:10,880
So by going there, this will take you to the function definition for the input function.

97
00:07:11,480 --> 00:07:18,260
So again, we can try another function such as open right click go to.

98
00:07:19,880 --> 00:07:21,290
Implementations.

99
00:07:21,440 --> 00:07:24,710
It takes you again to the definition of the open function.

100
00:07:26,570 --> 00:07:32,770
If I press this asterisk, it will expand the definition.

101
00:07:32,780 --> 00:07:35,420
So this is just comments.

102
00:07:35,420 --> 00:07:36,410
This is not code.

103
00:07:36,410 --> 00:07:43,850
It's the description of the function, which is actually what you get when you do help open in the command

104
00:07:43,850 --> 00:07:44,300
line.

105
00:07:44,300 --> 00:07:51,560
So if you do help open, you actually see the same description

106
00:07:54,050 --> 00:07:56,690
and that is referred to as a docstring.

107
00:07:57,050 --> 00:07:59,630
So it's the documentation of the function.

108
00:08:02,480 --> 00:08:11,210
And then down here you have paths so you don't actually see the code here, you only see this docstring

109
00:08:11,210 --> 00:08:11,930
and.

110
00:08:14,030 --> 00:08:14,820
Pass.

111
00:08:14,840 --> 00:08:24,680
And that is because all these functions so open and input, these were written in C language.

112
00:08:25,610 --> 00:08:27,890
So C is the language.

113
00:08:29,030 --> 00:08:32,030
Used to ride the interpreter of Python.

114
00:08:32,299 --> 00:08:35,720
Remember when we installed the interpreter from Python dot org?

115
00:08:37,350 --> 00:08:43,650
That is a program which interprets Python language and that program is written in C language.

116
00:08:43,650 --> 00:08:48,450
So all these functions are actually written in C language and we cannot see the code.

117
00:08:49,390 --> 00:08:51,540
You'd ask, Well, what is this then?

118
00:08:51,750 --> 00:09:00,720
Well, this is just a fake representation of the function and this is done by Python, so other IDs

119
00:09:00,720 --> 00:09:03,900
may not offer this feature.

120
00:09:04,650 --> 00:09:07,440
So this is not so much useful.

121
00:09:07,470 --> 00:09:12,330
Just wanted to show you that these are actually written somewhere.

122
00:09:12,330 --> 00:09:14,640
In this case, it's in C language.

123
00:09:16,260 --> 00:09:20,940
You'll see that other functions which will use later in the course are written in Python actually,

124
00:09:21,360 --> 00:09:24,600
but not these main functions.

125
00:09:26,610 --> 00:09:29,070
So let's close the console.

126
00:09:30,920 --> 00:09:36,560
And next, I want to talk about variable scope, to be exact.

127
00:09:38,370 --> 00:09:41,460
So if I was to open a new python file here.

128
00:09:44,810 --> 00:09:48,650
Just give it a name, whatever you like, and create a new function.

129
00:09:48,650 --> 00:09:49,820
Such as?

130
00:09:52,890 --> 00:09:53,730
Greet.

131
00:09:58,010 --> 00:10:01,730
This function has a variable which has a value.

132
00:10:01,760 --> 00:10:02,900
Let's say hello.

133
00:10:04,610 --> 00:10:10,220
And then maybe another variable you message equal to message thought.

134
00:10:12,880 --> 00:10:17,860
Capitalize and return new message.

135
00:10:19,210 --> 00:10:25,090
Meanwhile, I would like to right click here and keep our program open.

136
00:10:26,770 --> 00:10:28,090
Here on the right.

137
00:10:28,330 --> 00:10:30,340
So maybe you want to see that, too.

138
00:10:33,640 --> 00:10:35,560
To compare the functions.

139
00:10:35,950 --> 00:10:40,300
So both functions have a def keyword and return statement as well.

140
00:10:40,990 --> 00:10:42,520
The syntax is the same.

141
00:10:42,730 --> 00:10:45,730
They both have a variable declaration.

142
00:10:46,690 --> 00:10:47,820
In the body.

143
00:10:48,780 --> 00:10:52,710
This one has two variables here, here and here.

144
00:10:53,490 --> 00:10:55,560
So now I can call this function.

145
00:10:57,300 --> 00:11:06,360
Let's say greeting is equal to greet and then print out greeting.

146
00:11:07,890 --> 00:11:17,540
And we need to make one more break line there for being consistent with recommendations of style and

147
00:11:17,580 --> 00:11:18,210
run the code.

148
00:11:18,250 --> 00:11:20,010
Yeah, run test.

149
00:11:21,090 --> 00:11:22,200
And that's the output.

150
00:11:22,200 --> 00:11:24,480
Hello with Capital H.

151
00:11:25,920 --> 00:11:32,400
So what's happened is that when the function is called, then this is returned.

152
00:11:34,120 --> 00:11:35,520
And what is the value of that?

153
00:11:35,560 --> 00:11:36,100
Well.

154
00:11:37,380 --> 00:11:44,490
So here message gets this halo string and then the value of new message will be Halo with H capitalized.

155
00:11:44,640 --> 00:11:48,080
And so the function returns Halo capitalized.

156
00:11:48,090 --> 00:11:52,410
Therefore the value of this is going to be Halo.

157
00:11:53,690 --> 00:11:57,220
And that value is stored in the variable greeting here.

158
00:11:57,230 --> 00:11:58,610
And then we print out that value.

159
00:11:58,610 --> 00:11:59,120
So we get.

160
00:11:59,120 --> 00:11:59,630
Hello.

161
00:11:59,990 --> 00:12:01,280
So far, so good.

162
00:12:01,310 --> 00:12:12,560
What happened to though, if we print new message and this is where we talk about variable scope.

163
00:12:12,560 --> 00:12:15,860
So if we run this program, we're going to get an error.

164
00:12:15,890 --> 00:12:17,840
It says Name, error, name.

165
00:12:17,840 --> 00:12:19,550
New message is not defined.

166
00:12:19,760 --> 00:12:22,370
How comes since we define it here?

167
00:12:23,450 --> 00:12:26,810
Well, that's because this variable.

168
00:12:27,990 --> 00:12:34,800
Is only valid inside the grid function, so its scope is local.

169
00:12:36,470 --> 00:12:39,860
That's why you cannot access that variable from here.

170
00:12:39,890 --> 00:12:42,080
You'd ask, Why is that?

171
00:12:43,500 --> 00:12:45,000
Well, let's take an example.

172
00:12:45,000 --> 00:12:52,170
Let's say you go to a cafe and you go to the bartender and you order a coffee.

173
00:12:53,920 --> 00:12:55,580
You just say, I want a coffee.

174
00:12:55,600 --> 00:12:58,740
So these words are the function call.

175
00:12:58,750 --> 00:13:00,370
You just call the function.

176
00:13:00,850 --> 00:13:04,570
The function itself is the process of making the coffee.

177
00:13:04,570 --> 00:13:11,230
So the codes of the function, the elements of the function definition are maybe coffee beans.

178
00:13:11,230 --> 00:13:17,170
It's the machine, the water, the interactions between all these.

179
00:13:17,410 --> 00:13:26,290
And it's not a good idea for you as a client to have access to these elements which make up the function,

180
00:13:26,680 --> 00:13:28,550
the coffee process.

181
00:13:28,550 --> 00:13:32,170
So the function is the coffee making process.

182
00:13:33,140 --> 00:13:36,710
So all you get as a client is the coffee.

183
00:13:37,310 --> 00:13:40,430
That's what we get also from this return.

184
00:13:41,770 --> 00:13:42,400
Statement.

185
00:13:42,400 --> 00:13:51,220
So the written statement makes available only one variable, the output variable that you want the function

186
00:13:51,220 --> 00:13:57,340
to produce in our coffee example, that would be the actual coffee cup with the coffee inside.

187
00:13:57,340 --> 00:13:58,570
So that is the output.

188
00:14:00,100 --> 00:14:04,180
Likewise, you cannot access the message variable.

189
00:14:04,180 --> 00:14:06,340
So every variable in here is.

190
00:14:08,310 --> 00:14:10,290
Not accessible.

191
00:14:12,230 --> 00:14:18,980
So you can only access what is returned and you can access it by calling the function, not by referring

192
00:14:18,980 --> 00:14:20,830
to the variable name itself.

193
00:14:20,840 --> 00:14:24,800
That's an internal name inside the process.

194
00:14:27,690 --> 00:14:29,760
Now back to our app.

195
00:14:29,970 --> 00:14:39,270
We see I have this line here and this is saying that this name shadows to dos from alter scope.

196
00:14:39,330 --> 00:14:41,400
Alter scope is.

197
00:14:42,800 --> 00:14:46,580
The variables which are declared outside functions.

198
00:14:47,740 --> 00:14:50,710
So in this program here, that would be, for example, greeting.

199
00:14:50,710 --> 00:14:54,610
This is a variable in the outer scope of the program.

200
00:14:56,370 --> 00:15:02,580
So the problem here in our app, which is not a problem, actually it's a recommendation, but we can

201
00:15:02,580 --> 00:15:03,300
fix it.

202
00:15:04,050 --> 00:15:09,930
The thing is that you we have this variable with this name here, and it's not a good idea to have this

203
00:15:09,930 --> 00:15:11,250
variable with the same name.

204
00:15:11,250 --> 00:15:16,980
So what we can do is we can change the name to this variable, let's say to this local.

205
00:15:20,740 --> 00:15:21,520
And then of course.

206
00:15:21,520 --> 00:15:22,510
Yea, be careful.

207
00:15:22,510 --> 00:15:24,760
You have to return that same variable.

208
00:15:25,720 --> 00:15:26,350
Right?

209
00:15:29,240 --> 00:15:38,660
And so now what we have is we create the local variable in here, which will hold this list from to

210
00:15:38,660 --> 00:15:39,350
dos.

211
00:15:42,820 --> 00:15:45,400
And then we return the value of that.

212
00:15:45,930 --> 00:15:48,760
Therefore, now this other variable, which is.

213
00:15:49,630 --> 00:15:51,280
Not a local variable.

214
00:15:52,270 --> 00:15:53,980
It's a global variable.

215
00:15:54,870 --> 00:15:57,240
So the opposite of local is global.

216
00:15:58,200 --> 00:16:04,530
So this variable now will be equal to the value that this variable produces in the function here.

217
00:16:05,540 --> 00:16:07,300
We have the same problem with file.

218
00:16:07,310 --> 00:16:14,270
So this is also complaining that we have another variable, a global variable with name file, so we

219
00:16:14,270 --> 00:16:20,240
can convert this to local as well and also reflect that here.

220
00:16:21,320 --> 00:16:25,970
So be careful, file locally or file local there.

221
00:16:28,190 --> 00:16:29,850
So that's the final code.

222
00:16:29,870 --> 00:16:32,700
Let's fix this complaint as well.

223
00:16:32,720 --> 00:16:36,170
I can reformat the file and that will add a new break line.

224
00:16:36,170 --> 00:16:41,030
So as I said, it's good to have two break lines between functions and other parts of the code.

225
00:16:42,680 --> 00:16:46,520
So thanks a lot for following this and I hope it makes sense.

226
00:16:47,060 --> 00:16:51,740
We will be using functions over and over again throughout the course, so let's keep going.

