‫-: Hi.
‫Within this lecture, we are going to
‫start learning about functions.
‫So in order to do that
‫I'm going to create another playground
‫because this functions deserves actually
‫their own playground, okay?
‫This is an important subject.
‫I'm going to go for a desktop
‫and my playgrounds
‫and I'm going to create this.
‫So what is a function and why do we use them?
‫So function is actually a piece of code, okay?
‫It's a coding block.
‫And in fact, it's very easy to create one
‫if you use words func, okay?
‫You can name your function, whatever you want
‫like this, my function.
‫And if you open and close a parenthesis
‫then you can open a curly braces.
‫So whatever we write
‫inside of this function will get executed
‫when we call or something calls this function.
‫So what does even this mean?
‫It means that if I write something in here
‫like printing out something
‫whenever I call my function in my code
‫this will execute what I write inside of this piece of code.
‫So why do we use functions?
‫If I write something in a function
‫I can call it anytime I want.
‫So it'll be much more efficient for me.
‫Like if I say print my function, okay?
‫I can call this whenever I want.
‫And with print, of course it doesn't make sense
‫but I'm gonna show you much more useful ways of functions.
‫Don't worry. Right now I just wanna show you something.
‫If I run this, what will happen?
‫Do I see any log inside my log cat
‫or inside of my logs?
‫Of course I won't
‫because this function will not get executed by itself.
‫It has to be called
‫or it has to be assigned to some view
‫or some button or something
‫so that when a user clicks on a button
‫it will get called.
‫But right now I'm just creating the function.
‫I'm not doing anything else with it.
‫I'm not calling the function itself.
‫So let's see.
‫As you can see, I don't have anything in my log
‫but if I print something over here
‫like some test or print, okay?
‫I can see it, right?
‫As you can see, I see the print.
‫But I don't see my function
‫because again, defining a function
‫and calling the function is two different things.
‫So if I say my function over here
‫then this will be called in the line 9.
‫So let me run this.
‫As you can see
‫first I see the print,
‫then I see my function.
‫Whenever I call this
‫whatever I write in here, this will get executed.
‫So first of all, this is a great way to make yourself
‫make your codes more readable,
‫more in a structured way.
‫Because for example, you have to do some 1000 line operation
‫and you have to do it two, twice or three times.
‫You can write in in a function
‫and you can just call this function later on in three ways
‫so that it will get executed three times
‫rather than copying and pasting
‫or just writing whole 1000 lines in your code.
‫And this is not the only way,
‫this is not the only purpose
‫that we are using functions at all.
‫We can do so much more with functions,
‫We can take inputs
‫and we can return outputs with the function.
‫And that is basically why we use function in most cases.
‫But again, if you understand the logic here
‫I create my function
‫and I call my function,
‫then it's okay
‫because this is the basis of the functions.
‫We create a function,
‫we write whatever we want it to happen
‫inside of that function.
‫And when we call it, it gets executed.
‫So let me tell you about this input, output
‫and returning thing.
‫So you would understand
‫the function's purpose a little bit more.
‫So in here, in a function, I can get an input
‫from the user, from decoding,
‫from developer or something.
‫What do I mean?
‫Let me create a sum function
‫in which I will sum, I will add two numbers together.
‫Okay?
‫So how can I do that?
‫I can ask for two integers
‫when some function is get called.
‫In order to do that,
‫I will write them here.
‫I will call one X.
‫And I'm going to say X is an integer.
‫And I'm going to call the other one Y.
‫And the Y will be an integer as well.
‫Right, now when I call some function
‫it'll ask me to give two integers
‫and I can do anything I want with them.
‫I can just sum them.
‫I can just print the results.
‫Okay? Like this.
‫If if I can write print X plus Y.
‫So let me test this, actually.
‫Let me call some function.
‫And as you can see, this calls,
‫this actually asks for an X and a Y.
‫So X is integer.
‫Y is integer.
‫So if I do something like this
‫it'll give me an error
‫because it'll ask me for an integer, not a string.
‫I have to give some integer over here, like 10, 20.
‫And if I run this
‫it'll add them together
‫and display the result to me.
‫So what if I want to return some result
‫rather than displaying it?
‫What do I mean by return?
‫So I can return anything I want.
‫I can give an output.
‫And in order to do that.
‫I have to do it like this.
‫This will return an integer.
‫You literally draw an arrow here
‫saying that this will return something.
‫Okay?
‫So with dash and greater sign, of course.
‫And if I don't do it
‫it can just print out
‫it can just return nothing.
‫And as you can see, it shows a void over here.
‫Void means nothing.
‫Void means emptiness.
‫So it returns nothing.
‫But if I say Int, then this sum function
‫will give me an output of an integer, okay?
‫Then I can use the result
‫of this sum function in an integer way.
‫So if I have to give an integer
‫to someplace in my code
‫I can call this sum function
‫and I can make it the return X plus Y.
‫So return means return the output.
‫So if I call sum function,
‫as you can see now , it's an integer.
‫It's returning an integer to me.
‫Now, I won't see the result in the logs
‫when I called this
‫because this won't be a print statement or something.
‫This will be an integer itself.
‫I will see the result on the right hand side, okay?
‫But I won't see it in logs.
‫So this is the interior 30.
‫I can make it even any variable
‫and I can make it actually be equal to sum function
‫like my function variable, okay?
‫Sum function X 10, 20
‫because my function variable is actually an integer
‫at this point, right?
‫So I can do that.
‫So I, if I want to, I can return something with functions
‫and I can use them as variables.
‫And if I don't want to
‫I don't have to return anything at all
‫and I can do whatever I want to do with functions.
‫So can I just return integers?
‫No, of course I can return bullions
‫or strings if I want.
‫Let me create a logic function for example.
‫This will take again an integer, okay?
‫Or maybe we can call this A
‫in order to avoid any confusion and B
‫and this will return a bullion
‫instead of integer, okay?
‫So how can I use this?
‫I can check to see if A is bigger than B
‫and return true if that happens like this.
‫If A is greater than B,
‫then return true, right?
‫So else, return false.
‫So I can compare this A and B
‫and I can return a bullion
‫instead of returning an integer.
‫If I want,
‫I can return a string.
‫It really doesn't matter.
‫I can just have it in a way that I want.
‫So let me call this logic function
‫and let me give 10 as 0 to here
‫and let me run this
‫and we can see the result is true.
‫So I, if I do this minus 10
‫then I will see the result will be false.
‫So this is kind of cool, right?
‫We can create our own functions.
‫We can create our own pieces of code
‫that does anything that we want.
‫So we are going to use those functions a lot.
‫So let me do a little bit more example.
‫Rather than returning bullion
‫Let me see, greater
‫let me say greater over here.
‫And let me say less.
‫So we are returning a string right now, as you can see.
‫So we have adapted this function in less than a second.
‫So this is like returning less right now rather than false.
‫So you can create functions
‫you can adapt them in a way you want.
‫You can return bullions.
‫You can return integers.
‫You can return nothing, void if you want.
‫You just don't have to draw this arrow thing.
‫So far we have been only working with variables.
‫So right now we are working with functions
‫and the next step actually would be to work with classes.
‫Okay?
‫So classes will consist of lot of functions or something
‫but we are not gonna learn about classes right now.
‫We are gonna do that in a separate section
‫in a intermediate swift section.
‫So for right now, we're gonna stop here
‫and within the next lecture
‫we are going to discuss optionals.