Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.10 Methods

Strictly speaking, methods are nothing more than functions that are defined within a class or structure. Within the Swift language there is a little more to take into consideration when working with these methods. Let’s take a look.

Related Links

2.10 Methods

In this lesson, we're going to introduce the concept of methods. Now a method is really nothing more than a function that lives within a class. Now really those two things are oftentimes referred to interchangeably. And I, I won't make a statement either way, you can call it whatever you'd like. But just know that typically when you're working in Swift or even other programming languages. If you hear somebody mention the name method then that typically means it's just a function but it's contained within a class. So if you kinda keep that in the back of your mind it should be fairly easy to discern between the two. So in the previous lesson when we introduced properties, this was kind of what we were left with. So I introduced this concept of running totals so we could kind of talk a little about restricting access and maybe custom getters and setters. But really for our purposes that's not gonna be overly important, we're just going to stick with our total. And we'll state that var is going to be total and it's gonna be an integer and we'll just initialize it to 0. So here's our basic calculator right now. Now it doesn't really do anything. It has a total but that doesn't ever change just 0 or we could set it to something if we wanted. And then we have PI setting there where we have a constant PI value of 3.14 but there's nothing going on here. Well, what happens if I wanna add two numbers together or I wanna subtract two numbers or do multiplication or division? How do I add that functionality into my calculator class? Well, it's quite simple, as I mentioned before we simply write a function. So let's start with the basic most basic of calculator functions and let's just do an addition. So were going to create a new function and we're gonna call this add. And this is gonna take in two parameters, we want to add two parameters together. So in this case we'll say that we have i1 since this is gonna be an integer we'll call this an int. And then we'll have another parameter i2 which is also going to be an integer. And then at this point we could choose depending on our implementation to return a value here. But I want this to function like a calculator does where it's going to continue incrementing a total, so we can see what the value is as we continue to add these operations to it. So that being the case, I'm not going to return a value here. I'm simply going to then put the body of my function here and the functionality here is merely to take total and set it equal to i1 plus i2 and that's it. So let's go ahead and see what we have now. So I can now say that I want to create a new instance of my calculator class. So I'll say myCalc is gonna be equal to Calculator and now I can see my total myCalc total is going to be 0. And now I can say myCalc and I want to add. So let's say I wanna add 2 and 2. In this case now, we have, we can see over here on the right, on our little inspector that our total four or we could come down and say and now we can see it there. So now, if we wanted to reset that to be something else, I can say equal to 0. So that's a little clunky but that'll work. So why don't we add a helper method here that's going to reset this back to zero. So this will be a very simple operation. So we'll create a function here called reset which is not gonna take in any arguments at all or any parameters. It's merely going to reset the value of total back to 0. So now we don't have to worry about doing this as explicitly setting it to 0 we can merely call our reset method and then we'll see that after the fact our total is going to be 0. So that's pretty nice not a bad thing. But now the problem is going to be that every time I call this add method it's going to overwrite whatever is in total with the value here of what these two things are. Which is okay, I can do that but what if I want to just increment that particular, that total by some other value. So the example here would be if I were to take my calculator, do 2 plus 2 the screen says 4. But if I were to say plus 4 it would increment it to 8. So I wouldn't start over and just have 4, so how can we do that? Well let's create another method. So you'll see here that I can create another function that has the same name that's gonna have different arguments in it. So this is kind of similar to what you might call overloading in other languages. I could say now that I wanna take in a single integer value. And within the body here I'm going to increment, so I'll do plus equals on total i1. So now I have two methods, I have add so if I pass in two parameters it's going to overwrite total with that value. But I could just use the add method that takes in one that will increment total by a certain amount. So if I were to back up a little bit as you can see here, I have added 2 plus 2 which gives me a total of 4. But now I could also say myCalc.add and I could just add in 4 and now the total is going to be 8. So now I'm able to have that running total going on. And you can continue to do the same thing with all the different subtraction and multiplication and division, all that kind of good stuff. So we're creating here a nice little instance methods which means they're methods that are available to you after you've created a new instance of your particular type. So in our case calculator. So one other interesting specialized method that you're gonna find yourself probably using quite a bit, depending on what you're looking to accomplish is this concept of this initializer. So this by default, every time you create a class or a structure you are given an out of the box default initializer or constructor. Depending on what language you're coming from that takes 0 parameters or 0 arguments as you can see here but what if I wanted to create a class that. Did some sort of custom initialization based on some parameter or based on something I'm passing into it that's set up the current state of my class or my instance of a particular class. So let's just say for some strange reason when I create a new instance of my class, I don't want the total to start out as 0. I want it to be some other value that I specify and the way that I do that is by specifying a specialized function or a specialized method in here called init. So I can specify that I am going to create an init method in here and we'll just start out by using this, this base 1. So as you can see here, this is the default that is used or that is generated for us by Swift to give us this particular piece of functionality. So let's say we wanted to play with this a little bit and let's say within this initialization, I actually wanted to set my total equal to 1. So now after this re-runs, you're gonna see when I create a new instance of my calculator and I ask for total now I'm getting 1. So that's the basic concept behind what's going on when I create a new instance of my class using an initializer. But I can have this do any sort of custom logic, I want to set up this particular instance of my class I can either, I can even pass in a parameter. So let's say in this case, I wanted to specify a starting, a starting total. So we'll say that I have a starting total variable here that's going to be an integer. So now I can pass in a particular value to start off the total within my calculator but one thing to note here. Now in some other languages, you are given a default constructor out of the box all the time. Well, what happens now that I have specified a, an initialization function here that has to take in a value here, that has to take in an integer value. I am forced to do that so I'm, I no longer have that default initializer. I have to explicitly add that in later, should I want to. So as you can see here I'm getting an error that says this needs to take in a starting total or a starting value. So I could specify in here that I'm gonna give it 0 but I actually have to give it the name here. So this is gonna be a starting total and there you go. So now we're back to a point where I can actually pass in some data but the problem is is that I'm not using that anywhere. So now I can set the total equal to my starting total. And as you'll see here now, everything is going to be reset back to the point where total starts at 0. So now I said before that once I've created an initialization function here or method that's going to take in a value. I've kind of lost that ability to have that default initializer. So there's a couple different ways you can get over that. If it's as simple as something like this where I have a single initialization I could merely give this a default value of 0. And if I do that because it's a default value, I don't have to specify a parameter into my calculator it is just going to use this default. Now if you don't wanna do that or if you have multiple initialization methods in here then you would have to kind of overload this concept and create that default initializer that doesn't take in any values. And maybe do all of your basic initialization there where I would set this equal to 0. So now, I can call this initialization function here passing in 0 to start it off at 0 or I could pass in nothing. And in this case, it's going to use this initializer to initialize or do any sort of setup for this particular class, should I not pass in any data. So that's the basic concept behind the functions or methods that are contained within a class. And you can create as many of these as you would like, with as many inputs and output values as you would like and continue to build off some wonderful functionality in your custom types.

Back to the top