1
00:00:02,000 --> 00:00:04,390
Hey everybody top of them muffin to you today.

2
00:00:04,420 --> 00:00:06,660
Mark Price at the slopes dot com.

3
00:00:06,670 --> 00:00:11,230
And today we are going to learn about functions.

4
00:00:11,290 --> 00:00:16,360
Open up your X code 8 and create a new playground.

5
00:00:16,360 --> 00:00:21,430
My playground we're going to call this up town function you

6
00:00:24,340 --> 00:00:33,280
and I'm going to save this where I'm going to save this Iowas 10 course and swift and new folder functions

7
00:00:33,280 --> 00:00:36,140
you can save yours where ever you want.

8
00:00:37,260 --> 00:00:37,820
OK.

9
00:00:38,080 --> 00:00:47,920
So a function is a core component in programming core building block building block in building functional

10
00:00:47,920 --> 00:00:50,710
applications applications that can actually do things.

11
00:00:50,730 --> 00:00:56,020
OK so here's a perfect example of why we need to use a function.

12
00:00:56,080 --> 00:01:00,560
Let's say that I want to get the area of a shape or a rectangle or square.

13
00:01:00,580 --> 00:01:13,360
OK so say bar length equals five bar with equals and say 10 and then bar area equals length times with.

14
00:01:13,360 --> 00:01:15,160
Now you've probably done something similar to this.

15
00:01:15,190 --> 00:01:16,770
Written wrote some variables.

16
00:01:16,770 --> 00:01:17,680
That's math operation.

17
00:01:17,670 --> 00:01:19,110
We now have the area right.

18
00:01:19,570 --> 00:01:21,310
Well this is cool.

19
00:01:21,370 --> 00:01:23,880
Maybe you don't see what's wrong with this.

20
00:01:24,040 --> 00:01:25,980
Well let's do this now.

21
00:01:25,990 --> 00:01:27,230
Now we have a new shape.

22
00:01:27,310 --> 00:01:29,580
So this was shape number one.

23
00:01:30,220 --> 00:01:30,910
All right.

24
00:01:31,220 --> 00:01:31,840
Oh no.

25
00:01:31,870 --> 00:01:33,220
We've got a new shape now.

26
00:01:33,460 --> 00:01:33,850
OK.

27
00:01:33,850 --> 00:01:41,110
It's another rectangle with a different size so now we're going to say OK length two equals six var

28
00:01:41,350 --> 00:01:51,270
with two equals 12 and var area two equals length two times with two.

29
00:01:51,830 --> 00:01:52,220
OK.

30
00:01:52,240 --> 00:01:53,230
Well that's not so bad.

31
00:01:53,410 --> 00:01:58,640
So this is a shape too but oh no.

32
00:01:59,080 --> 00:02:02,980
Now our program requires a third shape shape 3.

33
00:02:03,160 --> 00:02:06,220
And if you're asking me Is it really going to type this out again.

34
00:02:06,220 --> 00:02:13,130
The answer is yes because I want you to see the brutal pain of not working with function Savar length

35
00:02:13,130 --> 00:02:28,330
three equals three are with three equals eight and bar area 3 equals 0 length 3 times with 3 K.

36
00:02:28,540 --> 00:02:30,190
This is lame.

37
00:02:30,190 --> 00:02:31,370
This is really lame.

38
00:02:31,460 --> 00:02:35,020
And anybody who programming can probably realize that this is not the best way.

39
00:02:35,020 --> 00:02:38,320
You don't have to be an advanced programmer to realize something smells wrong.

40
00:02:38,320 --> 00:02:43,720
This is called a code smell a code smells when something looks or smells wrong with your code.

41
00:02:43,960 --> 00:02:46,660
You know maybe don't know what it is but it just doesn't feel right.

42
00:02:46,760 --> 00:02:48,600
Well this violates a principle.

43
00:02:48,610 --> 00:02:51,470
It violates the dry principle which means don't repeat yourself.

44
00:02:51,490 --> 00:02:53,900
And we are definitely repeating ourself here.

45
00:02:54,220 --> 00:03:02,380
So this is how functions can come in handy functions are a way to perform an operation over and over.

46
00:03:02,380 --> 00:03:04,600
You can pass data into it return things.

47
00:03:04,600 --> 00:03:09,730
In fact let's see what our handy dandy internet says about functions.

48
00:03:09,940 --> 00:03:16,470
Define function in programming in computer science.

49
00:03:16,510 --> 00:03:20,610
Functional Programming is a programming paradigm a style of building the structure and elements of a

50
00:03:20,610 --> 00:03:26,650
computer program that treats computation as the evaluation of mathematical functions and avoids changing

51
00:03:26,650 --> 00:03:28,730
state and mutable data.

52
00:03:28,810 --> 00:03:30,880
So this is functional programming.

53
00:03:30,900 --> 00:03:37,480
It's not functions in general but there actually is some merit to functional programming functional

54
00:03:37,480 --> 00:03:44,380
programming basically means you want to create functions that can repeat operations in a mathematical

55
00:03:44,380 --> 00:03:48,340
sense and don't do a whole bunch of things and manipulate all kinds of data.

56
00:03:48,370 --> 00:03:55,230
They do one thing and they do it well and so that's kind of how you should be designing functions anyway.

57
00:03:56,020 --> 00:03:58,330
But that's not exactly what we're looking for.

58
00:03:58,330 --> 00:04:03,340
Let's go over to Wikipedia and if you're wondering why does Mark Price always pull up documentation

59
00:04:03,350 --> 00:04:06,700
always go to these things because you should not be just copying and pasting code.

60
00:04:06,700 --> 00:04:11,800
I'm going to teach you how to become a programmer and programmers the good programmers always learn

61
00:04:11,980 --> 00:04:15,420
they read the documentation they go underneath the hood and that's what we're going to do here.

62
00:04:15,490 --> 00:04:21,310
And if you don't like it just put me on to x speed and you can listen to me talk really fast just like

63
00:04:21,310 --> 00:04:21,710
this.

64
00:04:21,820 --> 00:04:26,740
OK so let's take a side in a function OK.

65
00:04:27,190 --> 00:04:28,030
And

66
00:04:32,070 --> 00:04:33,540
this is a good one too.

67
00:04:33,540 --> 00:04:38,400
A function is a relation between a set of inputs and a set of permissible outputs with the property

68
00:04:38,640 --> 00:04:41,640
that each input is related to exactly one output.

69
00:04:41,640 --> 00:04:48,210
This is really cool how does the word function it translates into different things in math but it all

70
00:04:48,210 --> 00:04:49,860
makes those very similar.

71
00:04:49,860 --> 00:04:51,540
Look at this diagram right here.

72
00:04:51,600 --> 00:04:58,260
A function takes input and it can also provide output doesn't always have to though and this can be

73
00:04:58,260 --> 00:04:59,880
the same thing in programming.

74
00:05:00,230 --> 00:05:05,430
And I would encourage you to do some more searches on an functional math functional programming.

75
00:05:05,430 --> 00:05:06,870
What is a function.

76
00:05:07,200 --> 00:05:11,400
Really good idea of what's going on and I'm going to show you hands on now.

77
00:05:11,520 --> 00:05:14,990
OK we just took a glance at some of the things that are on the Internet.

78
00:05:14,990 --> 00:05:18,020
It's always fun to do that to start educating yourself.

79
00:05:18,200 --> 00:05:23,140
And so what I want you to do is create a function just like I'm going to do.

80
00:05:23,160 --> 00:05:25,000
Func is how you do it in swift.

81
00:05:26,220 --> 00:05:29,810
And we're going to call this calculate area.

82
00:05:30,410 --> 00:05:33,730
OK so we have a function and it's going to do something.

83
00:05:33,750 --> 00:05:41,640
So what we're going to do is we're going to put in two parameters and we're going to say length of time.

84
00:05:41,810 --> 00:05:45,590
This can just be an int and width of type.

85
00:05:46,050 --> 00:05:47,330
So this is the input.

86
00:05:47,370 --> 00:05:51,150
Just like in that diagram that you saw or something go into the function then it does something and

87
00:05:51,150 --> 00:05:52,220
something can come out.

88
00:05:52,470 --> 00:05:54,320
We're can do that exact same thing here.

89
00:05:54,400 --> 00:06:00,630
OK very similar to what you might do in some paper math and length and width and so what's going to

90
00:06:00,630 --> 00:06:04,620
happen is we're going to pass in the length and passing it with it's going to calculate it and it's

91
00:06:04,620 --> 00:06:07,450
going to return it's going to give us back of value.

92
00:06:07,530 --> 00:06:09,870
So this is the syntax for a function.

93
00:06:09,900 --> 00:06:11,670
You start off with the func keyword.

94
00:06:11,850 --> 00:06:16,260
OK and then you give it a name and then you create parentheses.

95
00:06:16,470 --> 00:06:19,520
OK you don't have to have parameters in.

96
00:06:19,860 --> 00:06:25,930
It can be OK that it's just not doing what I want it to do.

97
00:06:25,980 --> 00:06:31,450
You don't have to print this or stuff in the parentheses it can be empty like that.

98
00:06:32,140 --> 00:06:39,780
But good programming or the functional programming that you want to start gravitating towards takes

99
00:06:39,810 --> 00:06:46,170
inputs and returns output without necessarily modifying what you would call your variables or your properties.

100
00:06:46,260 --> 00:06:50,430
And if this doesn't make any sense to set that's absolutely fine because when you watch this video again

101
00:06:50,670 --> 00:06:53,490
you probably should start making more sense.

102
00:06:53,580 --> 00:06:58,230
So we're going to pass in a length passen with these are parameters which means you can put things in

103
00:06:58,230 --> 00:07:04,860
them and then what we're going to do is we're going to put a dash and then a greater than sign.

104
00:07:04,860 --> 00:07:06,360
Kind of like an arrow right.

105
00:07:06,360 --> 00:07:12,880
This is what you're going to return what data type Do we want to return where we want to return an int.

106
00:07:13,350 --> 00:07:14,760
When we say let.

107
00:07:14,750 --> 00:07:21,180
When a critic constant area equals length times with no that's just a simple math operation right.

108
00:07:21,610 --> 00:07:23,410
Length times with.

109
00:07:23,410 --> 00:07:24,670
But then what we're going to do.

110
00:07:24,670 --> 00:07:27,910
We're going to return the area.

111
00:07:27,910 --> 00:07:29,560
So what does this all mean.

112
00:07:29,560 --> 00:07:32,110
OK what's going on here.

113
00:07:32,290 --> 00:07:37,620
Well you don't know because we haven't put it into action yet so what we're going to do is we're going

114
00:07:37,630 --> 00:07:40,680
to say var actual say let.

115
00:07:41,350 --> 00:07:45,580
Area equals calculate area so we're going to call our function.

116
00:07:45,580 --> 00:07:47,980
Notice how it's appearing here on the autocomplete.

117
00:07:48,010 --> 00:07:49,090
It is appearing it's blue.

118
00:07:49,090 --> 00:07:50,110
It has our parameters in it.

119
00:07:50,110 --> 00:07:57,450
This is great so we're going to say calculate area and we're going to pass in a length and width.

120
00:07:57,460 --> 00:08:07,140
So let's say 5 and then with this for k Kukla air with five and four.

121
00:08:07,200 --> 00:08:10,620
And we already have an area means that it doesn't like that because we already have an area I'm going

122
00:08:10,620 --> 00:08:13,220
to call this new area.

123
00:08:13,410 --> 00:08:14,040
OK.

124
00:08:14,270 --> 00:08:17,310
So let new area calculate area and look at this.

125
00:08:17,340 --> 00:08:21,680
It's giving us the proper result of what is happening here what is going on.

126
00:08:21,870 --> 00:08:26,550
Well let's see if I can show you.

127
00:08:26,550 --> 00:08:29,430
So we're calling a function or passing in a 5.

128
00:08:29,430 --> 00:08:35,970
So the second that we pass it to 5 it's taking that and dropping it here in this bucket and calling

129
00:08:36,060 --> 00:08:39,660
it length so it's taken this value in putting it in a new variable called length.

130
00:08:39,660 --> 00:08:44,250
Actually this is a constant you can't change these values OK once they're set in there and then it's

131
00:08:44,250 --> 00:08:48,160
taking this one and it's passing it in here and giving it a name.

132
00:08:48,240 --> 00:08:51,940
So the values you pass in it gives it a new name puts it in a new constant.

133
00:08:51,960 --> 00:08:56,670
You can use inside of your function and then we take the length and the width and it performs a math

134
00:08:56,670 --> 00:09:02,680
operation and stores the result here in this constant and then using the return keyword.

135
00:09:02,860 --> 00:09:03,640
OK.

136
00:09:03,690 --> 00:09:04,620
We actually return something.

137
00:09:04,620 --> 00:09:09,310
So what happens is this is called and then goes right into here.

138
00:09:09,930 --> 00:09:17,220
OK so we pass in the length we pass in the with it performs an operation here and then it returns that

139
00:09:17,220 --> 00:09:21,840
operation or the value of the operation it returns here into our variable.

140
00:09:21,840 --> 00:09:23,140
So now we can use that.

141
00:09:23,460 --> 00:09:26,040
And this is really really cool.

142
00:09:26,170 --> 00:09:28,110
And why is this cool.

143
00:09:28,470 --> 00:09:32,300
Because we don't have to write all this crazy code now pay.

144
00:09:32,370 --> 00:09:32,950
We don't.

145
00:09:33,030 --> 00:09:36,480
The more you have to write code the more chances you have of screwing up.

146
00:09:36,480 --> 00:09:39,980
So what if I was writing the third area here and actually did a minus sign.

147
00:09:39,990 --> 00:09:41,510
Well that's no longer the area is it.

148
00:09:41,550 --> 00:09:46,040
I just broke our code because I wasn't paying attention maybe a sneeze or whatever.

149
00:09:46,200 --> 00:09:51,660
I had to write more code and it created more possibilities for mistakes.

150
00:09:51,660 --> 00:10:01,450
So now if I want to get three different shapes that let's just call this shape one OK let shaped two

151
00:10:01,910 --> 00:10:07,680
we can just now call the function and all the code inside that program just crash.

152
00:10:07,680 --> 00:10:08,910
But all the code inside

153
00:10:11,580 --> 00:10:12,760
you don't ever have to write again.

154
00:10:12,810 --> 00:10:25,500
And that's the beauty of functions open recent.

155
00:10:25,570 --> 00:10:33,380
Now we go so calculate area and this and we're going to say 6 and 2.

156
00:10:33,850 --> 00:10:35,290
OK.

157
00:10:35,290 --> 00:10:36,180
Cool stuff.

158
00:10:36,190 --> 00:10:45,490
So now we've only written the code once inside and it's been used multiple times.

159
00:10:45,490 --> 00:10:48,690
So you're already starting to see the value of a function.

160
00:10:48,700 --> 00:10:56,320
It lets you take input perform operations mathematical or other operations on that input and returning

161
00:10:56,320 --> 00:10:59,580
the result of that so somebody can use it and this is a nice clean function.

162
00:10:59,980 --> 00:11:04,870
And if we really want to clean things up and save some space we don't even have to store this in a constant

163
00:11:04,870 --> 00:11:05,260
like this.

164
00:11:05,260 --> 00:11:14,710
We can actually just say return length times with like so and it's going to perform the operation and

165
00:11:14,720 --> 00:11:16,910
then it's going to return automatically.

166
00:11:16,960 --> 00:11:18,410
And so we don't have to change anything else.

167
00:11:18,420 --> 00:11:22,780
Behere we just cleaned up a little bit of code and this is more common for simple operations.

168
00:11:22,780 --> 00:11:26,980
If you have multiple multi-line operations you have to do then of course stored in the content like

169
00:11:26,980 --> 00:11:28,150
we had before.

170
00:11:28,150 --> 00:11:32,990
So again you take values you pass it into these parameters here also.

171
00:11:33,010 --> 00:11:33,920
That's the keyword.

172
00:11:34,120 --> 00:11:34,770
OK.

173
00:11:34,930 --> 00:11:37,180
Write this down if you're new to programming write this down.

174
00:11:37,180 --> 00:11:39,640
These are parameters also known as arguments.

175
00:11:39,640 --> 00:11:42,140
Some nice people use those terms interchangeably.

176
00:11:42,250 --> 00:11:48,280
Also another thing to write down functions are called functions in some languages and methods in other

177
00:11:48,280 --> 00:11:48,920
languages.

178
00:11:48,930 --> 00:11:52,100
OK very important you might write that keyword down as well too.

179
00:11:52,330 --> 00:11:57,310
Over here on the right hand side is the return statement and executing that return statement the return

180
00:11:57,310 --> 00:11:58,060
type.

181
00:11:58,060 --> 00:12:00,870
OK this is where you specify the return type.

182
00:12:01,120 --> 00:12:04,780
And this is the type here dash greater then sign like an arrow.

183
00:12:05,170 --> 00:12:11,110
And then if you are returning something you must use the return keyword to return a value of that type.

184
00:12:11,110 --> 00:12:17,690
Notice how we couldn't return a string if we wanted to I couldn't say hey you let's try that.

185
00:12:19,690 --> 00:12:24,820
Cannot convert return expression of type string to return type and it must be an integer.

186
00:12:25,030 --> 00:12:26,700
OK this is no bueno.

187
00:12:26,830 --> 00:12:27,630
No good.

188
00:12:28,020 --> 00:12:28,540
OK.

189
00:12:30,320 --> 00:12:36,280
And so we've already saved ourselves some errors by using a function here which is pretty cool and you've

190
00:12:36,280 --> 00:12:40,600
just learned really the core features of a function of what you would use it for and these are used

191
00:12:40,600 --> 00:12:41,950
for everything.

192
00:12:41,950 --> 00:12:43,180
Anything you can think of.

193
00:12:43,180 --> 00:12:46,370
For instance let's talk about bank accounts.

194
00:12:46,720 --> 00:12:59,750
So let's say var say bank account balance equals five hundred zero zero which is a double.

195
00:13:00,300 --> 00:13:01,160
OK.

196
00:13:01,210 --> 00:13:07,020
And then what we do is we say var what you want to.

197
00:13:07,080 --> 00:13:08,600
What do we want to buy something cool.

198
00:13:08,620 --> 00:13:09,240
So yeah.

199
00:13:09,250 --> 00:13:16,870
So the other day get this the day Reebok announces the release of you know the alien Stomper shoes the

200
00:13:16,870 --> 00:13:22,960
shoes that Sigourney Weaver wore in the alien aliens movie.

201
00:13:22,960 --> 00:13:23,490
OK.

202
00:13:23,500 --> 00:13:25,050
And also the Android war of them.

203
00:13:25,480 --> 00:13:27,790
You know 30 years later they're rereleased in the shoes right.

204
00:13:27,790 --> 00:13:29,450
I was all excited ready for it.

205
00:13:29,530 --> 00:13:32,460
Went on the one onto the Web site the second and went live.

206
00:13:32,500 --> 00:13:34,940
And then guess what happened because there's only 2000 shoes.

207
00:13:35,230 --> 00:13:38,270
A bunch of nerds that should have been me because I'm a nerd.

208
00:13:38,370 --> 00:13:43,150
Many nerds with their bots bought up every shoe and then sold them on eBay for like 10 times the amount

209
00:13:43,150 --> 00:13:43,770
of money.

210
00:13:44,230 --> 00:13:45,990
That's all irrelevant.

211
00:13:46,000 --> 00:13:48,700
So my point is that's what we're going to buy today is

212
00:13:52,770 --> 00:14:00,070
you know we're going to buy Sigourney Weaver Alien Stomper shoes.

213
00:14:00,070 --> 00:14:03,610
OK you got $350 now.

214
00:14:03,670 --> 00:14:05,310
She's OK.

215
00:14:05,330 --> 00:14:07,240
So we want to buy these items right.

216
00:14:07,610 --> 00:14:15,350
So you know we could write out some logic here that says you know if you have enough money buy the shoes

217
00:14:15,380 --> 00:14:18,480
and then do another item but what if we had a function that could just purchase an item.

218
00:14:18,580 --> 00:14:19,210
OK.

219
00:14:19,370 --> 00:14:20,540
So let's do that now.

220
00:14:20,720 --> 00:14:24,520
Funk where you say purchase item.

221
00:14:25,610 --> 00:14:26,470
OK.

222
00:14:26,570 --> 00:14:37,200
And what we're going to do purchase item we're going to pass in the current balance type double.

223
00:14:37,310 --> 00:14:41,010
And the item price of type double.

224
00:14:41,260 --> 00:14:42,190
Okay.

225
00:14:42,470 --> 00:14:45,930
And I'll come back to the return statement later.

226
00:14:46,410 --> 00:14:55,090
What we're going to do is say if item price is less than or equal to current balance.

227
00:14:55,590 --> 00:14:55,880
Okay

228
00:14:59,880 --> 00:15:01,220
that's what I'm looking for.

229
00:15:01,220 --> 00:15:03,460
Well we're going to we're going to return.

230
00:15:03,890 --> 00:15:07,250
We're going to return the new balance that you have because you just purchased the items.

231
00:15:07,250 --> 00:15:09,210
Maybe we'll print something too just for fun.

232
00:15:09,230 --> 00:15:14,990
But anyway this is generic It doesn't matter if it's Sigourney Weaver Alien shoes or if it's you know

233
00:15:15,380 --> 00:15:19,310
Teletubbies limited edition lunchpail box.

234
00:15:19,310 --> 00:15:20,370
OK.

235
00:15:20,760 --> 00:15:22,850
From Nineteen ninety eight.

236
00:15:22,890 --> 00:15:29,870
And anyway if item price is less than or equal to current balance you guys are like Do you even hear

237
00:15:29,870 --> 00:15:31,230
the words come out of your mouth Mark.

238
00:15:31,340 --> 00:15:31,700
No.

239
00:15:31,700 --> 00:15:33,880
The answer is No I just say whatever's there.

240
00:15:34,220 --> 00:15:42,590
So now what we can do is return if the prices aren't equal to current balance we can return current

241
00:15:42,590 --> 00:15:47,270
balance minus item price returned the New Balance.

242
00:15:47,280 --> 00:15:47,960
Okay.

243
00:15:48,310 --> 00:15:50,910
And then let's make a return statement.

244
00:15:50,990 --> 00:15:53,620
We're going to make this type double OK.

245
00:15:55,040 --> 00:16:00,900
Like so and then what we do is we're going to say if you can't.

246
00:16:01,040 --> 00:16:07,150
So if it's not less than or equal to then we're going to just return the current balance as it is.

247
00:16:07,170 --> 00:16:08,670
OK.

248
00:16:08,910 --> 00:16:20,500
And what we can do here is say print purchased item for and we can use string interpolation which is

249
00:16:20,500 --> 00:16:22,170
a key word you should have memorized.

250
00:16:22,490 --> 00:16:28,480
Purchased item for item price and we can even put a little dollar sign in front of it.

251
00:16:28,490 --> 00:16:30,390
That's kind of cool I like that.

252
00:16:30,650 --> 00:16:33,290
And otherwise here

253
00:16:36,350 --> 00:16:37,840
you are broke.

254
00:16:40,660 --> 00:16:44,570
You should learn how to save money.

255
00:16:45,140 --> 00:16:46,250
OK.

256
00:16:46,930 --> 00:16:49,720
And so this is cool.

257
00:16:49,720 --> 00:16:56,860
Now what we can do is we can call that function we can say purchase items so purchase item and we can

258
00:16:56,860 --> 00:17:03,160
pass in the narratives we want our current balance so we're going to call it we're going to pass and

259
00:17:03,160 --> 00:17:06,640
bank account balance and then also wants the price of the item.

260
00:17:06,650 --> 00:17:09,810
So this is Sigourney Weaver Alien Stomper shoes.

261
00:17:09,910 --> 00:17:10,650
OK.

262
00:17:11,050 --> 00:17:11,870
And so.

263
00:17:11,980 --> 00:17:19,300
And it's always going to return to us our current balance and now we can say let New Balance equals

264
00:17:19,600 --> 00:17:27,250
not to be confused with the shoe new balance because clearly we're buying Reebok's Reebok so purchased

265
00:17:27,250 --> 00:17:29,380
item for $350.

266
00:17:29,380 --> 00:17:30,870
And notice how the balance is now changed.

267
00:17:30,880 --> 00:17:32,110
Now we're we're being smart.

268
00:17:32,110 --> 00:17:35,870
We're working with functions we're putting in input it's returning things.

269
00:17:36,220 --> 00:17:41,590
And now you know that's our new balance right there are we could well we could have done is instead

270
00:17:41,590 --> 00:17:46,240
of just saying New Balance we could have just said bank account balance we can override it.

271
00:17:46,240 --> 00:17:52,810
Now equals purchased item and then it again remember this is returning something.

272
00:17:52,810 --> 00:17:56,680
OK let me just show you again in case you forgot.

273
00:17:56,800 --> 00:18:02,990
So this return statement right here is returning a value and that's coming in right here.

274
00:18:03,280 --> 00:18:04,410
Okay.

275
00:18:04,570 --> 00:18:10,510
Also keep in mind whenever you call return that is the end that is the end of that function nothing

276
00:18:10,510 --> 00:18:11,790
else will be called after return.

277
00:18:11,800 --> 00:18:14,710
So if it gets here nothing else is going to be called after it.

278
00:18:14,830 --> 00:18:18,300
So if I write code here you know like a print statement you know.

279
00:18:18,490 --> 00:18:20,400
Hello Nurse.

280
00:18:20,400 --> 00:18:21,390
All right.

281
00:18:21,400 --> 00:18:27,130
That is never ever ever ever ever going to be called and he even gives me a warning code after return

282
00:18:27,160 --> 00:18:28,440
will never be executed.

283
00:18:28,590 --> 00:18:33,320
OK return statements and your function.

284
00:18:33,370 --> 00:18:33,880
OK.

285
00:18:34,150 --> 00:18:35,830
So now now we're being smart you know.

286
00:18:35,830 --> 00:18:37,450
And so now we can buy whatever we want.

287
00:18:37,450 --> 00:18:48,400
You know we could save our you know retro lunch box you know equals you know $40.

288
00:18:48,400 --> 00:18:49,230
All right.

289
00:18:49,600 --> 00:18:55,810
So now we have to do is save bank account balance equals purchase item pass in the current bank account

290
00:18:55,810 --> 00:19:01,770
balance and then the item price is you know the retro lunchbox and boom.

291
00:19:02,030 --> 00:19:02,450
OK.

292
00:19:02,500 --> 00:19:05,780
Our bank account balance has now been updated and it's $110.

293
00:19:06,040 --> 00:19:08,050
And we bought we bought her item.

294
00:19:08,050 --> 00:19:10,120
See how it's reusing the code.

295
00:19:10,150 --> 00:19:12,670
We don't have to rewrite things over and over and over again.

296
00:19:12,820 --> 00:19:15,950
That is the beauty of working with functions.

297
00:19:16,020 --> 00:19:20,490
OK now one more thing I want to show you.

298
00:19:20,590 --> 00:19:26,080
This is a little more on the advance side but I just think it be good to know is that you know how I

299
00:19:26,080 --> 00:19:27,450
said these are constants right.

300
00:19:27,470 --> 00:19:33,420
You can not modify these values for instance I cannot say current balance equals zero.

301
00:19:33,700 --> 00:19:35,370
Let's see what happens if I do that.

302
00:19:35,460 --> 00:19:39,490
He yells at us can assign value to current balance is a letter or constant.

303
00:19:39,490 --> 00:19:45,340
Well what if I may not recommend doing it for the specific function function but what if I didn't want

304
00:19:45,340 --> 00:19:46,780
to pass in the current balance.

305
00:19:46,780 --> 00:19:52,840
OK what if or what if I didn't want to return the new balance because it just sounds silly.

306
00:19:53,020 --> 00:19:57,790
I'm purchasing that item why you give me the new balance seems a little bit silly so well we can do

307
00:19:58,600 --> 00:20:01,490
is return the or remove the return statement.

308
00:20:01,960 --> 00:20:02,600
OK.

309
00:20:02,680 --> 00:20:09,700
So instead of returning this here and returning this here what we can do is we can use a keyword here

310
00:20:09,880 --> 00:20:11,550
called in out.

311
00:20:11,970 --> 00:20:18,090
OK current balance in out what it's going to do is it's actually going to modify the original variable.

312
00:20:18,220 --> 00:20:21,320
Okay I'm going to get rid of these 2 x we don't need to assign a new value anywhere.

313
00:20:21,530 --> 00:20:28,030
Okay purchase item and we'll come back to those in a second so we can use the in out keyword which will

314
00:20:28,060 --> 00:20:33,460
allow us to modify the bank account balance directly which is kind of cool.

315
00:20:33,460 --> 00:20:34,760
Now you want to use it sparingly.

316
00:20:34,780 --> 00:20:36,980
But I'm just showing you you can use this.

317
00:20:37,090 --> 00:20:43,450
And so what we can do is we can put the same math we had before except just adjust the existing parameter

318
00:20:43,450 --> 00:20:43,750
here.

319
00:20:43,750 --> 00:20:48,280
Remember how it was a constant we couldn't change it but now we can and that's done by putting the in

320
00:20:48,280 --> 00:20:49,930
out before the type right there.

321
00:20:49,930 --> 00:20:56,190
So now we can say current balance equals current balance minus item price.

322
00:20:56,410 --> 00:20:57,920
Okay.

323
00:20:58,000 --> 00:21:01,890
And so it allows us to modify the variable directly.

324
00:21:02,050 --> 00:21:03,350
Pretty awesome right then right here.

325
00:21:03,370 --> 00:21:04,890
We don't have to do anything except print.

326
00:21:04,890 --> 00:21:06,960
We don't have to return any things is great.

327
00:21:06,970 --> 00:21:10,840
And then over here we can reduce our functions or purchase item.

328
00:21:10,840 --> 00:21:17,770
And what we're going to do is we're going to pass in the bank account balance or the the bank account

329
00:21:17,770 --> 00:21:24,130
balance current balance is what is going on here.

330
00:21:24,440 --> 00:21:32,370
No silly it's being silly bank account balance and item price is going to be the Sigourney Weaver issue

331
00:21:32,370 --> 00:21:33,150
right.

332
00:21:33,680 --> 00:21:34,320
Pretty cool right.

333
00:21:34,320 --> 00:21:37,570
And so all you do is you put that ampersand symbol here.

334
00:21:38,000 --> 00:21:43,040
When you're when you're doing it now when you're doing it out OK put the ampersand symbol before the

335
00:21:43,040 --> 00:21:46,240
variable you pass in and that lets you modify the variable directly.

336
00:21:46,340 --> 00:21:47,880
So we didn't even have to return anything.

337
00:21:47,900 --> 00:21:50,910
So it just whatever you put in here it modifies this directly.

338
00:21:51,040 --> 00:21:51,290
OK.

339
00:21:51,290 --> 00:21:53,060
Again there is use cases for.

340
00:21:53,060 --> 00:21:56,750
I don't recommend using this all the time but I want to show you that it's in your toolbelt of things

341
00:21:56,750 --> 00:21:57,960
that you can do.

342
00:21:58,340 --> 00:22:03,940
And otherwise you know you probably want to go back the way we had it which is just fine and that's

343
00:22:04,040 --> 00:22:04,860
functions.

344
00:22:05,090 --> 00:22:09,330
Creating something that's reusable that performs an operation over and over again.

345
00:22:09,460 --> 00:22:14,150
It's it's something that you're going to use every single day and it's going to take a while to get

346
00:22:14,150 --> 00:22:14,840
this down.

347
00:22:14,840 --> 00:22:18,100
I know that if you especially if you're new to programming and it's fine.

348
00:22:18,230 --> 00:22:22,700
Just keep practicing you'll get better with it and that's it functions.

349
00:22:24,920 --> 00:22:28,000
I'm trying to think of anything else I could say about it but I doubt it.

350
00:22:28,070 --> 00:22:30,880
Ninety nine point nine percent of everything you'll do every single day.

351
00:22:31,040 --> 00:22:31,630
We just did.

352
00:22:31,770 --> 00:22:32,260
So that's it.

353
00:22:32,260 --> 00:22:35,390
Mark Price of slopes dot com.

354
00:22:35,390 --> 00:22:36,560
See you next time.

