1
00:00:05,310 --> 00:00:09,630
We're going to start
diving in into functions.

2
00:00:09,630 --> 00:00:11,370
Let's see a little bit

3
00:00:11,370 --> 00:00:15,135
the anatomy of a function and
the syntax of a function.

4
00:00:15,135 --> 00:00:18,530
Here we have it. We're going

5
00:00:18,530 --> 00:00:20,690
to start a function
as you can see here,

6
00:00:20,690 --> 00:00:24,615
we will start with the
keyword D-E-F, DEF.

7
00:00:24,615 --> 00:00:26,550
We're defining a function,

8
00:00:26,550 --> 00:00:28,530
we're going to give it a name.

9
00:00:28,530 --> 00:00:29,810
In this case, as you can see,

10
00:00:29,810 --> 00:00:31,970
we're using the keyword sum,

11
00:00:31,970 --> 00:00:33,870
and then we open parentheses.

12
00:00:33,870 --> 00:00:35,610
Within the parentheses we're

13
00:00:35,610 --> 00:00:37,630
going to specify
what we would call

14
00:00:37,630 --> 00:00:42,925
the parameters or the
arguments of the function.

15
00:00:42,925 --> 00:00:45,585
These are going to be variables,

16
00:00:45,585 --> 00:00:47,790
so variables that
will be used within

17
00:00:47,790 --> 00:00:51,970
the function to execute
a particular task.

18
00:00:51,970 --> 00:00:55,835
In this case we're seeing
number 1 and number 2.

19
00:00:55,835 --> 00:00:57,630
As we go into the function,

20
00:00:57,630 --> 00:00:58,870
you will see there's

21
00:00:58,870 --> 00:01:00,640
something that is
quite invisible here,

22
00:01:00,640 --> 00:01:01,915
but it's the indentation.

23
00:01:01,915 --> 00:01:03,690
Everything that
has been indented,

24
00:01:03,690 --> 00:01:06,730
meaning that we have
a tab distance from

25
00:01:06,730 --> 00:01:08,650
the line where we start

26
00:01:08,650 --> 00:01:12,250
the definition means that
exist within the function.

27
00:01:12,250 --> 00:01:14,790
Other programming
languages actually use

28
00:01:14,790 --> 00:01:18,910
a curly bracket kind

29
00:01:18,910 --> 00:01:22,310
of separator to identify what
belongs to the function.

30
00:01:22,310 --> 00:01:25,590
But within Python, we are
actually using indentation,

31
00:01:25,590 --> 00:01:27,490
meaning that this
distance is important

32
00:01:27,490 --> 00:01:30,400
to identify what belongs
to the function.

33
00:01:30,530 --> 00:01:33,060
Let's read the first line.

34
00:01:33,060 --> 00:01:37,585
The first line says
print number 1,

35
00:01:37,585 --> 00:01:39,660
and that's a string,

36
00:01:39,660 --> 00:01:41,680
meaning that it's a text.

37
00:01:41,680 --> 00:01:45,135
Then comma number 1.

38
00:01:45,135 --> 00:01:49,300
The variable that
is obtained from

39
00:01:49,300 --> 00:01:51,600
the argument or the
parameter print number

40
00:01:51,600 --> 00:01:54,500
2 and then the
number 2 variable.

41
00:01:54,500 --> 00:01:56,440
Then the addition,
the expression is

42
00:01:56,440 --> 00:02:00,480
addition equals sum1 plus sum2

43
00:02:00,480 --> 00:02:02,970
and return it to
keyword that allows

44
00:02:02,970 --> 00:02:07,110
the function once it's executed,

45
00:02:07,110 --> 00:02:09,980
to create a new value and

46
00:02:09,980 --> 00:02:14,240
return that value
wherever we're using it.

47
00:02:14,640 --> 00:02:17,365
This might all sound
a bit confusing,

48
00:02:17,365 --> 00:02:19,320
we're going to see an example of

49
00:02:19,320 --> 00:02:21,480
this code together when

50
00:02:21,480 --> 00:02:23,320
we're going to be
breaking this down.

51
00:02:23,320 --> 00:02:27,320
Most perhaps concerning
thing here might be like,

52
00:02:27,320 --> 00:02:29,000
well where is the
data really like?

53
00:02:29,000 --> 00:02:32,920
I don't see at any point
here any information,

54
00:02:32,920 --> 00:02:36,610
actual numbers that I'm
using to do this addition.

55
00:02:36,610 --> 00:02:38,940
But we have to assume that

56
00:02:38,940 --> 00:02:40,660
once the function gets executed,

57
00:02:40,660 --> 00:02:42,960
meaning that at some other
point in the program,

58
00:02:42,960 --> 00:02:44,320
we're going to
call the function.

59
00:02:44,320 --> 00:02:47,060
We're going to execute that
function at that point,

60
00:02:47,060 --> 00:02:50,280
instead of providing variables
as we're doing here,

61
00:02:50,280 --> 00:02:52,250
we will provide actual data.

62
00:02:52,250 --> 00:02:56,715
The function is
using a placeholder,

63
00:02:56,715 --> 00:02:58,980
this number 1, and number
2 are variables that

64
00:02:58,980 --> 00:03:02,000
operate as placeholders
to do an operation.

65
00:03:02,000 --> 00:03:04,000
But that information
will be provided

66
00:03:04,000 --> 00:03:06,420
once we execute the function.

67
00:03:06,420 --> 00:03:08,910
Let's see how we would
execute that function.

68
00:03:08,910 --> 00:03:11,460
Here's the version of
that, much simpler,

69
00:03:11,460 --> 00:03:13,200
certainly, the execution of

70
00:03:13,200 --> 00:03:16,200
the function is the
condensed version of that.

71
00:03:16,200 --> 00:03:18,100
We use the keyword that

72
00:03:18,100 --> 00:03:19,720
we identify as the name

73
00:03:19,720 --> 00:03:21,875
of the function,
in this case some.

74
00:03:21,875 --> 00:03:23,970
We provide the argument.

75
00:03:23,970 --> 00:03:26,860
Here we're actually providing
a five comma and eight

76
00:03:26,860 --> 00:03:28,760
which will represent number 1

77
00:03:28,760 --> 00:03:31,090
and number 2 in the function.

78
00:03:31,090 --> 00:03:34,900
Together with getting a
print into the console,

79
00:03:34,900 --> 00:03:38,425
we will get the
return result of 13.

80
00:03:38,425 --> 00:03:41,480
We can actually use
this sum function

81
00:03:41,480 --> 00:03:43,640
with different arguments,
with different values,

82
00:03:43,640 --> 00:03:46,300
and executed many times
within our code without

83
00:03:46,300 --> 00:03:49,715
having to repeat the
definition of the function.

84
00:03:49,715 --> 00:03:52,000
Let me just go back to
the previous slide.

85
00:03:52,000 --> 00:03:53,160
As you can see here, this is

86
00:03:53,160 --> 00:03:55,000
the definition of the function,

87
00:03:55,000 --> 00:03:56,840
and the execution of

88
00:03:56,840 --> 00:04:01,180
the function takes this
more succinct smaller form.

89
00:04:01,370 --> 00:04:07,180
One small caveat is
that within Python,

90
00:04:07,180 --> 00:04:10,660
and specifically also within
the processing environment,

91
00:04:10,660 --> 00:04:12,240
which is what we're working on,

92
00:04:12,240 --> 00:04:15,415
there are already some
inbuilt functions.

93
00:04:15,415 --> 00:04:21,250
Things that if you basically
you type this word,

94
00:04:21,250 --> 00:04:23,100
it would switch color within

95
00:04:23,100 --> 00:04:25,080
the editor and we
will be able to

96
00:04:25,080 --> 00:04:26,380
identify that there is

97
00:04:26,380 --> 00:04:29,025
a function associated
with that keyword.

98
00:04:29,025 --> 00:04:31,340
You might have to avoid
some of these keywords,

99
00:04:31,340 --> 00:04:33,960
and if you see again the
text change in color,

100
00:04:33,960 --> 00:04:35,580
that means that maybe
there's something associated

101
00:04:35,580 --> 00:04:37,345
with that, that
could be a function.

102
00:04:37,345 --> 00:04:39,830
Many times something
like the function

103
00:04:39,830 --> 00:04:42,305
float or function int,

104
00:04:42,305 --> 00:04:44,110
which are functions specifically

105
00:04:44,110 --> 00:04:46,430
constructed for you
to convert data

106
00:04:46,430 --> 00:04:48,425
into those specific
variable types

107
00:04:48,425 --> 00:04:50,980
are variables that are
in-built into the language.

108
00:04:50,980 --> 00:04:54,710
The length of a list ways
of sorting information.

109
00:04:54,710 --> 00:04:56,870
A series of functions
that we will be

110
00:04:56,870 --> 00:04:59,530
learning gradually as we
go within this course.

111
00:04:59,530 --> 00:05:02,190
But for now you have
to know that there's

112
00:05:02,190 --> 00:05:03,370
some keywords that are

113
00:05:03,370 --> 00:05:05,210
already in built
into the language.

114
00:05:05,210 --> 00:05:07,635
Let's jump into the code.

115
00:05:07,635 --> 00:05:12,970
Here we go. Let's write header.

116
00:05:21,040 --> 00:05:23,540
We're going to
define our function

117
00:05:23,540 --> 00:05:27,845
and let's just print_sum.

118
00:05:27,845 --> 00:05:29,855
I'm going to call this
function slightly different

119
00:05:29,855 --> 00:05:31,985
of what we saw in
the example before.

120
00:05:31,985 --> 00:05:35,720
I'm going to call it
the print_sum so it'll

121
00:05:35,720 --> 00:05:40,415
print the result of an
addition operation.

122
00:05:40,415 --> 00:05:44,660
Let's open parentheses to
provide arguments here or the

123
00:05:44,660 --> 00:05:46,490
parameters that are part
of the function that

124
00:05:46,490 --> 00:05:49,100
ultimately will be the argument,

125
00:05:49,100 --> 00:05:51,845
so x and y.

126
00:05:51,845 --> 00:05:54,275
Notice that I'm going
to use the column

127
00:05:54,275 --> 00:05:55,955
here, this is a keyword.

128
00:05:55,955 --> 00:05:58,595
When I use the column, you
see if I press "Enter",

129
00:05:58,595 --> 00:06:00,710
the software
automatically understands

130
00:06:00,710 --> 00:06:02,240
that I'm within the function.

131
00:06:02,240 --> 00:06:03,770
The indentation
that we were just

132
00:06:03,770 --> 00:06:07,175
discussing is actually
automatically calculated for us,

133
00:06:07,175 --> 00:06:10,385
we don't have to be pressing
"Space" many times.

134
00:06:10,385 --> 00:06:13,430
If for whatever reason we press

135
00:06:13,430 --> 00:06:17,105
"Delete" and we move back
from that indentation,

136
00:06:17,105 --> 00:06:20,180
we can press "Tab"
again and we will be at

137
00:06:20,180 --> 00:06:21,260
the right indentation that is

138
00:06:21,260 --> 00:06:24,545
expected to be for
this function to work.

139
00:06:24,545 --> 00:06:27,485
Let's do a few print commands.

140
00:06:27,485 --> 00:06:33,360
Let's print input one.

141
00:06:34,270 --> 00:06:37,715
When you can print a string,

142
00:06:37,715 --> 00:06:41,000
and let's add to that string.

143
00:06:41,000 --> 00:06:43,970
This is maybe a little
bit strange because we're

144
00:06:43,970 --> 00:06:46,760
actually adding a
string to a text.

145
00:06:46,760 --> 00:06:48,290
We're adding a number.

146
00:06:48,290 --> 00:06:50,150
If we add a number, we
will have a problem.

147
00:06:50,150 --> 00:06:55,550
We can add strings together
by adding two pieces of text,

148
00:06:55,550 --> 00:06:58,550
and we will end up
with the concatenation

149
00:06:58,550 --> 00:07:01,220
or joining those texts together.

150
00:07:01,220 --> 00:07:04,145
In order to do that,
we have to convert

151
00:07:04,145 --> 00:07:06,620
our x variable and
we do that with

152
00:07:06,620 --> 00:07:10,115
a function called STR(x).

153
00:07:10,115 --> 00:07:12,095
We're converting the x value,

154
00:07:12,095 --> 00:07:14,690
which will most
likely be an integer.

155
00:07:14,690 --> 00:07:16,790
We're expecting it
to be an integer.

156
00:07:16,790 --> 00:07:18,710
We're going to convert
it into a string

157
00:07:18,710 --> 00:07:19,940
or we're going to
make sure that it's

158
00:07:19,940 --> 00:07:26,105
a string and that is the
print command number 1.

159
00:07:26,105 --> 00:07:28,520
Let's do another one of those,

160
00:07:28,520 --> 00:07:30,380
and you can see we're still

161
00:07:30,380 --> 00:07:33,470
within the indentation of
the function so that means

162
00:07:33,470 --> 00:07:36,410
that a series of lines

163
00:07:36,410 --> 00:07:40,055
will be executed every time
that we execute the function.

164
00:07:40,055 --> 00:07:43,530
In this case, input number 2.

165
00:07:43,870 --> 00:07:47,660
I'm just copy-pasting the
line because it's not

166
00:07:47,660 --> 00:07:51,545
necessary that I write those
lines over and over again.

167
00:07:51,545 --> 00:07:54,960
I'm just going to
change the x for y.

168
00:07:55,840 --> 00:07:59,645
Similarly, we will have
two print commands.

169
00:07:59,645 --> 00:08:01,355
I want to make sure
what is input one,

170
00:08:01,355 --> 00:08:02,765
what is input two,

171
00:08:02,765 --> 00:08:04,685
and then I also want to

172
00:08:04,685 --> 00:08:06,920
calculate the summation
or the addition

173
00:08:06,920 --> 00:08:10,970
between x and y and finally,

174
00:08:10,970 --> 00:08:13,460
do a final print.

175
00:08:13,460 --> 00:08:15,905
Once more I'm going to

176
00:08:15,905 --> 00:08:20,375
copy-paste that
line and this time,

177
00:08:20,375 --> 00:08:25,350
let's just delete all this
here and type result.

178
00:08:26,650 --> 00:08:30,860
The result we're
going to convert into

179
00:08:30,860 --> 00:08:37,760
a string value of
some expression.

180
00:08:37,760 --> 00:08:41,105
That's great. We actually
finished the function.

181
00:08:41,105 --> 00:08:44,420
We have completed our program.

182
00:08:44,420 --> 00:08:45,950
Let's press "Play",

183
00:08:45,950 --> 00:08:49,020
and you'll see that,
well, nothing happens.

184
00:08:49,120 --> 00:08:52,325
What's wrong here?
What's going on?

185
00:08:52,325 --> 00:08:54,125
As we just described,

186
00:08:54,125 --> 00:08:57,440
this definition of a function
will not necessarily be

187
00:08:57,440 --> 00:09:01,580
executed unless we implement
or execute the function.

188
00:09:01,580 --> 00:09:03,410
In order to do this,

189
00:09:03,410 --> 00:09:05,975
we have to use the
name of the function.

190
00:09:05,975 --> 00:09:08,795
We're going to that here
outside the indentation.

191
00:09:08,795 --> 00:09:10,640
Let's make sure
that we're doing it

192
00:09:10,640 --> 00:09:14,120
starting from the
very edge here.

193
00:09:14,120 --> 00:09:16,055
Open parentheses.

194
00:09:16,055 --> 00:09:17,720
Here, we're going to
provide information,

195
00:09:17,720 --> 00:09:23,525
so 2,3, so this
will represent x,

196
00:09:23,525 --> 00:09:25,715
and three will represent y.

197
00:09:25,715 --> 00:09:32,210
We're executing the function
just with this execution.

198
00:09:32,210 --> 00:09:35,405
Let's see. What do we get?

199
00:09:35,405 --> 00:09:38,240
We could try to expand

200
00:09:38,240 --> 00:09:41,585
this area here and
we get input one,

201
00:09:41,585 --> 00:09:43,535
it's two, input two is three,

202
00:09:43,535 --> 00:09:46,875
and the result is five.

203
00:09:46,875 --> 00:09:50,155
Can I call this
function once more?

204
00:09:50,155 --> 00:10:00,100
Well, it's executed maybe
another time with 6,20.

205
00:10:02,380 --> 00:10:05,285
As you can see sequentially,

206
00:10:05,285 --> 00:10:06,650
we have the print

207
00:10:06,650 --> 00:10:09,215
for the first execution
of the function,

208
00:10:09,215 --> 00:10:12,450
the print for the second
execution of the function.

209
00:10:12,520 --> 00:10:15,230
Again, this is what gives

210
00:10:15,230 --> 00:10:16,940
us the foundation of modularity.

211
00:10:16,940 --> 00:10:19,520
We do not have to repeat
this piece of code.

212
00:10:19,520 --> 00:10:21,875
This is a function
that now we can use

213
00:10:21,875 --> 00:10:25,355
in many places in our program.

214
00:10:25,355 --> 00:10:28,010
If we feel that there's a
particular part of code that

215
00:10:28,010 --> 00:10:30,410
we are needing over
and over again,

216
00:10:30,410 --> 00:10:31,990
it might be worth considering

217
00:10:31,990 --> 00:10:33,110
making a function out of it.

218
00:10:33,110 --> 00:10:35,330
I hope that's clear
and we're going to

219
00:10:35,330 --> 00:10:38,670
continue using functions along
the way with this class.

220
00:10:38,670 --> 00:10:41,510
But I'll see you in
the next lesson.