1
00:00:00,330 --> 00:00:01,163
To begin

2
00:00:01,170 --> 00:00:05,460
the first thing I want to talk about is more on functions.

3
00:00:05,880 --> 00:00:09,120
So we've already seen a couple of things that we can do with functions.

4
00:00:09,630 --> 00:00:13,860
The first thing I showed you was how to create a very simple bog-standard 

5
00:00:13,860 --> 00:00:18,060
function using the def, the name, the empty parentheses,

6
00:00:18,240 --> 00:00:21,690
and then the colon. And then within the body of the function,

7
00:00:21,780 --> 00:00:26,780
you can do a bunch of things which will be carried out every single time you

8
00:00:27,420 --> 00:00:30,630
trigger or call the function like this.

9
00:00:31,260 --> 00:00:35,310
And the first type of function basically just helps you reduce the amount of

10
00:00:35,310 --> 00:00:36,420
code you have to write

11
00:00:36,720 --> 00:00:40,980
when you have instructions that you want to execute repeatedly. Now,

12
00:00:40,980 --> 00:00:45,420
the next type of function we saw actually had something inside the parentheses

13
00:00:45,930 --> 00:00:50,930
and this something is an input which can be passed over when we call the

14
00:00:51,480 --> 00:00:53,820
function. So in this case, 123

15
00:00:53,820 --> 00:00:58,820
is the argument which gets passed into this parameter called something

16
00:00:59,670 --> 00:01:02,520
and then it gets used within the body of the function.

17
00:01:03,030 --> 00:01:05,160
The second type of function, the functions

18
00:01:05,160 --> 00:01:10,160
which allow for inputs, gives us the ability to modify the code in the function

19
00:01:10,920 --> 00:01:13,740
and to get it to do something different each time

20
00:01:13,920 --> 00:01:16,350
depending on what input was passed in.

21
00:01:17,070 --> 00:01:20,820
We're now onto the final sort of flavor functions

22
00:01:21,270 --> 00:01:22,770
and these are functions

23
00:01:22,830 --> 00:01:26,280
which allow you to have an output

24
00:01:26,430 --> 00:01:28,170
once the function is completed.

25
00:01:28,920 --> 00:01:32,460
We start off with a simple function, and in this case

26
00:01:32,460 --> 00:01:36,720
you'll notice that even though I could, I'm actually not giving it any inputs.

27
00:01:36,750 --> 00:01:39,840
The inputs and the outputs are completely separate from each other.

28
00:01:40,350 --> 00:01:44,940
So this is a normal function that you've seen many times now. And within the

29
00:01:44,940 --> 00:01:49,740
body of the function, I'm going to do some piece of calculation. Let's say

30
00:01:49,740 --> 00:01:52,200
I'm going to calculate what is three times two,

31
00:01:52,560 --> 00:01:56,790
and then save it to a variable called result. Now, once that's done,

32
00:01:57,060 --> 00:02:00,980
I can use the output keyword, which is return.

33
00:02:01,460 --> 00:02:04,940
And then after that keyword, I can put whatever it is

34
00:02:04,940 --> 00:02:09,340
I want to be the output of this function. So in this case,

35
00:02:09,500 --> 00:02:11,570
the output is the result.

36
00:02:11,990 --> 00:02:16,990
And what that means is when I call this function later on and it runs,

37
00:02:18,350 --> 00:02:22,400
then it will go ahead and output this result

38
00:02:22,880 --> 00:02:27,710
and it replaces this line of code where this function was called.

39
00:02:28,220 --> 00:02:33,220
Now that result is held in the code where the function was called and I could

40
00:02:33,770 --> 00:02:38,180
save it to another variable. Result, of course, equals

41
00:02:38,180 --> 00:02:42,770
3 * 2 and that means this output now equal 6.

42
00:02:43,280 --> 00:02:47,960
So this is what the code might look like. Somewhere I would define my function

43
00:02:48,140 --> 00:02:53,120
which has an output, which you can tell by the return keyword. Now,

44
00:02:53,180 --> 00:02:57,230
at some other point in my code, I decide to call that function

45
00:02:57,710 --> 00:03:02,710
and once that function executes, that return or whatever is outputted replaces

46
00:03:03,760 --> 00:03:06,460
the function call. And at the very end,

47
00:03:06,550 --> 00:03:11,200
it means this variable output will store whatever the output is.

48
00:03:11,530 --> 00:03:14,710
So in this case, output would be equal to 6.

49
00:03:15,790 --> 00:03:19,840
The way that I like to think about functions with outputs is almost kind of like

50
00:03:19,840 --> 00:03:20,890
a machine, right?

51
00:03:21,400 --> 00:03:26,400
If you had a machine where in goes some empty bottles and after some processing

52
00:03:27,130 --> 00:03:31,360
or some inputs, outcomes a bottle filled with milk.

53
00:03:31,750 --> 00:03:36,490
So in this case, this function has an input, the empty glass bottle.

54
00:03:36,790 --> 00:03:41,380
It has an output, the glass bottle filled with milk and in the middle,

55
00:03:41,410 --> 00:03:46,410
there's some processes or some code that's being executed to create this change.

56
00:03:47,560 --> 00:03:50,980
This ties together all the things that we've learned about functions.

57
00:03:51,430 --> 00:03:55,180
So now head over to the Day 10 starting replit,

58
00:03:55,690 --> 00:03:59,320
and go ahead and fork your own copy of it. Now,

59
00:03:59,320 --> 00:04:01,030
once you have your own copy of it,

60
00:04:01,540 --> 00:04:05,890
we're going to create some functions with outputs of our own.

61
00:04:06,640 --> 00:04:09,940
Let's start off by creating a new function

62
00:04:10,330 --> 00:04:12,820
which I'm going to call format_name

63
00:04:13,390 --> 00:04:18,390
and the reason is because this function is going to take a first name and a last

64
00:04:19,810 --> 00:04:23,740
name, and it's going to change those inputs,

65
00:04:24,160 --> 00:04:27,040
even if it was lowercase or if it was all caps,

66
00:04:27,070 --> 00:04:31,270
it's going to change it into title case, something like this

67
00:04:31,570 --> 00:04:36,570
where the first letter is capitalized in each word. To begin,

68
00:04:37,030 --> 00:04:41,770
I want you to create this function and then go ahead and add two inputs to it.

69
00:04:42,250 --> 00:04:47,200
One could f_name, so first name and another code l_name,

70
00:04:47,740 --> 00:04:50,710
which is the last name. Pause the video and give that a go.

71
00:04:53,110 --> 00:04:53,410
All right.

72
00:04:53,410 --> 00:04:58,410
So all we have to do to add inputs to our function is just to put the names of

73
00:04:58,660 --> 00:05:01,840
these parameters inside the parentheses.

74
00:05:02,410 --> 00:05:07,410
So now our function is able to take these parameters as inputs

75
00:05:07,780 --> 00:05:09,460
whenever this function gets called.

76
00:05:10,630 --> 00:05:15,630
The next step is we're going to convert these strings that gets passed in,

77
00:05:16,030 --> 00:05:19,240
so it could be something like this, all lower case.

78
00:05:19,270 --> 00:05:23,410
It could be all capitalized, or it could be kind of random, right?

79
00:05:23,410 --> 00:05:26,290
Like some are capitalized, some are not.

80
00:05:26,650 --> 00:05:29,260
But we're going to make it all uniform,

81
00:05:29,260 --> 00:05:33,910
so all of these are going to end up like this. And to do this,

82
00:05:34,270 --> 00:05:39,270
we can search around in Google and you might come across a similar question in

83
00:05:39,910 --> 00:05:44,740
Stack Overflow: How do you convert a string to title case in Python?

84
00:05:45,220 --> 00:05:49,810
They have an example and it's trying to do exactly what we're trying to do.

85
00:05:50,590 --> 00:05:52,660
If we take a look at the answers,

86
00:05:52,930 --> 00:05:57,250
you can see that somebody is helpfully pointing to this title function.

87
00:05:57,680 --> 00:06:02,390
So you take a string and then you write .title and then afterwards,

88
00:06:02,390 --> 00:06:07,390
the string gets transformed and every word now starts off with a capital letter.

89
00:06:09,650 --> 00:06:12,950
And if you want to know more about how this title case works,

90
00:06:13,250 --> 00:06:17,420
then you can click on that link in title and then have a read through the Python

91
00:06:17,420 --> 00:06:21,200
documentation. Once you've read through that,

92
00:06:21,410 --> 00:06:26,410
go ahead and modify this Fname and Lname so that they end up being both in

93
00:06:28,520 --> 00:06:31,340
title case. Pause the video and give that a go.

94
00:06:33,700 --> 00:06:34,490
Right?

95
00:06:34,490 --> 00:06:34,810
All right.

96
00:06:34,810 --> 00:06:39,810
So we want to go ahead and take this Fname and then write a dot and then write

97
00:06:40,160 --> 00:06:43,660
title to convert it to title case.

98
00:06:44,410 --> 00:06:48,640
Once we've done that, then we're going to do the same with the l_name.

99
00:06:50,260 --> 00:06:53,530
And if we go ahead and actually print these out,

100
00:06:54,880 --> 00:06:57,460
and of course, in order for the function to actually execute,

101
00:06:57,760 --> 00:07:02,080
we'll have to call the function. So I'm going to put two inputs in here,

102
00:07:02,080 --> 00:07:07,000
one which is going to be, um, just angela, all in lowercase

103
00:07:07,510 --> 00:07:11,620
and then the second one is going to be ANGELA all in up a case.

104
00:07:12,190 --> 00:07:14,140
And now when I run this code,

105
00:07:14,530 --> 00:07:18,640
you'll see that the result is they both get converted to title case.

106
00:07:19,270 --> 00:07:19,960
In essence,

107
00:07:19,960 --> 00:07:24,960
this part of the code becomes whatever the previous value of f_name is converted

108
00:07:25,240 --> 00:07:30,240
to title case. You might have spotted already that this title function actually

109
00:07:31,930 --> 00:07:33,400
has an output.

110
00:07:33,730 --> 00:07:38,410
It returns a version of the string where each word is title cased.

111
00:07:39,070 --> 00:07:41,770
Instead of simply just printing this,

112
00:07:42,100 --> 00:07:44,980
we can also capture it inside a variable.

113
00:07:45,460 --> 00:07:50,460
Let's create a new variable called formatted_f_name and also

114
00:07:55,330 --> 00:07:57,490
formatted_l_name.

115
00:07:58,690 --> 00:08:03,690
Now what's happening here is we are transforming this input f_name,

116
00:08:05,680 --> 00:08:08,140
turning it into title case.

117
00:08:08,560 --> 00:08:11,530
And then once this function executes,

118
00:08:11,860 --> 00:08:16,860
it has an output and that output replaces this part of the code

119
00:08:17,590 --> 00:08:20,830
and then it gets stored inside this variable.

120
00:08:21,490 --> 00:08:26,020
Now we can go ahead and print the final version of our function

121
00:08:26,380 --> 00:08:31,380
which is going to use an fstring to take the formatted Fname and then add a

122
00:08:32,049 --> 00:08:37,049
space and then print out the formatted Lname, like this.

123
00:08:39,280 --> 00:08:43,090
Now, if I start out with my first name

124
00:08:43,120 --> 00:08:46,000
but all in strange sort of casing,

125
00:08:46,330 --> 00:08:48,730
and then my last name in all caps,

126
00:08:49,030 --> 00:08:51,580
let's go ahead and hit run and see what happens.

127
00:08:52,240 --> 00:08:55,180
You can see that my first name gets converted to title the case,

128
00:08:55,230 --> 00:08:57,600
my last name gets converted to title case

129
00:08:57,990 --> 00:09:01,320
and then that result is stored in formatted Fname

130
00:09:01,590 --> 00:09:05,130
and this result becomes stored in this Lname,

131
00:09:05,460 --> 00:09:10,200
and then they get printed and formatted, like so. Now

132
00:09:10,230 --> 00:09:13,110
instead of printing this result out,

133
00:09:13,140 --> 00:09:18,140
we could also return it as well. In the same way that this title function

134
00:09:19,650 --> 00:09:23,370
returns the output, replaces this part of the code,

135
00:09:23,670 --> 00:09:27,330
we can also do the same with our format_name function.

136
00:09:27,720 --> 00:09:32,520
So let's delete the print function and go ahead and use return

137
00:09:32,550 --> 00:09:33,383
instead.

138
00:09:34,440 --> 00:09:38,100
Now this formatted string becomes the output

139
00:09:38,520 --> 00:09:40,380
and as we mentioned before,

140
00:09:40,500 --> 00:09:45,500
what happens with functions with outputs is this output replaces the part of the

141
00:09:46,620 --> 00:09:50,190
code where the function was called. Now,

142
00:09:50,250 --> 00:09:53,280
if this is the string that we want to print,

143
00:09:53,580 --> 00:09:58,050
then all we have to do is just to save the output

144
00:09:58,110 --> 00:10:03,060
which replaces this part of the code inside a variable like this.

145
00:10:03,660 --> 00:10:08,660
And then all we have to do is just go ahead and print that formatted string.

146
00:10:10,170 --> 00:10:15,000
As you can see, when I run the code, the same thing happens. Now you can,

147
00:10:15,090 --> 00:10:17,280
and you might've alternatively,

148
00:10:17,310 --> 00:10:22,310
just taken this function call and put it straight inside the print statement.

149
00:10:22,830 --> 00:10:24,930
That works exactly the same way.

150
00:10:25,260 --> 00:10:27,870
And we can visualize this using Thonny.

151
00:10:28,350 --> 00:10:33,350
If I go ahead and paste everything we have inside the editor and go ahead and

152
00:10:34,110 --> 00:10:35,070
run debug,

153
00:10:37,410 --> 00:10:41,760
then we can step into and see what are the steps that happen.

154
00:10:42,270 --> 00:10:43,290
So the first thing

155
00:10:43,410 --> 00:10:48,410
when this line 6 gets executed is it tries to call this function, format_name.

156
00:10:50,190 --> 00:10:52,740
So it passes in these two inputs.

157
00:10:54,000 --> 00:10:59,000
And we're now within the function where we have access to these two inputs,

158
00:10:59,310 --> 00:11:00,660
Fname and Lname.

159
00:11:01,410 --> 00:11:04,320
Now the formated_f_name becomes the title-cased

160
00:11:04,320 --> 00:11:08,790
version of the first name and the same thing happens with the last name.

161
00:11:09,090 --> 00:11:12,930
So we now have a formatted_f_name and a formatted_l_name

162
00:11:13,320 --> 00:11:16,800
and that is what's going to be returned as the output.

163
00:11:18,240 --> 00:11:22,230
Now, once that's completed, that output replaces

164
00:11:22,260 --> 00:11:24,840
whatever was in between the print statement,

165
00:11:25,140 --> 00:11:27,720
which is where we called this format name function

166
00:11:28,110 --> 00:11:32,670
and this is what gets printed into the console. As I mentioned before,

167
00:11:32,670 --> 00:11:37,530
we've actually already been using functions that we didn't create that have all

168
00:11:37,530 --> 00:11:40,290
of these functionalities, right? For example,

169
00:11:40,320 --> 00:11:44,190
the Len function takes an input and has an output.

170
00:11:44,760 --> 00:11:47,370
The input is going to be a string,

171
00:11:47,730 --> 00:11:51,300
my name again, seemed to be really obsessed with my own name.

172
00:11:52,380 --> 00:11:57,160
So now this line of code is going to run this built in len function,

173
00:11:57,640 --> 00:12:01,570
calculating the number of letters inside this input that we've given it.

174
00:12:02,080 --> 00:12:04,900
And then it's going to return the result as an output,

175
00:12:05,260 --> 00:12:08,320
which we can now capture inside a variable.

176
00:12:09,040 --> 00:12:12,490
This is the inputs, this is the function

177
00:12:12,910 --> 00:12:15,190
and once this is completed,

178
00:12:15,490 --> 00:12:20,490
the return or whatever it is after the return statement is going to replace this

179
00:12:21,940 --> 00:12:23,140
part of the code.

180
00:12:23,650 --> 00:12:28,240
Then that result or output then gets stored inside of this variable.

181
00:12:29,560 --> 00:12:34,150
So this return keyword is really the most important thing in order to create a

182
00:12:34,150 --> 00:12:36,100
function that has an output,

183
00:12:36,130 --> 00:12:41,130
because everything that comes after it is going to replace where the function

184
00:12:41,170 --> 00:12:43,780
was called. In the next lesson

185
00:12:43,810 --> 00:12:48,730
we'll take a quick look at the return keyword in more detail. Specifically,

186
00:12:48,760 --> 00:12:49,840
let's see what happens

187
00:12:49,870 --> 00:12:54,490
when we have more than one return statement in the same function. All right.

188
00:12:54,700 --> 00:12:56,710
For all of that and more, I'll see you there.

