Hello and we're coming in on this list then we're going to have a quick introduction to the use of functions.
We will have a section on lessons about the use of classes and object oriented programming and functions
in the coming sections.
But for now I thought it would be useful if we get the basic introduction to what functions are and
why do we use them and how do we essentially use them and write them because we have been using them
for a while now now it kind of makes sense that we know a little bit more about them.
So functions are essentially the functioning part of an object.
An object could have properties such as when you have a label that label has a property called text
but also could have functions that does the things for us.
We've talked about this in more detail in the future but for now we need to know how do we use them.
So let's jump back into ex-cult and for this project I'm actually going to use on all U.S. applications
simply because the way that we write functions in command line tools is a little bit different than
the ones that we do you know in iOS up occasions and the command line is a little bit out of the scope
of this course essentially.
So I'm going to make a new iOS single view application and I'm going to call it functions by voice.
So let's see what do we mean by functions.
I'm going to go in here.
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
so we have a cleaner and tidy you know code.
Not very overwhelming.
And well I'm going to do is I'm going to say and Aslaug Hello.
So what this does is very similar with a command line tool.
When the app compiles on the application essentially launches in the simulator VBN see the command debug
console and that's going to show us the value that we try to print.
So now down in here it says hello which perfect Don't worry about that little thing up there.
That's a machine printed you know.
If if you have that depending on the application and the version of the simulator that you're using
you might see different things.
But this is the thing that we are seeing now here I want to add a new function.
Let's see how do we do that.
And then I'm going to explain what it does.
I'm going to call this one my function and this is what my function does.
It does this in a long Hello.
So I currently have a new function in my project essentially in the implementation of your controller.
Don't worry if you don't know what that is we'll talk about this in for more details in the future.
But I have a new function which is actually quite similar to the Lord.
You see it has a dash avoid the view of the alloy.
I have it.
I shall watch it.
My function to call that I'm going to say self which means this column and file this client I see my
function.
So now I'm calling the function from self.
So what do you think is going to happen when I run this application now when I relaunch it.
Viewed that lot is going to happen because that's the one that happens by default and that the fourth
is going to say Well go on find this function and run it and it finds this function says hello.
Which is exactly what we expected.
Now let's get a sense of what the function is or how it works.
Every function essentially has these few things in it.
I'm going to write them in comments.
So it has a dash that shows it is not a construct or we'll talk about this in the future.
Let's imagine there's always a dash and then within the Qantas's is the return type.
It says what does this function return for us.
And then there is the name of the function.
And then there is a column an option or a column and then there is an all humans type and then there's
an art you man's name.
And this continues.
Let's see what they have written it seems a little bit complicated.
It actually is not is saying that there is a return type.
What does this one pretend my function doesn't retain or anything it's return type is avoid it just
does something.
It's not like my function is meant to calculate somebody and give me a number.
It simply means to print hello and that is saying what's the name of the function that someone.
And then some optional arguments.
Are there any arguments that you want to enter into it.
Now that's a function that has an argument.
So I'm going to go in here something right again boy and envoi I'm going to say my new function data
has the identity of the type and this a string but I call it input.
So let's see what this one means.
It means there is a function called my new function.
I receive is something called input.
So it is going to call that going to say cell for my new function.
And it says hey you have to feed me Genesis 3.
I say OK this thing is a mirror.
So now this function receives something but you haven't really done anything in here which we call the
body of the function body of the function.
You haven't done anything in your heart what we print that we say and the slogan perience what ever
you will find in input.
So whatever is the value that someone has fed into this function gets into the slot and gets printed.
Let's see if that works.
OK.
So it says hello and then the other function.
This fine print.
I mean that's the one.
Now bear with me for a few minutes.
It will become a lot more you know a lot easier to understand now and clean or delete all of these functions.
And I'm going to have a function that when you give it the number it returns the square effect.
So I'm going to call it war.
That is where function and my work function receives an integer let's call it input again.
And what does it do.
It says yins rez result is input multiplied by input.
That is a squared value of something and log in a slide it says it's quite easy.
What is it.
It is going to be.
So whoever calls this function and gives it a value it's really calculate the square feet and it gets
printed for us the strike in the triangle.
When I say sell a square function of eight let's run
now I have it saying a square is 64.
Exactly like how I expected.
Now let's have another function that we give it to numbers it tells us which one is larger.
So I'm going to delete all of these again.
I'm going to say I have a void in my void which is bigger.
These are obviously names coming up by solve our ints a and b.
And that is going to be in beak.
These are essentially high because you don't really need them but you want to separate your arguments.
You have to put their space between them.
Every human needs to know what is its type.
In here both of them are integer.
Maybe you're writing something that one of them is integer.
One of them is a string.
You should however mention or maybe you should have said this a little earlier.
This lesson isn't for you to learn how to write functions you will get there in the future.
This lesson is for you to understand how functions are being called and why do we use functions in general
so don't worry overly about the ins and outs of the functions we will get there in a few sessions sections
from now.
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
is the value in a D is larger.
And that is a else and this log what is the value is larger in B of course.
So let's see how we can call this in my view that a lot because the method or the function that runs
by default function runs out the value all the time.
I'm going to say self which is bigger 5 and 15.
And if you run this code now it's going to take a little while.
And that is going to tell us 15 is large because it's calling that function.
Now like I said the exact details of how do we use functions or how do we write our own functions is
going to come up sometime in the future.
What is important right now is understanding that how do we call functions and we do it using their
bracket operations in Objective-C in swift and other languages.
We do it slightly differently.
Now I want to show you some of the functions that we have been using forever.
Now one of them is the first I would say my name is equal to a string and then the other thing is either
for instance.
So that is essentially a function a function for a string.
So just like for now in view controller I added my own function.
Somebody has programmed something that is called is equal to a string that receives a string.
Just make it here and returns a value that is Boolean or functions didn't return anything they want
it.
What that function essentially returns something it's the other one.
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
format.
Again this is a function somebody has programmed it's somebody out there or another one with B and S
dictionary.
My dictionary is this dictionary dictionary with objects and keys for instance a gain a function that
somebody has programmed for us and that is how we call it.
And then we call it that tells us well there's an argument that you have to feed me.
So with that in mind let me just quickly recap this.
Here is again the same thing I had written earlier on.
So return type that is a return type of function name an optional call.
All right.
You meant type arguments.
Name that is how a function is produced.
And that is how we use it.
Again we will be writing functions in more details in the future.
But for now when you're using functions keep your eye on the types that is returning and the arguments
that it's accepting.
For instance if you have something like this immutable a string my A C R and that is going to have DNS
and mutable a string the string weight or string.
You see it says it's instance type return.
So whatever is its own type.
It will return the same thing.
It will return and ask new to the string and it needs non-call and it's a string so you have to feed
me with a string.
If I do that I say well I have to give you a value or something.
Doesn't matter one way it cannot be null.
It has to have an actual value in.
So keep an eye on those and you get more familiarity and you get more confidence in dealing with functions
and calling methods and from different objects in your Iowas program.
So that's about that.
Let's keep it up to here and we'll move on to our next lesson which is actually a very interesting exercise.