1
00:00:05,380 --> 00:00:09,520
Welcome back everyone in this lecture we're going to finish off this section by talking about functions

2
00:00:09,760 --> 00:00:12,180
and then right after that we'll have some exercises for you.

3
00:00:13,530 --> 00:00:18,600
Now creating clean repeatable code is a key part of becoming an effective programmer.

4
00:00:18,600 --> 00:00:23,820
Functions allow us to create blocks of code that can be easily executed many times without needing to

5
00:00:23,820 --> 00:00:27,470
constantly rewrite the entire block of code over and over again.

6
00:00:27,480 --> 00:00:32,370
Basically if you have a chunk of code that you intend on reusing more than once it probably makes sense

7
00:00:32,370 --> 00:00:37,530
to assign it to a function that we just do a simple function call instead of writing all that code over

8
00:00:37,530 --> 00:00:38,270
and over again.

9
00:00:39,430 --> 00:00:42,490
So let's see the actual syntax of a Python function.

10
00:00:42,490 --> 00:00:47,620
We start off with the DPF key word that basically tells Python that what you're about to write is the

11
00:00:47,620 --> 00:00:48,750
name of the function.

12
00:00:48,910 --> 00:00:53,890
Then you have the name of your function which you can call whatever you want and you'd start with name

13
00:00:54,040 --> 00:00:56,230
of function here with underscores.

14
00:00:56,230 --> 00:01:01,900
Keep in mind by convention names of functions should be lower case than we have open and closed parentheses

15
00:01:02,230 --> 00:01:04,530
a colon and then a block of code.

16
00:01:04,570 --> 00:01:09,970
Right below that block of code you could optionally have a documentation string and let's separate out

17
00:01:09,970 --> 00:01:13,450
by two sets of triple quotes and that just explains the function.

18
00:01:13,480 --> 00:01:18,610
In case you want someone else to be able to view your documentation string and then you'll see we have

19
00:01:18,640 --> 00:01:19,090
something like.

20
00:01:19,090 --> 00:01:22,400
PRINT Hello as the actual code to be executed.

21
00:01:22,420 --> 00:01:28,260
So in your actual Python script or at the command line if you were to call name a function over here

22
00:01:28,260 --> 00:01:32,710
at the bottom in purple and then open close princes when you run that you would get back.

23
00:01:32,710 --> 00:01:33,330
Hello.

24
00:01:33,550 --> 00:01:38,980
So is the simplest function possible basically just printing something out for you.

25
00:01:39,120 --> 00:01:43,290
Now often you're going to want your functions to accept parameters or arguments.

26
00:01:43,350 --> 00:01:49,890
Here we want to accept a name parameter and then we're going to say hello plus that name essentially

27
00:01:49,890 --> 00:01:52,310
concatenate that name to Hello.

28
00:01:52,590 --> 00:01:57,000
So what this actually looks like when you execute it is a name a function and you passing in the string

29
00:01:57,090 --> 00:01:59,020
Jose and the output there is then.

30
00:01:59,040 --> 00:02:00,020
Hello Jose.

31
00:02:00,060 --> 00:02:05,940
Just concatenating Hello whatever name was provided and notice here the name variable is something you

32
00:02:05,940 --> 00:02:08,970
could choose yourself just like you could choose a temporary variable.

33
00:02:08,970 --> 00:02:11,970
When using for loops.

34
00:02:12,070 --> 00:02:15,080
Now typically we're not just printing things with functions.

35
00:02:15,080 --> 00:02:20,290
Instead we're going to be using the return keyword to send back the result of the function instead of

36
00:02:20,290 --> 00:02:21,690
just printing it out.

37
00:02:21,700 --> 00:02:25,660
Return allows us to assign the output of the function to a new variable.

38
00:02:26,790 --> 00:02:28,790
So here we have another really simple function.

39
00:02:28,830 --> 00:02:29,800
It's called DFA.

40
00:02:29,800 --> 00:02:31,970
And then we say add underscore function.

41
00:02:32,070 --> 00:02:35,490
So this functions don't add function and it takes two parameters.

42
00:02:35,550 --> 00:02:37,250
Number one and number two.

43
00:02:37,320 --> 00:02:43,290
So it expects these to be numbers and then it's going to return number one plus num to notice a difference

44
00:02:43,290 --> 00:02:43,540
here.

45
00:02:43,560 --> 00:02:45,140
It's not printing the results.

46
00:02:45,140 --> 00:02:46,900
Instead it's returning the results.

47
00:02:47,040 --> 00:02:49,710
That way you can actually save these results to a variable.

48
00:02:49,920 --> 00:02:55,950
So I can say the result is equal to add underscore function one comma to and then you can print that

49
00:02:55,950 --> 00:03:02,140
result and you get back the number three OK let's explore more examples of using functions by heading

50
00:03:02,140 --> 00:03:03,820
over to the Adam text editor.

51
00:03:03,820 --> 00:03:08,050
Let's get started by going over the basic syntax of a Python function.

52
00:03:08,050 --> 00:03:11,890
We start by using the DFA keyword and you notice there is actually autocomplete here.

53
00:03:11,890 --> 00:03:16,240
So you could hit enter and it will autocomplete this function for you but right now I want to go through

54
00:03:16,240 --> 00:03:18,330
each of these pieces one by one.

55
00:03:18,610 --> 00:03:22,480
So you have the DFI keyword and then you can provide the name of your function.

56
00:03:22,660 --> 00:03:25,870
So this will typically be lowercase and use snake casing.

57
00:03:26,080 --> 00:03:28,340
For example we'll say something like.

58
00:03:28,550 --> 00:03:34,500
Reports underscore person then you're going to have open and close parentheses.

59
00:03:34,500 --> 00:03:38,760
And later I'll show you how you can add arguments that this right now will assume no arguments for this

60
00:03:38,760 --> 00:03:39,700
function.

61
00:03:39,840 --> 00:03:41,530
Then you say colon.

62
00:03:41,530 --> 00:03:46,410
And then here you're going to actually execute a block of code every time this report underscore person

63
00:03:46,410 --> 00:03:48,190
function is called.

64
00:03:48,390 --> 00:03:55,660
So here I'm just going to say print reporting person.

65
00:03:55,680 --> 00:03:58,090
Now let's actually run this function.

66
00:03:58,100 --> 00:04:05,340
So over here notice in the station I'm already outside of this function and the say report person open

67
00:04:05,340 --> 00:04:07,600
and close print sees to actually run them.

68
00:04:07,830 --> 00:04:09,340
So get noticed in the station.

69
00:04:09,350 --> 00:04:13,930
This is the function that self report person is now outside of the.

70
00:04:14,210 --> 00:04:21,160
Now I'm going to run this code and as I expand this I get back reporting person as my output.

71
00:04:21,170 --> 00:04:24,210
Now what happens if I forget these parentheses.

72
00:04:24,800 --> 00:04:27,230
And I've run this I don't get any output.

73
00:04:27,260 --> 00:04:30,400
That's because they actually execute the report person function.

74
00:04:30,500 --> 00:04:32,800
You're going to need those open and close parentheses.

75
00:04:32,870 --> 00:04:36,860
So keep that in mind as you're working with Python and functions OK.

76
00:04:37,100 --> 00:04:40,540
So right now our argument or excuse me our function is pretty simple.

77
00:04:40,580 --> 00:04:42,240
It's not accepting any arguments.

78
00:04:42,440 --> 00:04:48,140
So if we want to accept arguments we just need to give it an argument name of our choosing such as name

79
00:04:48,950 --> 00:04:50,640
and then we need to do something with it here.

80
00:04:50,900 --> 00:04:56,350
So let's say instead of reporting person now we're going to say reporting and I'm going to concatenate

81
00:04:56,350 --> 00:04:59,640
this with name.

82
00:04:59,740 --> 00:05:04,420
So what happens if I try running report person again with no additional arguments I'm going to get an

83
00:05:04,420 --> 00:05:08,530
error and the error says hey report person is missing.

84
00:05:08,560 --> 00:05:13,990
One required positional argument called name which is basically Python trying to tell you Hey you forgot

85
00:05:13,990 --> 00:05:16,580
to provide this argument that this function is expecting.

86
00:05:16,780 --> 00:05:23,350
So let's provide it one will say reporting Cindy will save that.

87
00:05:23,640 --> 00:05:27,840
Run this again and now it says reporting Cindy.

88
00:05:28,070 --> 00:05:33,910
So the function did is it took in your argument for name and then it went ahead and said print's reporting

89
00:05:33,970 --> 00:05:38,190
and it concatenated that name that you provided with reporting.

90
00:05:38,260 --> 00:05:43,310
Now let's imagine that sometimes you want your user to have some sort of default argument value.

91
00:05:43,420 --> 00:05:44,550
That's actually quite easy.

92
00:05:46,080 --> 00:05:49,470
You can just say name is equal to and then whatever default argument you want.

93
00:05:49,530 --> 00:05:53,560
So maybe you want blank to be the default.

94
00:05:53,850 --> 00:05:55,170
So I'll save that.

95
00:05:55,170 --> 00:05:59,230
And now notice what happens if I provide Sindy the nothing changes.

96
00:05:59,250 --> 00:06:04,710
I still see reporting Sindee but for some reason I don't provide that argument.

97
00:06:04,800 --> 00:06:06,560
Now I no longer get an error.

98
00:06:06,570 --> 00:06:09,870
Instead I go to the default which is reporting blank.

99
00:06:09,870 --> 00:06:14,400
So if you ever want to have a default value for your argument you can just define that default right

100
00:06:14,400 --> 00:06:16,710
here in the actual function definition.

101
00:06:16,770 --> 00:06:21,330
You'll say report function the name of your argument as equal to and then whatever the default value

102
00:06:21,330 --> 00:06:21,990
launches.

103
00:06:22,260 --> 00:06:27,120
And keep in mind this parameter or argument name it can really be up to you.

104
00:06:27,120 --> 00:06:30,650
So just to make that really clear I could say Jeli is equal to blank.

105
00:06:30,660 --> 00:06:33,290
And instead of saying plus name I can just say.

106
00:06:33,420 --> 00:06:35,850
Plus Jeli and Soilwork the exact same way.

107
00:06:35,880 --> 00:06:36,960
Reporting blank.

108
00:06:37,020 --> 00:06:41,190
So these parameter names are totally up to you just like when they were learning about four loops and

109
00:06:41,190 --> 00:06:43,780
you could choose any parameter name there.

110
00:06:43,840 --> 00:06:49,030
Now something I really want to make clear is so far we've just been printing out stuff when we call

111
00:06:49,030 --> 00:06:49,720
our functions.

112
00:06:49,750 --> 00:06:52,060
And typically that's not what we're going to be doing.

113
00:06:52,060 --> 00:06:56,870
Instead we're going to be returning variables back.

114
00:06:56,870 --> 00:07:04,630
So I going to delete all of this and create a new function we're going to say call this function add

115
00:07:04,760 --> 00:07:17,050
some and I'm going to take someone to and then right now I'm going to print one plus two.

116
00:07:17,150 --> 00:07:24,820
So that means if I were to call add on two and four I should see six pop out at my command line and

117
00:07:24,820 --> 00:07:25,390
there it is.

118
00:07:25,390 --> 00:07:27,080
That's adding two plus four.

119
00:07:27,460 --> 00:07:28,000
OK.

120
00:07:28,100 --> 00:07:33,170
So let's say I wanted to save this result for later and use it somewhere or some code for example.

121
00:07:33,170 --> 00:07:34,640
I want to do the following.

122
00:07:34,640 --> 00:07:37,390
I would say result is equal to add.

123
00:07:38,030 --> 00:07:42,040
And then later on I wanted to print results plus 10.

124
00:07:42,080 --> 00:07:47,420
So this is a mistake and something Beginners often do they just use prints inside of the functions and

125
00:07:47,420 --> 00:07:49,120
then try to save the result of a print.

126
00:07:49,160 --> 00:07:51,320
But that's actually not going to work.

127
00:07:51,380 --> 00:07:52,970
So let's run this and see what happens.

128
00:07:54,210 --> 00:07:57,160
So notice we get a traceback.

129
00:07:57,210 --> 00:08:00,040
First off I was able to get that initial 6 out.

130
00:08:00,120 --> 00:08:07,410
So I did print out two plus four online for one actually called add underscore what's not working is

131
00:08:07,410 --> 00:08:13,260
me saving this result because this result is actually a nun because print isn't returning anything it's

132
00:08:13,260 --> 00:08:15,380
just shooting something out to the command line.

133
00:08:15,450 --> 00:08:16,770
So you can see it.

134
00:08:16,800 --> 00:08:20,910
So when I scroll down here I may view my whole traceback which is just the error.

135
00:08:21,120 --> 00:08:25,110
It says Hey on line 6 on print's result plus 10.

136
00:08:25,110 --> 00:08:25,770
So right here.

137
00:08:25,770 --> 00:08:32,500
Print result plus then there's an unsupported operation plus for none type an integer because results

138
00:08:32,500 --> 00:08:33,180
right here.

139
00:08:33,300 --> 00:08:34,590
That's actually none.

140
00:08:34,590 --> 00:08:42,320
In fact we can prove it to ourselves by deleting plus 10 saving that run this again and I see none.

141
00:08:42,600 --> 00:08:44,490
So notice again what's happening here.

142
00:08:44,520 --> 00:08:46,420
Print isn't returning anything.

143
00:08:46,440 --> 00:08:50,600
You can't actually save the result of some sort of Printz function.

144
00:08:50,610 --> 00:08:55,020
So what do we do if we actually want to use this result later on in our code which is typically the

145
00:08:55,020 --> 00:08:57,630
way your Python programs are going to work.

146
00:08:57,780 --> 00:09:04,410
In that case we need to use the return keyword so we use the return keyword.

147
00:09:04,410 --> 00:09:08,900
And this is going to allow us to actually save whatever this function does.

148
00:09:08,940 --> 00:09:10,100
As a result.

149
00:09:10,380 --> 00:09:14,180
So now instead of printing out the numbers Ondoy to return that.

150
00:09:14,340 --> 00:09:16,450
So going to say add 2 and 4.

151
00:09:16,770 --> 00:09:19,950
Save that as a result and then print that result.

152
00:09:19,950 --> 00:09:23,630
But let's see what happens if I don't print the result first.

153
00:09:23,670 --> 00:09:27,390
So I run this and notice I don't get back any sort of result.

154
00:09:27,390 --> 00:09:29,260
However there were no errors.

155
00:09:29,310 --> 00:09:32,580
It's only when I print the results that actually get to see it.

156
00:09:32,910 --> 00:09:34,580
Now I get the see.

157
00:09:34,590 --> 00:09:39,040
So again the return key word what it's doing is it's adding someone and I'm to here.

158
00:09:39,100 --> 00:09:39,930
So two and four.

159
00:09:39,960 --> 00:09:45,000
And then saving it to this result where they can do stuff with it so that it can print results and if

160
00:09:45,000 --> 00:09:48,150
it wanted to I could then print result plus 10.

161
00:09:48,150 --> 00:09:51,280
So now when I run this I get back 16 again.

162
00:09:51,300 --> 00:09:56,040
That's the key difference between using something like print and something like Return Return is going

163
00:09:56,040 --> 00:10:01,870
to allow you to actually save your results of your function to variables to use later on in your code.

164
00:10:02,000 --> 00:10:06,270
So typically you're pretty much always using a return with functions instead of print.

165
00:10:06,450 --> 00:10:09,620
So keep that in mind this is a really common mistake for beginners to make.

166
00:10:10,080 --> 00:10:12,220
OK we're going to stop here for part one.

167
00:10:12,240 --> 00:10:16,440
Part two of our functions discussion will focus on solving problems with functions.

168
00:10:16,500 --> 00:10:18,450
Also cover some useful operators.

169
00:10:18,450 --> 00:10:20,340
So we'll see you in part two and the very next lecture.
