1
00:00:00,510 --> 00:00:06,750
So will come back again in this lesson we're going to talk about functions in swift which are a rather

2
00:00:06,750 --> 00:00:12,780
important topic and although they're quite important they're actually very simple to use in swithe so

3
00:00:12,780 --> 00:00:15,920
I'm going to quickly show you what they are and what they mean.

4
00:00:15,930 --> 00:00:17,390
Let's go to Scott.

5
00:00:17,970 --> 00:00:20,540
And this time I'm actually going to go and make a project.

6
00:00:20,550 --> 00:00:27,780
So once you put an eye in here and my quest command line tool if you make sure it is this language is

7
00:00:27,780 --> 00:00:28,280
free.

8
00:00:28,360 --> 00:00:33,450
And when I call it functions even if it's true.

9
00:00:34,250 --> 00:00:34,980
OK.

10
00:00:35,080 --> 00:00:36,950
And now create.

11
00:00:36,950 --> 00:00:43,320
So functions like strafed are in terms of the principles are exactly the same thing as Objective-C and

12
00:00:43,320 --> 00:00:49,620
most other programming languages they offer us a way of modular rising or code that also offer us a

13
00:00:49,620 --> 00:00:53,500
way to call a same piece of multiple times.

14
00:00:53,640 --> 00:00:55,750
And they help us categorize our code.

15
00:00:55,890 --> 00:01:02,160
So imagine I want to make a function in the past if this was objective c we would do something like

16
00:01:02,160 --> 00:01:02,460
this.

17
00:01:02,470 --> 00:01:06,810
We would do with Dasch depending on whether it is a constructor or not.

18
00:01:06,810 --> 00:01:14,760
And then we would say return type and then you would say name of function and then we would say a column

19
00:01:14,880 --> 00:01:20,660
and then we would say argument type and then would say a man's name.

20
00:01:20,850 --> 00:01:26,580
And then this whole thing would continue would have multiple arguments more human type so on and so

21
00:01:26,580 --> 00:01:27,100
forth.

22
00:01:27,470 --> 00:01:30,600
In swift It's actually a lot more streamlined.

23
00:01:30,620 --> 00:01:34,830
It is very much like most modern programming languages.

24
00:01:34,830 --> 00:01:41,690
So here is how I make a function that doesn't have any arguments doesn't return any arguments.

25
00:01:41,820 --> 00:01:43,860
So I'm going to say fine.

26
00:01:44,370 --> 00:01:50,310
My function doesn't have any arguments doesn't return anything.

27
00:01:50,460 --> 00:01:52,730
And here is the body of my function.

28
00:01:52,770 --> 00:01:58,530
If your function doesn't return anything that's kind of like your void function so it would actually

29
00:01:58,590 --> 00:02:00,910
either void in here if you want to.

30
00:02:01,050 --> 00:02:07,080
Well you can just completely drop it so this function whenever it is called It just does something such

31
00:02:07,080 --> 00:02:18,240
as What if I saved print hello and then somebody in here call my function that would print hello for

32
00:02:18,240 --> 00:02:18,610
me.

33
00:02:19,540 --> 00:02:21,200
Let's see that in practice

34
00:02:23,800 --> 00:02:29,780
that says hello and something you probably already noticed is not on like objective see that we had

35
00:02:29,780 --> 00:02:36,240
to say sell for and then self name of function and then bracket.

36
00:02:36,260 --> 00:02:40,150
In Shreeves we just call the name of the function with its arguments.

37
00:02:40,160 --> 00:02:43,000
In this case there are no arguments.

38
00:02:43,010 --> 00:02:46,870
This is of course giving an error simply because it's Objective-C.

39
00:02:47,090 --> 00:02:48,490
It doesn't work in here.

40
00:02:48,620 --> 00:02:49,620
So removed that.

41
00:02:49,620 --> 00:02:53,240
How about we now add an argument for a function.

42
00:02:53,240 --> 00:02:58,540
How about we are an argument that receives a name and says hello data name.

43
00:02:58,730 --> 00:03:06,290
So I'm going to go in here and say well I have something called input's name of that type of string.

44
00:03:06,620 --> 00:03:13,700
So my function has an input name of the type of string and whenever you want to print to say hello and

45
00:03:13,700 --> 00:03:18,040
then we interpret it in name.

46
00:03:18,200 --> 00:03:22,980
So whatever input name we have got them we say hello is space plane.

47
00:03:23,180 --> 00:03:25,730
So they call a function like this.

48
00:03:25,760 --> 00:03:31,820
I would simply say my function and data you have to enter name name.

49
00:03:31,850 --> 00:03:35,870
I'm going to say that E-Myth name is a war that's wrong not

50
00:03:39,810 --> 00:03:42,240
perfect it says hello worth.

51
00:03:42,300 --> 00:03:45,240
Hello comes from within the body of the function.

52
00:03:45,240 --> 00:03:47,390
The word comes from the argument.

53
00:03:47,460 --> 00:03:52,180
Now what if I had multiple arguments with you then just have the implant.

54
00:03:52,230 --> 00:03:59,390
Well what if my heart in name and the last name of that type of string.

55
00:03:59,610 --> 00:04:07,050
Well it would be exactly the same thing it would be hello Whatever say is space and then another one

56
00:04:07,590 --> 00:04:12,840
eem last name and then in you know I had to actually enter the other piece as well.

57
00:04:12,870 --> 00:04:15,640
And I would say hello ward.

58
00:04:15,750 --> 00:04:18,570
I don't know that's a weird last name to make right now.

59
00:04:18,780 --> 00:04:20,710
Hello world.

60
00:04:20,820 --> 00:04:22,870
All of us are good.

61
00:04:22,890 --> 00:04:23,980
I got there.

62
00:04:24,000 --> 00:04:24,670
OK.

63
00:04:24,690 --> 00:04:27,590
So hello we're off I was on the air with preens.

64
00:04:27,610 --> 00:04:28,550
Hello world.

65
00:04:28,570 --> 00:04:35,670
I was so so far we had a function that receives some argument or it can do whatever you can imagine

66
00:04:35,670 --> 00:04:38,030
between them and give us a response.

67
00:04:38,040 --> 00:04:44,310
For instance if you give a function that is going to multiply two numbers you would simply be lets say

68
00:04:44,470 --> 00:04:55,710
Im OK so if my fine shin and then I would say Im a of the type integer and Im B of the type Integer.

69
00:04:55,860 --> 00:05:06,660
And then I would say do this lets the rest of the Im a multiply by Gimple B and then say print and in

70
00:05:06,660 --> 00:05:12,510
the print say raise e is and then I would do one of these.

71
00:05:12,510 --> 00:05:14,790
What is raise raise is this.

72
00:05:15,090 --> 00:05:20,430
And then if I wanted to call that function I would say my function and it says hey you have to give

73
00:05:20,430 --> 00:05:26,790
me one guy called him put eight of the type integer and other guy of the type you know integer by the

74
00:05:26,790 --> 00:05:28,160
name of input B.

75
00:05:28,170 --> 00:05:36,470
I'm going to say 5 by 17 so far is five by 1785 I know top 5 y.

76
00:05:36,490 --> 00:05:37,950
So it's fun.

77
00:05:38,000 --> 00:05:38,860
It is right.

78
00:05:38,880 --> 00:05:39,780
Yes it is.

79
00:05:39,870 --> 00:05:41,770
I felt like second I doubted myself.

80
00:05:41,910 --> 00:05:43,000
So yes.

81
00:05:43,050 --> 00:05:48,270
So that's how we call a function that has multiple arguments does the same something.

82
00:05:48,270 --> 00:05:55,230
And then once it does that thing it you know it doesn't return anything the return is still void.

83
00:05:55,230 --> 00:05:58,260
So what if we wanted to actually get the return.

84
00:05:58,260 --> 00:06:04,960
What if it didn't want this guy to preens there is rather than that we wanted to see the actual raise.

85
00:06:04,980 --> 00:06:06,550
So here's how you would do that.

86
00:06:06,570 --> 00:06:13,590
I would say well this guy now has a return type of integer and this return type of integer has to be

87
00:06:13,590 --> 00:06:17,510
returned the somber sort of return address.

88
00:06:17,790 --> 00:06:24,960
So once again I have a function two arguments as input one integer has to be the output Offut or outcome

89
00:06:24,960 --> 00:06:31,260
of this function and I'm going to say whatever you have to do mathematically or otherwise and then return

90
00:06:31,260 --> 00:06:32,060
that value.

91
00:06:32,070 --> 00:06:35,340
So essentially and this is a very important point.

92
00:06:35,340 --> 00:06:40,980
This goes to the core of programming if you're new to programming if you have done programming in the

93
00:06:40,980 --> 00:06:42,550
past you would know this very well.

94
00:06:42,720 --> 00:06:50,690
But if I was going to enter values such as two and five we know two by five becomes 10 right.

95
00:06:50,700 --> 00:06:57,120
So if I wanted to call my function here I would say my function and I would say values are 25.

96
00:06:57,330 --> 00:07:05,950
But then this is just like saying instead of my function in a middle of your code you have written 10.

97
00:07:06,180 --> 00:07:13,650
There is really no way that this thing is actually being used right now because it returns a value.

98
00:07:13,650 --> 00:07:15,830
So you have to do something with it.

99
00:07:15,840 --> 00:07:16,950
What could you do.

100
00:07:16,950 --> 00:07:22,010
I don't know you wanted to send it to be your registration for a user.

101
00:07:22,020 --> 00:07:28,260
You want to send it to find the value of the number of the other note listings that are associated with

102
00:07:28,260 --> 00:07:34,310
a certain user in a real estate application whatever it is you are trying to do.

103
00:07:34,470 --> 00:07:36,410
But you shouldn't just call it in here.

104
00:07:36,430 --> 00:07:42,320
The return is like numeric value because the return of the S would be 2 by 5 10.

105
00:07:42,380 --> 00:07:47,830
Is as if I wrote ten in the middle of my code which doesn't really make any sense what does make sense.

106
00:07:47,830 --> 00:07:51,260
Is thing this saying preens.

107
00:07:51,420 --> 00:07:58,280
And then in the print I would say let's call this function say that is what we want.

108
00:07:58,290 --> 00:08:01,680
So that would be a much more acceptable formula.

109
00:08:01,680 --> 00:08:04,520
It would be print whatever you find in here.

110
00:08:04,530 --> 00:08:11,060
And if I run this now it will give me 10 just like how we expected it to be 10 perfect.

111
00:08:11,100 --> 00:08:16,850
Or you could you know in here you could have a variable that is equal to that function.

112
00:08:16,860 --> 00:08:22,470
So when you're defining and declaring your function and you should have a clear plan for what is the

113
00:08:22,470 --> 00:08:26,380
arguments that you're entering into them and what are you getting from them.

114
00:08:26,590 --> 00:08:28,920
And why do they match there for whatever reason.

115
00:08:28,920 --> 00:08:31,040
For instance I could give you an example.

116
00:08:31,230 --> 00:08:36,610
Imagine your function was meant to remove the factors that you don't want.

117
00:08:36,690 --> 00:08:41,500
For instance you wanted to look at a string and he was like a clean string.

118
00:08:41,550 --> 00:08:45,010
So I would say this is an input string.

119
00:08:45,540 --> 00:08:48,000
And then the return is a string.

120
00:08:48,270 --> 00:08:56,250
And then in here I don't know how you would do this you would say that arrays a string would be empty

121
00:08:56,250 --> 00:09:00,380
at the beginning and then we would do some cleaning.

122
00:09:00,660 --> 00:09:03,270
And then we would have it here and read a string.

123
00:09:03,420 --> 00:09:09,530
So what is happening in here is that when we call our function in protesting of the type this thing

124
00:09:09,540 --> 00:09:15,210
of course when we call her a function and we give it this string it cleans it up and gives it back to

125
00:09:15,210 --> 00:09:21,780
us and then maybe just maybe if you remember from some of our previous lessons this was the string you

126
00:09:21,780 --> 00:09:25,350
wanted to pass to the next view controller for instance.

127
00:09:25,350 --> 00:09:32,460
So something like that would be would be you know acceptive or maybe you know you have a my colleague

128
00:09:32,530 --> 00:09:34,760
next the view controller.

129
00:09:34,860 --> 00:09:42,170
And the next few Kuntar has a receiver that dad thought that would become mine find shen.

130
00:09:42,450 --> 00:09:45,630
And then my function would have sent him first string.

131
00:09:45,630 --> 00:09:49,020
I'm writing this in a comment because this just doesn't make any sense.

132
00:09:49,020 --> 00:09:50,940
I don't have an X controller.

133
00:09:50,990 --> 00:09:54,700
This is just for the sake of demonstration essentially.

134
00:09:54,720 --> 00:10:00,690
So my impis dream would be like I don't know my text F.L. the text.

135
00:10:00,690 --> 00:10:04,230
So it is values that I don't know if they're clean or not.

136
00:10:04,230 --> 00:10:09,810
And I would send it to this my function he would do some Kooning he would return a good one clean one

137
00:10:09,810 --> 00:10:16,410
to me and I would push the clean one into the received data of my next controller or something to that

138
00:10:16,410 --> 00:10:17,180
effect.

139
00:10:17,190 --> 00:10:24,300
So so far we saw functions that they can accept arguments they can return values or they could have

140
00:10:24,300 --> 00:10:25,600
neither of those two.

141
00:10:25,810 --> 00:10:31,070
But what I wanted to have a function that returns more than one value.

142
00:10:31,260 --> 00:10:38,790
What if the situation that I had was this I wanted my function to receive value and let me know whether

143
00:10:38,790 --> 00:10:46,500
the value it has received whether it was a positive or negative value and also returned the positive

144
00:10:46,500 --> 00:10:48,150
version no matter what.

145
00:10:48,300 --> 00:10:53,010
So essentially here is what my function should do if I give it minus five.

146
00:10:53,010 --> 00:10:59,870
It has to get dive to me with five on a false saying the positive version of this is five.

147
00:11:00,030 --> 00:11:02,360
And it wasn't positive on its own.

148
00:11:02,530 --> 00:11:07,710
And if I gave it 17 it should give me like 17 and saying true.

149
00:11:07,890 --> 00:11:14,100
So I want some sort of a structure that allows me to return to more than one value.

150
00:11:14,400 --> 00:11:19,900
And that is something we saw recently in our previous lesson if I'm not wrong and that's it.

151
00:11:20,070 --> 00:11:23,100
So let's see how we could rewrite this entire thing.

152
00:11:23,380 --> 00:11:25,230
So fine.

153
00:11:26,110 --> 00:11:33,150
I mean that's our input input integer if is of the type Integer.

154
00:11:33,150 --> 00:11:39,210
And I have a return which is an integer and a boolean.

155
00:11:39,210 --> 00:11:40,620
Now let's see what you have to do.

156
00:11:40,620 --> 00:11:41,740
It's very simple.

157
00:11:41,910 --> 00:11:52,440
So if input integer was larger than minus one then I have to have a value card as boolean that's going

158
00:11:52,440 --> 00:11:55,170
to be by default.

159
00:11:55,170 --> 00:11:59,770
I'm going to say that to be true and then I'm going to say if it is larger than that.

160
00:12:00,270 --> 00:12:01,200
Don't do anything.

161
00:12:01,200 --> 00:12:07,900
So if it is smaller than zero Reza Boullion has to become false.

162
00:12:08,130 --> 00:12:09,710
That's one part of it.

163
00:12:09,720 --> 00:12:11,720
I also need the return value.

164
00:12:11,720 --> 00:12:14,140
So I'm going to save our arrays.

165
00:12:14,160 --> 00:12:18,830
Integer is by default going to be input in.

166
00:12:19,260 --> 00:12:28,950
And then if it is below zero gets rather integer multiplied by minus 1 so change its signature.

167
00:12:28,950 --> 00:12:32,260
Once you're done on through with that say return.

168
00:12:32,340 --> 00:12:37,820
What do I want to 10 or when I returned the AS INTEGER and I wanted a $10 billion.

169
00:12:37,950 --> 00:12:42,080
So imagine if a number such as 5 goes through this thing.

170
00:12:42,090 --> 00:12:44,980
It says that is five is smaller than zero.

171
00:12:45,030 --> 00:12:46,120
That's not happening.

172
00:12:46,170 --> 00:12:49,030
So 5 billion is going to be true.

173
00:12:49,230 --> 00:12:51,990
And as integer is going to be five.

174
00:12:52,380 --> 00:12:53,540
So let's test this out.

175
00:12:53,550 --> 00:13:04,020
I'm going to say I have a left my jeans being minus five minus five and I'm going to have to say I have

176
00:13:04,090 --> 00:13:05,570
a let.

177
00:13:05,660 --> 00:13:07,740
State is going to be.

178
00:13:07,770 --> 00:13:16,430
What is it going to be is going to be determine input value and the input value is going to be my in

179
00:13:16,820 --> 00:13:19,520
dots one the boolean one.

180
00:13:19,520 --> 00:13:20,910
So that's the state.

181
00:13:20,910 --> 00:13:25,740
And then let's pause number the positive value of.

182
00:13:25,740 --> 00:13:28,220
Here is the term.

183
00:13:28,370 --> 00:13:33,660
And then again does zero the zero.

184
00:13:33,780 --> 00:13:36,690
So I find the a state of faith if it is minus five.

185
00:13:36,760 --> 00:13:41,800
I find out the positive value of it was minus five it becomes five.

186
00:13:41,810 --> 00:13:45,870
If it is five days five then I would say print.

187
00:13:45,890 --> 00:13:47,580
And here's what I want to print.

188
00:13:47,690 --> 00:13:56,300
I want to say the number that they had which is my eant laws.

189
00:13:57,120 --> 00:13:57,700
What was it.

190
00:13:57,690 --> 00:14:08,070
It was the estate and is now let's find out what I know this that this earlier a very crude version

191
00:14:08,070 --> 00:14:11,310
of doing this thing but you understand the concept behind it.

192
00:14:11,310 --> 00:14:13,970
I hope so let's say let's see what you get.

193
00:14:13,980 --> 00:14:22,830
So if I wrong this is God tell me minus five was false and he's now five.

194
00:14:22,830 --> 00:14:31,650
And if it was 17 it says what does it say 17 was true and is now 17.

195
00:14:31,650 --> 00:14:32,190
Perfect.

196
00:14:32,190 --> 00:14:36,870
So again I managed to get two values out of a function.

197
00:14:36,870 --> 00:14:39,110
You could also call this one.

198
00:14:39,120 --> 00:14:45,750
You could call this one on the sign the number or whatever you'd like to.

199
00:14:46,110 --> 00:14:48,330
And then this phone call it a status.

200
00:14:48,330 --> 00:14:52,610
So just like two people that we could call its values or variables.

201
00:14:52,740 --> 00:14:54,020
You can also do it here.

202
00:14:54,120 --> 00:14:59,640
And if you had those you could essentially go in here and say well that one is that and this one is

203
00:14:59,640 --> 00:15:02,440
the stop this you don't need to rely on the indexes.

204
00:15:02,610 --> 00:15:07,970
You can't rely on the actual names and would perfectly work the way you expected.

205
00:15:08,100 --> 00:15:12,960
Before I finished this lesson I want to tell you one more thing about functions in swift which is again

206
00:15:13,290 --> 00:15:19,070
a unique thing to swift and that is functions that are nested inside each other.

207
00:15:19,200 --> 00:15:26,340
So I could have a function called Find the one that doesn't have any humans that have any return even

208
00:15:26,340 --> 00:15:27,850
though they don't really matter.

209
00:15:28,050 --> 00:15:35,550
And inside that I could have another function call it on nested one or anything that you want to call

210
00:15:35,550 --> 00:15:35,940
it.

211
00:15:36,240 --> 00:15:45,210
And then an inside one maybe does something for instance it says this is printed from inside.

212
00:15:45,210 --> 00:15:45,630
All right.

213
00:15:45,630 --> 00:15:49,480
So I have a function that is nested inside this one.

214
00:15:49,620 --> 00:15:57,810
And since it is nested inside function one I can actually say nested one or mess that find Let's call

215
00:15:57,810 --> 00:16:06,170
it Nesse that function so now I have a function which is nested inside another one and I can call it

216
00:16:06,170 --> 00:16:06,470
in here.

217
00:16:06,470 --> 00:16:12,990
It is primarily used if you have a very long function and for some reason parts of it they can be categorized

218
00:16:13,280 --> 00:16:18,470
but you don't want to expose them outside your function you only want to keep them inside to be very

219
00:16:18,470 --> 00:16:25,280
honest with you I don't really use this very often I can say that throughout the years maybe I've used

220
00:16:25,280 --> 00:16:30,650
it a handful of times so don't worry overly about you know what it is and how it works it's obviously

221
00:16:30,650 --> 00:16:36,710
best if if you learn everything and you know that they exist but you have to craft your own way of doing

222
00:16:36,710 --> 00:16:41,870
things and every once in awhile look up on line and look at other people's code and try to get a sense

223
00:16:41,870 --> 00:16:45,670
of you know how they are doing things if they're raised better now.

224
00:16:45,890 --> 00:16:47,800
We need a way to call these functions.

225
00:16:47,810 --> 00:16:51,680
And of course we can call this one because that one is not even accessible.

226
00:16:51,680 --> 00:16:53,860
We don't have access to that guy only.

227
00:16:54,010 --> 00:16:55,170
These guys have.

228
00:16:55,250 --> 00:17:06,120
So if we can call find one and if we do that and we run this code now it says this is printed from inside.

229
00:17:06,170 --> 00:17:08,620
So that's about not our.

230
00:17:08,690 --> 00:17:12,010
We talked about functions that they have no arguments.

231
00:17:12,020 --> 00:17:19,070
We talked about functions that they have some arguments we had we talked about functions that they had

232
00:17:19,430 --> 00:17:25,020
no return types they were voids or the ones that the vendor actually returning something.

233
00:17:25,100 --> 00:17:29,250
And of course if they're returning there has to be a return for them.

234
00:17:29,300 --> 00:17:30,880
Something has to come out.

235
00:17:31,010 --> 00:17:38,220
We talked about functions that they returned to post instead of single values they return multiple values.

236
00:17:38,390 --> 00:17:42,500
And if that's the case again we have written a similar id..

237
00:17:42,500 --> 00:17:45,840
The chance really you know ignore any of them and be fine.

238
00:17:45,860 --> 00:17:48,980
We talked about having nested functions.

239
00:17:49,100 --> 00:17:50,510
Here I have an error.

240
00:17:50,540 --> 00:17:52,200
And that's because of this.

241
00:17:52,700 --> 00:17:54,150
Because of that.

242
00:17:54,350 --> 00:18:01,370
And then in your argument I'm going to say Ninh I don't want you know in Arctic anything to it.

243
00:18:01,430 --> 00:18:04,420
So that's about that that's about functions.

244
00:18:04,430 --> 00:18:06,020
Let's move on to our next lesson.
