So will come back again in this lesson we're going to talk about functions in swift which are a rather
important topic and although they're quite important they're actually very simple to use in swithe so
I'm going to quickly show you what they are and what they mean.
Let's go to Scott.
And this time I'm actually going to go and make a project.
So once you put an eye in here and my quest command line tool if you make sure it is this language is
free.
And when I call it functions even if it's true.
OK.
And now create.
So functions like strafed are in terms of the principles are exactly the same thing as Objective-C and
most other programming languages they offer us a way of modular rising or code that also offer us a
way to call a same piece of multiple times.
And they help us categorize our code.
So imagine I want to make a function in the past if this was objective c we would do something like
this.
We would do with Dasch depending on whether it is a constructor or not.
And then we would say return type and then you would say name of function and then we would say a column
and then we would say argument type and then would say a man's name.
And then this whole thing would continue would have multiple arguments more human type so on and so
forth.
In swift It's actually a lot more streamlined.
It is very much like most modern programming languages.
So here is how I make a function that doesn't have any arguments doesn't return any arguments.
So I'm going to say fine.
My function doesn't have any arguments doesn't return anything.
And here is the body of my function.
If your function doesn't return anything that's kind of like your void function so it would actually
either void in here if you want to.
Well you can just completely drop it so this function whenever it is called It just does something such
as What if I saved print hello and then somebody in here call my function that would print hello for
me.
Let's see that in practice
that says hello and something you probably already noticed is not on like objective see that we had
to say sell for and then self name of function and then bracket.
In Shreeves we just call the name of the function with its arguments.
In this case there are no arguments.
This is of course giving an error simply because it's Objective-C.
It doesn't work in here.
So removed that.
How about we now add an argument for a function.
How about we are an argument that receives a name and says hello data name.
So I'm going to go in here and say well I have something called input's name of that type of string.
So my function has an input name of the type of string and whenever you want to print to say hello and
then we interpret it in name.
So whatever input name we have got them we say hello is space plane.
So they call a function like this.
I would simply say my function and data you have to enter name name.
I'm going to say that E-Myth name is a war that's wrong not
perfect it says hello worth.
Hello comes from within the body of the function.
The word comes from the argument.
Now what if I had multiple arguments with you then just have the implant.
Well what if my heart in name and the last name of that type of string.
Well it would be exactly the same thing it would be hello Whatever say is space and then another one
eem last name and then in you know I had to actually enter the other piece as well.
And I would say hello ward.
I don't know that's a weird last name to make right now.
Hello world.
All of us are good.
I got there.
OK.
So hello we're off I was on the air with preens.
Hello world.
I was so so far we had a function that receives some argument or it can do whatever you can imagine
between them and give us a response.
For instance if you give a function that is going to multiply two numbers you would simply be lets say
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.
And then I would say do this lets the rest of the Im a multiply by Gimple B and then say print and in
the print say raise e is and then I would do one of these.
What is raise raise is this.
And then if I wanted to call that function I would say my function and it says hey you have to give
me one guy called him put eight of the type integer and other guy of the type you know integer by the
name of input B.
I'm going to say 5 by 17 so far is five by 1785 I know top 5 y.
So it's fun.
It is right.
Yes it is.
I felt like second I doubted myself.
So yes.
So that's how we call a function that has multiple arguments does the same something.
And then once it does that thing it you know it doesn't return anything the return is still void.
So what if we wanted to actually get the return.
What if it didn't want this guy to preens there is rather than that we wanted to see the actual raise.
So here's how you would do that.
I would say well this guy now has a return type of integer and this return type of integer has to be
returned the somber sort of return address.
So once again I have a function two arguments as input one integer has to be the output Offut or outcome
of this function and I'm going to say whatever you have to do mathematically or otherwise and then return
that value.
So essentially and this is a very important point.
This goes to the core of programming if you're new to programming if you have done programming in the
past you would know this very well.
But if I was going to enter values such as two and five we know two by five becomes 10 right.
So if I wanted to call my function here I would say my function and I would say values are 25.
But then this is just like saying instead of my function in a middle of your code you have written 10.
There is really no way that this thing is actually being used right now because it returns a value.
So you have to do something with it.
What could you do.
I don't know you wanted to send it to be your registration for a user.
You want to send it to find the value of the number of the other note listings that are associated with
a certain user in a real estate application whatever it is you are trying to do.
But you shouldn't just call it in here.
The return is like numeric value because the return of the S would be 2 by 5 10.
Is as if I wrote ten in the middle of my code which doesn't really make any sense what does make sense.
Is thing this saying preens.
And then in the print I would say let's call this function say that is what we want.
So that would be a much more acceptable formula.
It would be print whatever you find in here.
And if I run this now it will give me 10 just like how we expected it to be 10 perfect.
Or you could you know in here you could have a variable that is equal to that function.
So when you're defining and declaring your function and you should have a clear plan for what is the
arguments that you're entering into them and what are you getting from them.
And why do they match there for whatever reason.
For instance I could give you an example.
Imagine your function was meant to remove the factors that you don't want.
For instance you wanted to look at a string and he was like a clean string.
So I would say this is an input string.
And then the return is a string.
And then in here I don't know how you would do this you would say that arrays a string would be empty
at the beginning and then we would do some cleaning.
And then we would have it here and read a string.
So what is happening in here is that when we call our function in protesting of the type this thing
of course when we call her a function and we give it this string it cleans it up and gives it back to
us and then maybe just maybe if you remember from some of our previous lessons this was the string you
wanted to pass to the next view controller for instance.
So something like that would be would be you know acceptive or maybe you know you have a my colleague
next the view controller.
And the next few Kuntar has a receiver that dad thought that would become mine find shen.
And then my function would have sent him first string.
I'm writing this in a comment because this just doesn't make any sense.
I don't have an X controller.
This is just for the sake of demonstration essentially.
So my impis dream would be like I don't know my text F.L. the text.
So it is values that I don't know if they're clean or not.
And I would send it to this my function he would do some Kooning he would return a good one clean one
to me and I would push the clean one into the received data of my next controller or something to that
effect.
So so far we saw functions that they can accept arguments they can return values or they could have
neither of those two.
But what I wanted to have a function that returns more than one value.
What if the situation that I had was this I wanted my function to receive value and let me know whether
the value it has received whether it was a positive or negative value and also returned the positive
version no matter what.
So essentially here is what my function should do if I give it minus five.
It has to get dive to me with five on a false saying the positive version of this is five.
And it wasn't positive on its own.
And if I gave it 17 it should give me like 17 and saying true.
So I want some sort of a structure that allows me to return to more than one value.
And that is something we saw recently in our previous lesson if I'm not wrong and that's it.
So let's see how we could rewrite this entire thing.
So fine.
I mean that's our input input integer if is of the type Integer.
And I have a return which is an integer and a boolean.
Now let's see what you have to do.
It's very simple.
So if input integer was larger than minus one then I have to have a value card as boolean that's going
to be by default.
I'm going to say that to be true and then I'm going to say if it is larger than that.
Don't do anything.
So if it is smaller than zero Reza Boullion has to become false.
That's one part of it.
I also need the return value.
So I'm going to save our arrays.
Integer is by default going to be input in.
And then if it is below zero gets rather integer multiplied by minus 1 so change its signature.
Once you're done on through with that say return.
What do I want to 10 or when I returned the AS INTEGER and I wanted a $10 billion.
So imagine if a number such as 5 goes through this thing.
It says that is five is smaller than zero.
That's not happening.
So 5 billion is going to be true.
And as integer is going to be five.
So let's test this out.
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
a let.
State is going to be.
What is it going to be is going to be determine input value and the input value is going to be my in
dots one the boolean one.
So that's the state.
And then let's pause number the positive value of.
Here is the term.
And then again does zero the zero.
So I find the a state of faith if it is minus five.
I find out the positive value of it was minus five it becomes five.
If it is five days five then I would say print.
And here's what I want to print.
I want to say the number that they had which is my eant laws.
What was it.
It was the estate and is now let's find out what I know this that this earlier a very crude version
of doing this thing but you understand the concept behind it.
I hope so let's say let's see what you get.
So if I wrong this is God tell me minus five was false and he's now five.
And if it was 17 it says what does it say 17 was true and is now 17.
Perfect.
So again I managed to get two values out of a function.
You could also call this one.
You could call this one on the sign the number or whatever you'd like to.
And then this phone call it a status.
So just like two people that we could call its values or variables.
You can also do it here.
And if you had those you could essentially go in here and say well that one is that and this one is
the stop this you don't need to rely on the indexes.
You can't rely on the actual names and would perfectly work the way you expected.
Before I finished this lesson I want to tell you one more thing about functions in swift which is again
a unique thing to swift and that is functions that are nested inside each other.
So I could have a function called Find the one that doesn't have any humans that have any return even
though they don't really matter.
And inside that I could have another function call it on nested one or anything that you want to call
it.
And then an inside one maybe does something for instance it says this is printed from inside.
All right.
So I have a function that is nested inside this one.
And since it is nested inside function one I can actually say nested one or mess that find Let's call
it Nesse that function so now I have a function which is nested inside another one and I can call it
in here.
It is primarily used if you have a very long function and for some reason parts of it they can be categorized
but you don't want to expose them outside your function you only want to keep them inside to be very
honest with you I don't really use this very often I can say that throughout the years maybe I've used
it a handful of times so don't worry overly about you know what it is and how it works it's obviously
best if if you learn everything and you know that they exist but you have to craft your own way of doing
things and every once in awhile look up on line and look at other people's code and try to get a sense
of you know how they are doing things if they're raised better now.
We need a way to call these functions.
And of course we can call this one because that one is not even accessible.
We don't have access to that guy only.
These guys have.
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.
So that's about not our.
We talked about functions that they have no arguments.
We talked about functions that they have some arguments we had we talked about functions that they had
no return types they were voids or the ones that the vendor actually returning something.
And of course if they're returning there has to be a return for them.
Something has to come out.
We talked about functions that they returned to post instead of single values they return multiple values.
And if that's the case again we have written a similar id..
The chance really you know ignore any of them and be fine.
We talked about having nested functions.
Here I have an error.
And that's because of this.
Because of that.
And then in your argument I'm going to say Ninh I don't want you know in Arctic anything to it.
So that's about that that's about functions.
Let's move on to our next lesson.