1
00:00:05,190 --> 00:00:07,920
Welcome back, everyone, to this lecture on functions.

2
00:00:09,360 --> 00:00:14,010
Now, often as you're coding of Python, you're going to have some code that you actually want to use

3
00:00:14,010 --> 00:00:19,800
multiple times, but you don't always have to want to rewrite that exact same block of code over and

4
00:00:19,800 --> 00:00:22,770
over again, especially across multiple Python files.

5
00:00:22,800 --> 00:00:26,940
It would be nice if you could set it up in a block, they can then just call to execute.

6
00:00:27,180 --> 00:00:29,010
And that's where the function comes into play.

7
00:00:29,250 --> 00:00:32,670
It allows you to just be able to repeat the use of code.

8
00:00:34,130 --> 00:00:39,770
Now, creating a function requires a very specific syntax, including the IDF or defining keyword.

9
00:00:40,100 --> 00:00:44,510
Then you have the correct indentation to concern yourself about as well as the proper structure.

10
00:00:44,870 --> 00:00:48,710
So I want to give you a quick overview of the Python function, structure and syntax.

11
00:00:48,920 --> 00:00:51,080
Before we jump to the editor to start coding.

12
00:00:52,570 --> 00:00:57,430
So it all begins with the death keyword, and this keyword essentially tells Python that you're beginning

13
00:00:57,460 --> 00:00:58,330
to write a function.

14
00:00:59,410 --> 00:01:02,060
And then you have the name of the function you should notice.

15
00:01:02,080 --> 00:01:05,560
It follows snake casing and you can decide on the function name.

16
00:01:06,280 --> 00:01:09,700
Snake casing just means it's all lowercase of underscores between the words.

17
00:01:10,180 --> 00:01:11,080
This is by convention.

18
00:01:11,080 --> 00:01:15,010
You don't have to do it this way, but it is the proper way to do it, so to speak.

19
00:01:15,760 --> 00:01:17,440
Then you're going to have some parentheses.

20
00:01:17,920 --> 00:01:22,510
Later on, we're going to discover we can actually pass in arguments or parameters into the function

21
00:01:22,630 --> 00:01:23,830
using these parentheses.

22
00:01:25,610 --> 00:01:30,800
Then you have the colon, which indicates an upcoming indented block, and everything indented is then

23
00:01:30,800 --> 00:01:35,360
inside the function that's going to be executed each time you just call the name of the function.

24
00:01:36,930 --> 00:01:42,630
Now there's the option of providing a documentation string, which will explain the function if somebody

25
00:01:42,630 --> 00:01:47,190
passes in the name of your function into the built in help function with Python.

26
00:01:47,910 --> 00:01:52,440
We should also note that everything inside the function is indented, including in this documentation

27
00:01:52,440 --> 00:01:52,710
straight.

28
00:01:54,270 --> 00:01:58,680
And then you're just going to have code that you want to actually execute each time you call the function.

29
00:01:58,980 --> 00:02:03,550
Here we have a very, very simple function that is simply going to print hello each time you call.

30
00:02:03,570 --> 00:02:04,650
Name a function.

31
00:02:05,990 --> 00:02:10,970
So, for example, if I were to call name a function open and close parentheses, I would just see the

32
00:02:10,970 --> 00:02:12,380
print hello as the result.

33
00:02:16,210 --> 00:02:16,570
OK.

34
00:02:16,960 --> 00:02:21,520
We should also note that functions could actually accept arguments to be passed by the user.

35
00:02:21,940 --> 00:02:23,300
So notice now what's changed?

36
00:02:23,320 --> 00:02:29,500
I have a name of the function, but now it's going to actually be able to accept an argument and this

37
00:02:29,500 --> 00:02:31,660
argument is a variable called name.

38
00:02:32,080 --> 00:02:37,570
And then what's gonna happen is it's going to take that name and then tack it on to hello.

39
00:02:37,720 --> 00:02:41,830
So if I call name a function, Jose, they'll report back or print back.

40
00:02:42,010 --> 00:02:42,760
Hello, Jose.

41
00:02:43,150 --> 00:02:46,030
So functions can accept arguments to be passed by the user.

42
00:02:47,170 --> 00:02:51,850
So note again, name is being passed in and then we're saying print hello name.

43
00:02:52,360 --> 00:02:54,760
So in the output input, I can see.

44
00:02:55,180 --> 00:02:56,440
Name a function, Jose.

45
00:02:56,950 --> 00:02:57,390
Print out.

46
00:02:57,400 --> 00:02:58,060
Hello, Jose.

47
00:02:59,640 --> 00:03:06,060
Typically, however, we're not just printing results with functions, we typically use the return keyword

48
00:03:06,180 --> 00:03:09,600
to send back the result of the function instead of just printing it out.

49
00:03:10,080 --> 00:03:13,740
Return allows us to assign the output of the function to a new variable.

50
00:03:14,160 --> 00:03:16,800
A question I get a lot from students beginning to learn.

51
00:03:16,800 --> 00:03:21,210
Python is what's the difference between printing in a function and returning in that function?

52
00:03:21,510 --> 00:03:25,740
And again, return allows us to save the results if we just print the results.

53
00:03:25,920 --> 00:03:29,910
We do see the output, but I don't get to save them to a variable to then later use.

54
00:03:30,540 --> 00:03:35,400
And as a quick note, we will have a deeper discussion of the return cured later on in the actual editor

55
00:03:35,400 --> 00:03:38,160
because it's easier to explain when we're actually coding this out.

56
00:03:39,450 --> 00:03:45,060
But as a quick example of what this would look like here have a function called ad function, and it

57
00:03:45,060 --> 00:03:46,540
takes in two arguments.

58
00:03:46,560 --> 00:03:51,810
Number one and number two and then it returns the result number one plus number two.

59
00:03:52,290 --> 00:03:55,620
So the return actually allows us to save the result to a variable.

60
00:03:55,920 --> 00:04:00,740
Notice now how I can say result is equal to add a function of one or two.

61
00:04:00,750 --> 00:04:03,570
And then if I want it to later on, I could print that result.

62
00:04:04,380 --> 00:04:06,420
So most functions will use return.

63
00:04:06,660 --> 00:04:10,860
We are very rarely just using a function to call a print statement.

64
00:04:11,130 --> 00:04:15,690
It depends on the use case, of course, but pretty much we're always going to add a return statement

65
00:04:15,870 --> 00:04:18,959
to a function to actually return a result for use later.

66
00:04:20,140 --> 00:04:23,590
OK, let's start creating functions with Python and the jump to the editor.

67
00:04:25,070 --> 00:04:29,360
All right, so let's begin by setting up our first function.

68
00:04:29,630 --> 00:04:31,190
This function is going to be super simple.

69
00:04:31,190 --> 00:04:33,680
It's actually just going to print something later on.

70
00:04:33,680 --> 00:04:34,820
We'll talk about return.

71
00:04:35,360 --> 00:04:37,680
So to start a function, we say DPF.

72
00:04:38,450 --> 00:04:41,990
And notice we already know it's a function based off the syntax highlighting.

73
00:04:42,320 --> 00:04:46,420
And I'm going to call this function simply say Underscore hello.

74
00:04:46,910 --> 00:04:50,750
Open close parentheses colon enter.

75
00:04:50,960 --> 00:04:52,370
The indentation starts.

76
00:04:52,820 --> 00:04:59,210
And then every time I call, say hello, I can just print out something like hello and let's.

77
00:04:59,210 --> 00:05:03,830
In fact, let's print out another line that says another hello.

78
00:05:05,420 --> 00:05:11,720
So typically, if I wanted to print hello and print another hello, each time I wanted to print both

79
00:05:11,720 --> 00:05:13,850
of these, I'd have to copy and paste both lines.

80
00:05:14,120 --> 00:05:21,020
But now that I have a function, what I can do is simply call, say hello and I execute that function

81
00:05:21,020 --> 00:05:22,370
with open and close parentheses.

82
00:05:22,760 --> 00:05:26,420
Pay very close attention to my indentation here.

83
00:05:26,600 --> 00:05:29,480
Say hello is no longer inside the function.

84
00:05:29,870 --> 00:05:32,420
And what's nice about the code editor of this visual studio?

85
00:05:32,720 --> 00:05:37,400
When you define the function, you can actually collapse the function by hitting this little dropdown

86
00:05:37,400 --> 00:05:37,940
arrow key.

87
00:05:38,510 --> 00:05:42,920
And then you can really see what's inside your function that actually helps you kind of read your code

88
00:05:42,920 --> 00:05:43,940
and save a little space.

89
00:05:44,210 --> 00:05:50,090
If you want to collapse the function code, so again, be very careful, don't accidentally do something

90
00:05:50,090 --> 00:05:50,570
like this.

91
00:05:50,570 --> 00:05:55,730
In which case you're calling, say hello within, say hello and you can check for doing that by collapsing.

92
00:05:56,240 --> 00:06:01,070
So again, pull say hello out, which means I'm actually going to execute the block of code inside.

93
00:06:01,070 --> 00:06:01,640
Say hello.

94
00:06:02,510 --> 00:06:09,110
So when I run this, I see hello and another hello, and I can call this function now as many times

95
00:06:09,290 --> 00:06:11,750
as I want, so I can call it twice here.

96
00:06:12,230 --> 00:06:12,740
Say hello.

97
00:06:12,740 --> 00:06:13,380
Say hello.

98
00:06:13,460 --> 00:06:14,820
And so we should see it.

99
00:06:15,620 --> 00:06:16,940
Hello, another hello.

100
00:06:16,940 --> 00:06:17,900
Hello another.

101
00:06:17,900 --> 00:06:18,260
Hello.

102
00:06:18,800 --> 00:06:24,320
OK, now I didn't mention typically, you're not actually calling print statements like this within

103
00:06:24,320 --> 00:06:24,830
a function.

104
00:06:25,160 --> 00:06:30,230
Instead, you like to perform some sort of logic or calculation and then actually return the results.

105
00:06:30,680 --> 00:06:32,960
So let's talk about this return keyword.

106
00:06:33,890 --> 00:06:40,610
Return allows you to actually return something to be saved as a variable.

107
00:06:40,970 --> 00:06:43,730
So I'm actually now just going to return the string.

108
00:06:44,980 --> 00:06:45,430
Hello.

109
00:06:46,610 --> 00:06:49,010
And you're going to see what happens when I call say hello.

110
00:06:50,070 --> 00:06:52,500
So notice now I'm just returning it.

111
00:06:52,950 --> 00:06:54,240
I'm no longer printing it.

112
00:06:54,300 --> 00:06:59,610
And when you run the Python code now after saving those changes, you actually don't see anything output

113
00:06:59,610 --> 00:06:59,910
here.

114
00:07:00,240 --> 00:07:04,110
And that's because return is just saying, Hey, I'm going to give you this string.

115
00:07:04,350 --> 00:07:06,090
It's up to you to do whatever you want with it.

116
00:07:06,600 --> 00:07:10,260
So, for example, I could save it to a variable like this.

117
00:07:10,260 --> 00:07:15,390
I could say my variable is now equal to say hello and then I could print out.

118
00:07:16,930 --> 00:07:24,370
My variable save those changes, and now you see hello, print it out, what you cannot do is something

119
00:07:24,370 --> 00:07:24,940
like this.

120
00:07:25,360 --> 00:07:26,350
If I were to say.

121
00:07:28,150 --> 00:07:29,020
Prince, hello.

122
00:07:30,110 --> 00:07:36,710
And then try to do an assignment like this online for this actually won't work, you can't really assign

123
00:07:36,710 --> 00:07:43,190
print hello to this and then print out that, well, you're going to end up seeing is hello and then

124
00:07:43,190 --> 00:07:44,810
you're going to get this weird nun.

125
00:07:45,860 --> 00:07:51,440
You get hello because you did call say hello, but then say hello is not actually returning anything,

126
00:07:51,440 --> 00:07:53,850
which is why my variable then becomes not.

127
00:07:53,870 --> 00:07:55,100
It's just a placeholder there.

128
00:07:55,580 --> 00:08:01,040
So typically, we want to be able to save the results of all the hard work we do within our functions,

129
00:08:01,340 --> 00:08:07,370
which is why we end up returning things within the functions in order to save them as variables.

130
00:08:07,640 --> 00:08:12,380
You may or may not want to print out that variable, but returning it allows you the chance to actually

131
00:08:12,380 --> 00:08:12,860
save it.

132
00:08:13,250 --> 00:08:17,330
So let's start adding in some complexity by allowing for arguments.

133
00:08:17,510 --> 00:08:24,080
So now I'm going to say hello to someone's name, so I'm going to be able to accept an argument name

134
00:08:24,080 --> 00:08:25,950
and I can accept as many arguments as you want.

135
00:08:26,330 --> 00:08:31,460
And you can also call this variable whatever you want this argument parameter, you can call it whatever

136
00:08:31,460 --> 00:08:31,880
you want.

137
00:08:32,299 --> 00:08:37,789
And now what I'm going to say is return the string and it's going to be an f string literal this time

138
00:08:37,789 --> 00:08:43,520
that says hello, curly braces and then the name that's passed them by the user.

139
00:08:43,820 --> 00:08:48,080
All the arguments up here are going to be passed in by the user.

140
00:08:48,530 --> 00:08:53,240
So now I'm going to say my variable is equal to the result returned by say hello, which is going to

141
00:08:53,240 --> 00:08:54,490
be this string literal.

142
00:08:54,500 --> 00:08:55,490
It says hello name.

143
00:08:56,000 --> 00:09:00,350
If you just run it like this, you're actually going to get an error because it expects a name to be

144
00:09:00,350 --> 00:09:00,860
passed it.

145
00:09:01,400 --> 00:09:07,430
So if I run this right now, it's going to say, Hey, say hello is missing a required positional argument

146
00:09:07,640 --> 00:09:08,090
name.

147
00:09:08,600 --> 00:09:10,400
So how do I pass in the argument?

148
00:09:10,940 --> 00:09:14,840
I say name is equal to and then whatever you actually want to pass in as the name.

149
00:09:15,440 --> 00:09:19,940
So now when you're running this code, you see, Hello, Jose.

150
00:09:20,420 --> 00:09:22,040
The user is passing in.

151
00:09:22,040 --> 00:09:23,450
I want name equal to Jose.

152
00:09:23,900 --> 00:09:27,200
And so it's executing Say hello, it's returning back the string.

153
00:09:27,410 --> 00:09:31,040
Hello, Jose, save it to the variable and print that out.

154
00:09:33,800 --> 00:09:39,350
And we can pass in more than just one parameter here, I can say, taken a first name.

155
00:09:40,450 --> 00:09:45,430
Karma taking a last name, and then I could return something like, hello.

156
00:09:46,410 --> 00:09:47,280
First name.

157
00:09:48,410 --> 00:09:49,070
Space.

158
00:09:49,940 --> 00:09:58,130
And then insert last name, so say hello, and then now I have to make sure these match up, you can

159
00:09:58,130 --> 00:10:00,350
do it by position or by name.

160
00:10:00,350 --> 00:10:05,360
So I could just say something like Jose Comma Porsche here.

161
00:10:06,170 --> 00:10:06,770
Save that.

162
00:10:07,070 --> 00:10:09,110
And then when you run this code, you should see.

163
00:10:09,530 --> 00:10:10,550
Hello, Jose Padilla.

164
00:10:11,450 --> 00:10:16,730
I'm doing it by passing in by position, but it's often better just to be really explicit and say,

165
00:10:16,730 --> 00:10:18,250
OK, first name is equal to Jose.

166
00:10:19,040 --> 00:10:19,760
Last name.

167
00:10:20,940 --> 00:10:23,760
Is equal to Porsche that way, whoever's reading your code later on.

168
00:10:23,940 --> 00:10:28,800
Maybe you have one hundred lines in between this function call and the sexual assignment, in which

169
00:10:28,800 --> 00:10:32,550
case it'd be nice to know what these variables or arguments actually stood for.

170
00:10:33,480 --> 00:10:35,820
So let's start adding in a little bit of logic.

171
00:10:36,030 --> 00:10:43,260
In fact, let's make a function that is going to check if your a number is less than or equal to 100.

172
00:10:43,860 --> 00:10:50,100
So I'm going to create a function the f called checker and this simply text if your number is greater

173
00:10:50,100 --> 00:10:51,330
than or equal to 100.

174
00:10:52,020 --> 00:10:57,310
So I take in the number and I'm going to say some comparison logic.

175
00:10:57,330 --> 00:10:59,190
If the number.

176
00:11:00,140 --> 00:11:02,090
Is greater than 100.

177
00:11:02,810 --> 00:11:07,550
I'm going to return greater than 100.

178
00:11:08,960 --> 00:11:12,020
Else I'm going to return.

179
00:11:13,610 --> 00:11:15,980
Not greater than one hundred.

180
00:11:17,470 --> 00:11:21,130
The air there, so I have that function checker.

181
00:11:22,070 --> 00:11:28,850
Go ahead and save those changes and then what I can do here is print out the results of checker and

182
00:11:28,850 --> 00:11:30,290
let's check 90.

183
00:11:32,430 --> 00:11:35,010
And again, I'm printing the results so I can see them here in the output.

184
00:11:37,290 --> 00:11:39,420
And 90 is not greater than 100.

185
00:11:39,510 --> 00:11:39,930
Perfect.

186
00:11:39,960 --> 00:11:43,160
So it looks like it's working as expected, let's put in nine hundred there.

187
00:11:44,100 --> 00:11:44,670
Save that.

188
00:11:45,780 --> 00:11:48,030
And then it looks like nine hundred is greater than 100.

189
00:11:48,270 --> 00:11:48,780
Perfect.

190
00:11:50,220 --> 00:11:54,570
And keep in mind here, we're just showing very simple examples, but we can actually get quite complex

191
00:11:54,570 --> 00:11:54,990
of this.

192
00:11:55,350 --> 00:12:01,050
Let's imagine that I have a list like my list and it's filled in.

193
00:12:01,050 --> 00:12:05,220
If a bunch of random numbers, I'm going to pass in a bunch of random numbers here.

194
00:12:05,220 --> 00:12:10,020
We can think of this as maybe a data stream that's coming in, and I actually need to know whether or

195
00:12:10,020 --> 00:12:13,140
not the number 10 is in that list.

196
00:12:14,070 --> 00:12:14,970
So how can I do that?

197
00:12:14,980 --> 00:12:17,250
How can I check if 10 is in that list?

198
00:12:17,970 --> 00:12:22,650
One way I could do it is by actually using a for loop to go through this list.

199
00:12:22,830 --> 00:12:24,570
I should note there's a better way of doing that.

200
00:12:24,840 --> 00:12:28,800
You could just use the in operator, but I'm going to pretend I don't know that for right now, just

201
00:12:28,800 --> 00:12:31,020
to get the idea of logic within the function.

202
00:12:31,620 --> 00:12:38,280
So I'm going to have another function here that's called check or checker again, and it's going to

203
00:12:38,280 --> 00:12:40,380
take in the list to check.

204
00:12:40,740 --> 00:12:42,360
So let's go ahead and just pass in.

205
00:12:43,480 --> 00:12:45,190
List to check.

206
00:12:47,990 --> 00:12:53,390
And then what it's going to do is it's going to say for number in the list to check.

207
00:12:53,900 --> 00:12:56,960
Note that has to match the input variable.

208
00:12:57,260 --> 00:12:59,030
So for every number enlisted, check.

209
00:13:00,360 --> 00:13:00,780
If.

210
00:13:02,070 --> 00:13:06,480
That number is equal to, let's say, 10 is what we wanted to check for.

211
00:13:07,020 --> 00:13:09,360
We go ahead and return true.

212
00:13:11,040 --> 00:13:16,590
So what's going to happen here just for this first part of the logic is I'm going to go through every

213
00:13:16,590 --> 00:13:22,980
number in this list here and then I'm going to say, OK, is that number equal to 10?

214
00:13:23,310 --> 00:13:29,940
And if at any point this happens to be equal to true, the return hits and then I'm done.

215
00:13:30,690 --> 00:13:35,160
You'll notice that once you hit return in a function, the function ceases to operate.

216
00:13:35,760 --> 00:13:40,110
Now let's say I go through this entire for loop and this never actually returns.

217
00:13:40,380 --> 00:13:41,400
What do I want to return?

218
00:13:41,400 --> 00:13:42,600
Then I want to return false.

219
00:13:43,290 --> 00:13:46,800
So a question is where should I put the return false?

220
00:13:47,340 --> 00:13:49,350
Should I do it here?

221
00:13:50,190 --> 00:13:51,090
Return false?

222
00:13:51,570 --> 00:13:53,880
Or should I do it here?

223
00:13:54,420 --> 00:13:56,550
The indentation can sometimes get confusing.

224
00:13:57,300 --> 00:14:00,090
It should go here in line with the four.

225
00:14:00,390 --> 00:14:01,710
And why is that the case?

226
00:14:02,070 --> 00:14:08,190
Well, because I want to make sure I go through all these numbers first and then if I don't happen to

227
00:14:08,190 --> 00:14:10,950
return true at the very end, I'll return false.

228
00:14:11,370 --> 00:14:16,410
A really common beginner mistake to try to solve this sort of problem would to be use an else statement

229
00:14:16,410 --> 00:14:16,770
here.

230
00:14:17,850 --> 00:14:21,330
And then they'll put return false inside this else statement.

231
00:14:21,750 --> 00:14:27,720
But the reason this is incorrect is because at the very first number at one, what's going to happen

232
00:14:27,720 --> 00:14:29,250
is, well, that's not equal to 10.

233
00:14:29,640 --> 00:14:31,620
So I'm just simply going to return false.

234
00:14:31,920 --> 00:14:33,730
It will immediately stop at the first number.

235
00:14:33,750 --> 00:14:38,970
Instead of having the chance to look through all of them since we want it to have the chance to complete

236
00:14:38,970 --> 00:14:45,160
that for loop, we don't end up using that statement and we line up the return over here with that for

237
00:14:45,160 --> 00:14:45,390
a loop.

238
00:14:45,840 --> 00:14:47,490
So just try to keep that thing in mind.

239
00:14:47,850 --> 00:14:53,160
It's really helpful to sometimes use prints to decide what's actually happening within your block of

240
00:14:53,160 --> 00:14:53,490
code.

241
00:14:53,880 --> 00:14:56,040
But let's go ahead and use checker here.

242
00:14:56,610 --> 00:15:02,160
I'm going to call prints the result of checker on my list.

243
00:15:02,970 --> 00:15:06,810
Note my list does have a 10 on it, so I would expect it to return true here.

244
00:15:07,560 --> 00:15:11,850
And we're going to say Python Example Pi, and let's expand this.

245
00:15:13,420 --> 00:15:16,190
So Python example pie and it looks like a return, true.

246
00:15:16,210 --> 00:15:18,670
I'm going to zoom out one level here swing series thing that's going on.

247
00:15:19,360 --> 00:15:21,610
OK, so let's remove 10 now.

248
00:15:22,600 --> 00:15:24,670
So there is no longer a 10 on that list.

249
00:15:24,880 --> 00:15:26,200
We'll go ahead and save that change.

250
00:15:26,620 --> 00:15:27,580
Let's go ahead and run Python.

251
00:15:27,580 --> 00:15:28,510
Example Python.

252
00:15:28,870 --> 00:15:29,920
And now we get false.

253
00:15:30,640 --> 00:15:34,360
So you can see us beginning to combine many, many different ideas here.

254
00:15:34,600 --> 00:15:40,810
I'm combining the idea of a function, the idea of Boolean, the idea of a for loop, the idea of if

255
00:15:41,000 --> 00:15:43,240
but statements of comparison operators.

256
00:15:43,420 --> 00:15:47,110
It's all coming together to build some sort of functionality.

257
00:15:47,680 --> 00:15:53,200
What we're going to do next is give you a series of problems where you have to write a function that

258
00:15:53,200 --> 00:15:58,150
returns something and they're going to get increasingly more complex and more difficult just to make

259
00:15:58,150 --> 00:16:03,550
sure you understand the building blocks creating functions to end this section of the course.

260
00:16:04,240 --> 00:16:04,540
OK.

261
00:16:05,150 --> 00:16:05,590
Thanks.

262
00:16:05,740 --> 00:16:07,210
And I'll see you at the next lecture.

