image
Functions
Start course
Difficulty
Intermediate
Duration
2h 50m
Students
72
Ratings
5/5
Description

This course will provide you with a comprehensive understanding of the fundamentals of Swift. We're going to learn about variables, constants, arrays, dictionaries, sets, if statements, and more! 

Intended Audience

This course is designed for anyone who wants to:

  • Learn about iOS development and coding
  • Move into a career as an iOS developer
  • Master Swift skills

Prerequisites

To get the most out of this course, you should have some basic knowledge of iOS.

Transcript

Hi. Within this lecture, we're going to start learning about functions. So, in order to do that, I'm going to create another Playground because this function deserves actually their own Playground. Okay. This is an important subject. I'm going to go for a 'Desktop' and my 'Playground' 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 word func. Okay. You can name your function whatever you want like this, myFunction. And if you open and close the 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 myFunction 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 will be much more efficient for me. Like if I say print ("my function"), okay, I can call this whenever I want. And with prints, of course, it doesn't make sense. But I'm going to show you much more useful ways of functions. Don't worry. Right now, I just want to show you something. If I run this, what will happen? Do I see any log inside my Logcat 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 the 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 text or print, okay, I can see it . 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 myFunction over here, then this will be called in the line nine.

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 code more readable, more in a structured way. Because for example, you have to do some 1000th line operation and you have to do it twice or three times. You can write 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 input, and we can return output with the function, and that is basically why we use function in most cases. But again, if you understand the logic here, I create myFunction and I call myFunction, then it's okay. Because this is the basis of the functions. We create a function. We write whatever we wanted 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 sumFunction 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 sumFunction 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. I'm going to call the other one y, and the y will be an integer as well. Right now, when I call sumFunction, it will 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 result. Okay, like this if I can write print(x+y). So, let me test this actually. Let me call sumFunction. And as you can see, this calls this actually asks for an x and the y.

So, (x: Int, y: Int) So, if I do something like this, it will give me an error because it will 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 will add them together and display the result to me. So, what if I want to return some results 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 error 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 Void over here. Void means nothing. Void means emptiness. So, it returns nothing. But if I say Int, then this sumFunction will give me an output of an integer. Okay. Then I can use the result of this sumFunction in an integer way. So, if I have to give an integer to someplace in my code, I can call this sumFunction, and I can make it return x + y. So, return means return the output. So, if I call sumFunction, 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 integer Turkey. I can make it even a new variable and I can make it actually be equal to sumFunction like myFunctionVariable, okay, sumFunction( x: 10, y: 20). Because myFunction label is actually an integer at this point. So, I can do that. So , 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? Now, of course, I can return Booleans or strings if I want. Let me create a logicFunction 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 Boolean 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 > b, then return true. So, else, return false. So, I can compare this a and b. And I can return a Boolean 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 logicFunction and let me give 10 and 0 to here, and let me run this. And we can see the result is true. So, if I do this -10, then I will see the result will be false. So, this is kind of cool. We can create our own functions. We can create our own pieces of code that does anything that we want.

So, we're going to use those functions a lot. So, let me do a little bit more example. Rather than returning Boolean, let me say 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 Boolean. You can return integers. You can return nothing void if you want. You just don't have to drop this array of thing. So far, we have been only working with variables.

So, right now, we're working with functions, and the next step actually would be to work with classes. Okay. So, classes will consist of a lot of functions or something, but we're not going to learn about classes right now. We're going to do that in a separate section, in a intermediate swift section. So, for right now, we're going to stop here. And within the next lecture, we're going to discuss optionals.

 

About the Author
Students
2077
Courses
55
Learning Paths
3

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics