Check out a free preview of the full iOS App Development with Swift course

The "Functions" Lesson is part of the full, iOS App Development with Swift course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates writing functions in Swift, setting function return types and the option to omit the return keyword. How to utilize Xcode to help troubleshoot argument errors and how to handle optional arguments when defining a function are also covered in this segment.

Preview
Close

Transcript from the "Functions" Lesson

[00:00:00]
>> Now talking about function, here comes something that you're saying at this point you're saying, hey, that's simple, okay? That's straightforward. Well, wait a minute. So, a function is just a function. So that's simple, you define a function as you're used to, just with a func keyword. Some important things about functions, we have return types.

[00:00:21]
So a return type is using what is known as the thin arrow. So in JavaScript, we use the fat arrow with equals. This is called the thin arrow. So it's just a dash, okay, and greater than. And then the type, and when you have the type, you must return a value of that type, okay?

[00:00:42]
So that's simple to get anyway. So, something interesting, let's say for now interesting, is that you don't need the return keyword. So, in fact, that actually works. So even, you can put this in just one line. And there is an implicit return in that particular situation, that sometimes is useful, okay?

[00:01:11]
But sometimes, it's better to add a return keyword. It will add more, let's say, clarity to the code. So it depends on the case, okay? But it's interesting. So I did mention that some things will get weird. And I'm not sure if I will achieve the goal to make you like this today, because it needs a couple of days, okay?

[00:01:38]
But you will get there, okay? I promise you will get there. So I have a function here that looks pretty simple, sum a and b. It receives two integers, it returns an integer, so I have two arguments. Remember we use column for the data types. And then we have a return type, that returns just a+b.

[00:01:58]
Of course, I don't need the return, I can just say a + b, okay? Some of you may be thinking about a lambda expression but it's not like that, we will get into that idea in a minute. Okay, we just return a+b, but how do you call this?

[00:02:14]
So, you're expecting to call sum 10 and 5 like on any language. But no, it's complaining that I don't have or I'm missing argument labels, a and b. And if I click here on the error, so you just click in this error icon, it opens a little window where we have a Fix button.

[00:02:46]
So if I click Fix, it will fix this for me. And this is how it looks like. So, sum a and b. So we actually need to pass, should call the function using the argument names, and that's weird. But it's part of a pattern, okay, that can be customized.

[00:03:11]
For example, the idea is that in Swift, we are going to change how we call functions. Because what's the name of the function actually, it's not actually sum, it's sum a, b. In fact, if you look at the colors, you can see that S-U-M and a and b, they have the same color.

[00:03:36]
Okay, can you see that? So I have the same color here and here. That means that's the function name. If we merge everything, that's the function name. So why it's like this, so yeah.
>> So does it mean that you can create another function, sum with different arguments?

[00:03:55]
>> Yeah, that's correct. So if I do this, sum a, b, and c, and we return, for example, a and b, we call the other one, okay? And then plus c. So, is this actually an overload, a function overload? No, it's not a function overload, it's actually a function with a different name, sum a, b, and c.

[00:04:26]
It has the same prefix, if you want, okay? But we need to see these argument labels are kind of merging with the prefix, with the function name, and they create a larger function name.
>> What if we did this and made the c optional on this one?
>> Okay, yeah, that's a good question.

[00:04:51]
So, the question is what happens if b is optional?
>> No, but I wanna create those two that you did, a, b and a, b, c. And the second one, I'm gonna have optional c. It's gonna make a mess for my sum.
>> Which one is optional?
>> The c.

[00:05:03]
>> The c, so optional means this?
>> Yeah.
>> So in this case, there is a problem. Let me explain why. Let's go back to this example. If I say that b is optional, here it's complaining because maybe b is not there, okay? So, well, let's do this for now.

[00:05:22]
It's wrong, but let's do it for now. That when I call in sum, I can actually pass only a, I don't need to pass b, because it's optional. I can also pass nil, an explicit nil, and it will work, okay? But also, I don't need to set a value for it, okay?

[00:05:43]
So yeah, then you have a problem if you do that. Okay, so I know that this is weird, okay? But believe me, I now like this. So it takes time, but the whole idea is to, I mean, it's pretty common these days to find code like this. So you have a print, let's say you have a customer.

[00:06:06]
So printCustomer, and then you pass a string with a name, and then you have a couple of variables like this. So you read that code. What's 2 and 3? x and y, and 12 font size? So I don't know. When you read code, okay, you don't know about the arguments.

[00:06:31]
So the whole idea here is that first, I shouldn't call the printCustomer. I can probably say print and then create customer. And then I can say with X and Y, you define how explicit you are, fontSize, okay? The other two, I don't know what that is but let's say headerSize, whatever, and visible.

[00:07:01]
So, this is how Swift functions look like. And we write this in different ways. For example, we don't create a function called add, let's say Product. We create the function and that receives a product as an argument, okay? Let's say it's an integer, we create a function called add, and the product is, Then the name of the argument, the argument label that's actually a name.

[00:07:31]
So when we call add, it says add product, Like so. What if you say, well, you know what, when I have sum, it's actually pretty simple, two integer, so I don't wanna have i, a, and b. Well, if you want, you can customize these labels. So you can keep a and b as local variables, the arguments, but you can change the argument, the visible argument label to the caller.

[00:08:02]
So I can say an operand1, operand2. This is even worse now. But anyway, so when I'm calling sum here, it's going to complain, that now, these are the incorrect labels. The labels are operand1, operand2. So I still call them a and b internally, but then I change how I'm calling this from the outside of the function, okay?

[00:08:31]
And now you will like this, you can use underscore. If you use underscore as an argument label, it means you don't want an argument label. And now it looks like a JavaScript function. So now he's going to complain, there is a space, okay and mandatory space. So now it's saying no, you don't need argument labels, it's just 10 and 5, okay?

[00:09:02]
So you can still go back to the functions as you're used to name them, but you need to explicitly define an underscore as an argument label. And you can mix a match, for example, in this case, a nice swift name for this function will be sum with. So the first one is underscore, and the second one is with.

[00:09:29]
So now, when you read this, it's sum 10 with 5. That's how we want to name functions in Swift, so you can read it in English.
>> Very expressive.
>> Very expressive. The idea is to have clear code, simple to understand when you look at the code, okay?

[00:09:56]
Any question? Yeah.
>> So, when you're writing the function signature, are the parameters like on a keyword and positional? Could I call sum putting the with first?
>> That's a good question. So can I call sum and with first? No, unless you have optionals. You cannot change the order, but you can omit one in case it's optional, but you cannot change the order.

[00:10:26]
So if you're setting two values, you must use those values. Other languages such as Kotlin, they let you do something similar to this. Kotlin is the one that we use for Android today. And you can actually change the order when you're calling the function, but not here in Swift.

[00:10:42]
But you can omit some of the arguments if they are optional, okay?
>> Is there a way to run a formatter on Save, something like Prettier?
>> So, when you're calling, I'm not sure if it's one we're calling. So typically, when you have a lot, you go to a new line.

[00:11:06]
In fact, we're going to see that in Swift UI later. You go to a new line, okay, or also here, go to a new line, and then you send more arguments. I guess that's the question about, I'm sure.
>> No, it's just like when you hit Save-
>> Okay, no.

[00:11:24]
>> And everything auto formats, or like Go has go format.
>> No, you don't have an auto formatter automatically in Swift. So what you have here is that if you, I mean, I'm an old school developer, so I don't typically use, though, I don't like them actually. Those tools that will re-indent everything when I'm saving, I don't like them.

[00:11:45]
But you have here is a shortcut that you can use like a key shortcut. In the menu, if we go to Editor, okay, you have here a section for a Structure. On your Structure, you have Re-Indent, that is actually Ctrl+I, okay? So Ctrl+I, or you have more options here, okay, but Ctrl+I will actually re-indent the selection or everything according to the rules that Xcode has, okay?

[00:12:14]
So it depends on the case how this is going to end up.
>> I just looked in there, it's like a Swift formatter but it's an open source project.
>> Yeah, so Xcode supports plugins. Unfortunately, the community is not as big as VS code. I mean, the amount of plugin is probably 1% more or less compared to VS code.

[00:12:37]
So the amount of options that you have is not the same, okay? So you will feel a little limited in that sort of things, I mean. Can you use VS code to write Swift? Actually you can, but I mean, creating the iOS application will actually be a little difficult because you don't have the preview and tools that we will see later.

[00:12:58]
But the VS code supports Swift, so you can still write some code there if you want to, and it's going to be synced with Xcode.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now