1
00:00:01,790 --> 00:00:08,690
Hello and we're coming in on this list then we're going to have a quick introduction to the use of functions.

2
00:00:08,840 --> 00:00:16,460
We will have a section on lessons about the use of classes and object oriented programming and functions

3
00:00:16,520 --> 00:00:17,900
in the coming sections.

4
00:00:17,900 --> 00:00:23,750
But for now I thought it would be useful if we get the basic introduction to what functions are and

5
00:00:23,750 --> 00:00:29,840
why do we use them and how do we essentially use them and write them because we have been using them

6
00:00:29,840 --> 00:00:34,550
for a while now now it kind of makes sense that we know a little bit more about them.

7
00:00:34,610 --> 00:00:38,670
So functions are essentially the functioning part of an object.

8
00:00:38,690 --> 00:00:45,530
An object could have properties such as when you have a label that label has a property called text

9
00:00:45,860 --> 00:00:49,680
but also could have functions that does the things for us.

10
00:00:49,680 --> 00:00:55,660
We've talked about this in more detail in the future but for now we need to know how do we use them.

11
00:00:55,880 --> 00:01:03,800
So let's jump back into ex-cult and for this project I'm actually going to use on all U.S. applications

12
00:01:03,800 --> 00:01:09,710
simply because the way that we write functions in command line tools is a little bit different than

13
00:01:09,710 --> 00:01:15,680
the ones that we do you know in iOS up occasions and the command line is a little bit out of the scope

14
00:01:15,680 --> 00:01:17,880
of this course essentially.

15
00:01:18,130 --> 00:01:29,420
So I'm going to make a new iOS single view application and I'm going to call it functions by voice.

16
00:01:29,580 --> 00:01:32,410
So let's see what do we mean by functions.

17
00:01:32,440 --> 00:01:33,780
I'm going to go in here.

18
00:01:33,990 --> 00:01:39,300
I'm going to go to my viewpoint you can that I am not as you remember we can clear a lot of this out

19
00:01:39,310 --> 00:01:43,070
so we have a cleaner and tidy you know code.

20
00:01:43,140 --> 00:01:44,660
Not very overwhelming.

21
00:01:44,800 --> 00:01:48,450
And well I'm going to do is I'm going to say and Aslaug Hello.

22
00:01:49,170 --> 00:01:52,430
So what this does is very similar with a command line tool.

23
00:01:52,440 --> 00:02:00,690
When the app compiles on the application essentially launches in the simulator VBN see the command debug

24
00:02:00,690 --> 00:02:05,730
console and that's going to show us the value that we try to print.

25
00:02:05,730 --> 00:02:13,890
So now down in here it says hello which perfect Don't worry about that little thing up there.

26
00:02:13,890 --> 00:02:16,850
That's a machine printed you know.

27
00:02:17,210 --> 00:02:23,820
If if you have that depending on the application and the version of the simulator that you're using

28
00:02:23,820 --> 00:02:25,070
you might see different things.

29
00:02:25,080 --> 00:02:30,010
But this is the thing that we are seeing now here I want to add a new function.

30
00:02:30,060 --> 00:02:31,220
Let's see how do we do that.

31
00:02:31,230 --> 00:02:33,660
And then I'm going to explain what it does.

32
00:02:33,920 --> 00:02:42,870
I'm going to call this one my function and this is what my function does.

33
00:02:42,900 --> 00:02:46,360
It does this in a long Hello.

34
00:02:47,070 --> 00:02:54,700
So I currently have a new function in my project essentially in the implementation of your controller.

35
00:02:54,720 --> 00:02:59,650
Don't worry if you don't know what that is we'll talk about this in for more details in the future.

36
00:02:59,850 --> 00:03:05,550
But I have a new function which is actually quite similar to the Lord.

37
00:03:05,690 --> 00:03:08,710
You see it has a dash avoid the view of the alloy.

38
00:03:08,770 --> 00:03:09,370
I have it.

39
00:03:09,430 --> 00:03:10,570
I shall watch it.

40
00:03:10,740 --> 00:03:19,560
My function to call that I'm going to say self which means this column and file this client I see my

41
00:03:19,560 --> 00:03:20,760
function.

42
00:03:20,760 --> 00:03:24,620
So now I'm calling the function from self.

43
00:03:24,660 --> 00:03:29,690
So what do you think is going to happen when I run this application now when I relaunch it.

44
00:03:30,310 --> 00:03:35,670
Viewed that lot is going to happen because that's the one that happens by default and that the fourth

45
00:03:35,670 --> 00:03:41,690
is going to say Well go on find this function and run it and it finds this function says hello.

46
00:03:41,730 --> 00:03:43,890
Which is exactly what we expected.

47
00:03:43,890 --> 00:03:47,610
Now let's get a sense of what the function is or how it works.

48
00:03:47,760 --> 00:03:51,300
Every function essentially has these few things in it.

49
00:03:51,420 --> 00:03:53,490
I'm going to write them in comments.

50
00:03:53,490 --> 00:03:58,440
So it has a dash that shows it is not a construct or we'll talk about this in the future.

51
00:03:58,440 --> 00:04:04,570
Let's imagine there's always a dash and then within the Qantas's is the return type.

52
00:04:04,590 --> 00:04:08,660
It says what does this function return for us.

53
00:04:08,760 --> 00:04:12,690
And then there is the name of the function.

54
00:04:12,690 --> 00:04:23,350
And then there is a column an option or a column and then there is an all humans type and then there's

55
00:04:23,400 --> 00:04:25,760
an art you man's name.

56
00:04:25,840 --> 00:04:27,470
And this continues.

57
00:04:27,790 --> 00:04:31,360
Let's see what they have written it seems a little bit complicated.

58
00:04:31,390 --> 00:04:35,520
It actually is not is saying that there is a return type.

59
00:04:35,530 --> 00:04:41,950
What does this one pretend my function doesn't retain or anything it's return type is avoid it just

60
00:04:41,950 --> 00:04:43,540
does something.

61
00:04:43,660 --> 00:04:48,610
It's not like my function is meant to calculate somebody and give me a number.

62
00:04:48,670 --> 00:04:54,280
It simply means to print hello and that is saying what's the name of the function that someone.

63
00:04:54,520 --> 00:04:56,600
And then some optional arguments.

64
00:04:56,620 --> 00:04:59,800
Are there any arguments that you want to enter into it.

65
00:04:59,830 --> 00:05:03,180
Now that's a function that has an argument.

66
00:05:03,260 --> 00:05:13,680
So I'm going to go in here something right again boy and envoi I'm going to say my new function data

67
00:05:14,410 --> 00:05:21,040
has the identity of the type and this a string but I call it input.

68
00:05:21,040 --> 00:05:22,810
So let's see what this one means.

69
00:05:22,810 --> 00:05:25,810
It means there is a function called my new function.

70
00:05:25,810 --> 00:05:28,990
I receive is something called input.

71
00:05:29,290 --> 00:05:34,920
So it is going to call that going to say cell for my new function.

72
00:05:35,120 --> 00:05:38,600
And it says hey you have to feed me Genesis 3.

73
00:05:38,690 --> 00:05:41,420
I say OK this thing is a mirror.

74
00:05:41,980 --> 00:05:48,820
So now this function receives something but you haven't really done anything in here which we call the

75
00:05:48,820 --> 00:05:52,870
body of the function body of the function.

76
00:05:52,870 --> 00:06:01,420
You haven't done anything in your heart what we print that we say and the slogan perience what ever

77
00:06:01,510 --> 00:06:04,150
you will find in input.

78
00:06:04,210 --> 00:06:11,760
So whatever is the value that someone has fed into this function gets into the slot and gets printed.

79
00:06:11,980 --> 00:06:13,420
Let's see if that works.

80
00:06:17,960 --> 00:06:18,440
OK.

81
00:06:18,500 --> 00:06:20,800
So it says hello and then the other function.

82
00:06:20,840 --> 00:06:21,980
This fine print.

83
00:06:22,020 --> 00:06:23,870
I mean that's the one.

84
00:06:23,870 --> 00:06:26,440
Now bear with me for a few minutes.

85
00:06:26,450 --> 00:06:33,400
It will become a lot more you know a lot easier to understand now and clean or delete all of these functions.

86
00:06:33,500 --> 00:06:39,140
And I'm going to have a function that when you give it the number it returns the square effect.

87
00:06:39,170 --> 00:06:41,920
So I'm going to call it war.

88
00:06:42,010 --> 00:06:51,550
That is where function and my work function receives an integer let's call it input again.

89
00:06:51,990 --> 00:06:53,180
And what does it do.

90
00:06:53,210 --> 00:07:00,510
It says yins rez result is input multiplied by input.

91
00:07:00,560 --> 00:07:09,230
That is a squared value of something and log in a slide it says it's quite easy.

92
00:07:09,560 --> 00:07:10,910
What is it.

93
00:07:10,970 --> 00:07:12,720
It is going to be.

94
00:07:13,670 --> 00:07:20,270
So whoever calls this function and gives it a value it's really calculate the square feet and it gets

95
00:07:20,270 --> 00:07:23,540
printed for us the strike in the triangle.

96
00:07:23,540 --> 00:07:29,600
When I say sell a square function of eight let's run

97
00:07:33,830 --> 00:07:37,420
now I have it saying a square is 64.

98
00:07:37,430 --> 00:07:39,860
Exactly like how I expected.

99
00:07:39,860 --> 00:07:46,180
Now let's have another function that we give it to numbers it tells us which one is larger.

100
00:07:46,250 --> 00:07:48,670
So I'm going to delete all of these again.

101
00:07:48,800 --> 00:07:57,040
I'm going to say I have a void in my void which is bigger.

102
00:07:57,050 --> 00:08:05,950
These are obviously names coming up by solve our ints a and b.

103
00:08:05,950 --> 00:08:12,340
And that is going to be in beak.

104
00:08:12,560 --> 00:08:17,710
These are essentially high because you don't really need them but you want to separate your arguments.

105
00:08:17,720 --> 00:08:20,090
You have to put their space between them.

106
00:08:20,120 --> 00:08:23,030
Every human needs to know what is its type.

107
00:08:23,030 --> 00:08:25,320
In here both of them are integer.

108
00:08:25,340 --> 00:08:28,040
Maybe you're writing something that one of them is integer.

109
00:08:28,050 --> 00:08:29,540
One of them is a string.

110
00:08:29,640 --> 00:08:33,390
You should however mention or maybe you should have said this a little earlier.

111
00:08:33,470 --> 00:08:39,250
This lesson isn't for you to learn how to write functions you will get there in the future.

112
00:08:39,380 --> 00:08:45,920
This lesson is for you to understand how functions are being called and why do we use functions in general

113
00:08:46,250 --> 00:08:53,480
so don't worry overly about the ins and outs of the functions we will get there in a few sessions sections

114
00:08:53,480 --> 00:08:54,240
from now.

115
00:08:54,440 --> 00:09:06,660
So in any case if I receive an A and A B but I say if a is larger than B then and it's like are whatever

116
00:09:06,680 --> 00:09:11,610
is the value in a D is larger.

117
00:09:11,960 --> 00:09:26,550
And that is a else and this log what is the value is larger in B of course.

118
00:09:26,570 --> 00:09:32,430
So let's see how we can call this in my view that a lot because the method or the function that runs

119
00:09:32,480 --> 00:09:40,370
by default function runs out the value all the time.

120
00:09:40,460 --> 00:09:49,060
I'm going to say self which is bigger 5 and 15.

121
00:09:49,510 --> 00:09:53,090
And if you run this code now it's going to take a little while.

122
00:09:53,090 --> 00:10:00,640
And that is going to tell us 15 is large because it's calling that function.

123
00:10:00,800 --> 00:10:08,110
Now like I said the exact details of how do we use functions or how do we write our own functions is

124
00:10:08,120 --> 00:10:10,150
going to come up sometime in the future.

125
00:10:10,310 --> 00:10:16,070
What is important right now is understanding that how do we call functions and we do it using their

126
00:10:16,520 --> 00:10:20,780
bracket operations in Objective-C in swift and other languages.

127
00:10:20,780 --> 00:10:22,840
We do it slightly differently.

128
00:10:23,120 --> 00:10:27,440
Now I want to show you some of the functions that we have been using forever.

129
00:10:27,450 --> 00:10:38,600
Now one of them is the first I would say my name is equal to a string and then the other thing is either

130
00:10:38,810 --> 00:10:39,730
for instance.

131
00:10:39,830 --> 00:10:44,440
So that is essentially a function a function for a string.

132
00:10:44,600 --> 00:10:49,810
So just like for now in view controller I added my own function.

133
00:10:49,850 --> 00:10:56,550
Somebody has programmed something that is called is equal to a string that receives a string.

134
00:10:56,570 --> 00:11:04,040
Just make it here and returns a value that is Boolean or functions didn't return anything they want

135
00:11:04,050 --> 00:11:04,290
it.

136
00:11:04,290 --> 00:11:08,720
What that function essentially returns something it's the other one.

137
00:11:08,720 --> 00:11:16,800
Another one would be and this is throwing my a c r my A C R and that is going to be assisting a string

138
00:11:17,030 --> 00:11:17,860
format.

139
00:11:18,080 --> 00:11:25,460
Again this is a function somebody has programmed it's somebody out there or another one with B and S

140
00:11:25,550 --> 00:11:26,730
dictionary.

141
00:11:27,050 --> 00:11:37,370
My dictionary is this dictionary dictionary with objects and keys for instance a gain a function that

142
00:11:37,370 --> 00:11:40,360
somebody has programmed for us and that is how we call it.

143
00:11:40,410 --> 00:11:45,750
And then we call it that tells us well there's an argument that you have to feed me.

144
00:11:45,770 --> 00:11:50,110
So with that in mind let me just quickly recap this.

145
00:11:50,270 --> 00:11:54,570
Here is again the same thing I had written earlier on.

146
00:11:54,770 --> 00:12:04,650
So return type that is a return type of function name an optional call.

147
00:12:04,800 --> 00:12:05,230
All right.

148
00:12:05,240 --> 00:12:08,990
You meant type arguments.

149
00:12:09,020 --> 00:12:12,620
Name that is how a function is produced.

150
00:12:12,660 --> 00:12:15,070
And that is how we use it.

151
00:12:15,210 --> 00:12:18,830
Again we will be writing functions in more details in the future.

152
00:12:18,830 --> 00:12:26,320
But for now when you're using functions keep your eye on the types that is returning and the arguments

153
00:12:26,320 --> 00:12:27,290
that it's accepting.

154
00:12:27,290 --> 00:12:36,530
For instance if you have something like this immutable a string my A C R and that is going to have DNS

155
00:12:36,540 --> 00:12:40,870
and mutable a string the string weight or string.

156
00:12:41,010 --> 00:12:45,210
You see it says it's instance type return.

157
00:12:45,210 --> 00:12:46,950
So whatever is its own type.

158
00:12:46,950 --> 00:12:49,010
It will return the same thing.

159
00:12:49,050 --> 00:12:55,550
It will return and ask new to the string and it needs non-call and it's a string so you have to feed

160
00:12:55,570 --> 00:12:57,330
me with a string.

161
00:12:57,400 --> 00:13:02,210
If I do that I say well I have to give you a value or something.

162
00:13:02,220 --> 00:13:04,860
Doesn't matter one way it cannot be null.

163
00:13:04,860 --> 00:13:07,230
It has to have an actual value in.

164
00:13:07,500 --> 00:13:13,920
So keep an eye on those and you get more familiarity and you get more confidence in dealing with functions

165
00:13:13,920 --> 00:13:19,530
and calling methods and from different objects in your Iowas program.

166
00:13:19,920 --> 00:13:21,740
So that's about that.

167
00:13:21,750 --> 00:13:28,390
Let's keep it up to here and we'll move on to our next lesson which is actually a very interesting exercise.
