What are methods in programming? How do I use them? Why Should I care? Today I will be answering all these questions. We are going to look into what methods are in programming. Methods is one of the things that makes your code really powerful. And this is also a major part of what is called “Object Oriented Programming” (OOP). It is a concept that is necessary to have a grasp on. Don’t worry though, by the end of this article you will have a much better understanding of what they are and how to use them.
What are methods?:
There are three main types of methods; interface methods, constructor methods, and implementation methods. What we will focus on today is implementation methods. Which is the main way of using methods. Once you understand this you will automatically understand the other two. Therefore, don’t worry about learning the other types for now. Also, you may have heard of ‘functions’ in programming. A function is the same thing as a method. Just a different name. I wanted to address this early on in case you were wondering.
Here’s an example of what a method might look like:
String getMyName() {
return "Kalle Hallden";
}
What does this method do? Let’s break it down. This method contains four important parts:
Method signature
getMyName()
Method body, everything inside the curly braces {}
{
return "Kalle Hallden";
}
Return type
String
Return statement
return "Kalle Hallden";
Method signature:
What this method does, if explained in natural language, is that it gets a name. Specifically it gets my name. This is the first clue to what the method name should be. That is; what do you want the method to do? You can name your method anything you want.
Example:
String getMyFirstnameAndMyLastNameBecauseIFeelLikeIt() {
return "Kalle Hallden";
}
However, for cleaner and more readable code the convention is to give it as short of a name as possible. While still maintaining as high readability as possible. The way to pick a name is simply by first figuring out what you want the method to do. Ideally anyone should be able to only read the name of your method and understand what it does.
This serves two purposes. The first one being that it helps future you understand what past you has written. Past you is submerged into the code. For past you everything makes sense. Future you has worked on ten new projects in four other languages. Future you does not remember a thing from that code. If past you creates good method names, future you might be able understand how the code works. The second purpose is that by writing readable method names you have to limit what the method is supposed to do. If you are trying to have the method do too many things, the name of your method will become so long that it becomes unreadable. Which means you should probably break your method into two separate methods. For instance, the method name I wrote above ‘getMyFirstnameAndMyLastNameBecauseIFeelLikeIt’. What is this method supposed to do? It’s supposed to get my first name and last name. We could improve the method name by shortening it to ‘getMyFirstAndLastName’. But still that is quite long. It works, but it’s a little too long. Plus it is clear that it does two things. We could therefore improve this even further by dividing it into two methods:
getMyFirstName()
getMyLastName()
This improves our code in three ways. The two that we just discussed, it increases readability and it decreases what each method is supposed to do. Each method is responsible for only getting one part of my name. The third improvement is that it gives us a way to just get my first name, or just my last name. This is great in case in the future we don’t want the entire name. Before we would have had to call the ‘getMyFirstAndLastName’ method and then remove my last name from what we get back which is just super messy and unnecessary. This is often referred to as increased re-usability which is one of the main benefits of OOP. The ideal is writing clean and reusable code. As clean coders we do not ever want to have to write something twice. Being strict about keeping the scope (what it is supposed to do) of your method as small as possible will ensure that the code you produce is as clean and easy to understand as possible.
Mehtod body:
This is everything between the curly braces. Here is where you can do things like creating new variables, do calculations, call other methods etc.
{
String newVariable = "Hello";
int calculation = 10 * 2;
callOtherMethod();
}
This is what would be referred to as the scope of our method. So everything done inside a this scope is only available to us within the scope of the method. This would not work:
{
String myName = "Kalle";
}
myName = "New Name";
The reason being that we created the variable myName inside the scope of our method so therefore we cannot access it outside the scope of our method. However inside the scope of our method we can access things that are outside the scope. As long as the things we are trying to access are not inside another scope. This would therefore work:
String myName = "Kalle";
{
myName = "New Name";
}
Return type and return statement:
String getMyName() {
return "Kalle Hallden";
}
The return statement is where the last part of every method. This is where we return whatever value we want to return. This can be anything as long as it matches the return type of our method.
The return type in our case is a String. Return type means that what this method gives us must be a variable of type String. If we call getMyName() in our code we know that what we will get back is a String. Calling a method means every time we use the method signature in our code. If we type methodName() we are calling that method. The correct way to call our method getMyName() would be like this:
String myName = getMyName();
The reason that this is the correct way to call this method is because the method returns a String. Since it returns a String we want to get access to that String. Therefore we want to store it in a variable. After calling our method in this way we will be able to access whatever we got from our getMyName() by simply using our variable myName. The variable myName is now equal to whatever our method returns:
String getMyName() {
return "Kalle Hallden";
}
String myName = getMyName();
// myName is now equal to "Kalle Hallden"
There are as many return types as there are variable types. Our method could return an int, double, String, bool etc. But, there is also another return type here. That’s called void. If a method has the return type void it means that the method does not return anything. Void methods are mostly used to set things. What that means is that if we have a variable called myName that hasn’t got a value yet. Then a void method can be used to give that variable a specific value. Like this:
String myName;
void setMyName() {
myName = "Kalle Hallden";
}
As you can see there is no return statement. In this case calling the method would look like this:
setMyname();
Super simple. Because the method doesn’t return a value that means we do not need to, and cannot, store what it returns. Because it returns nothing.
More on method signature:
Alright so now you hopefully have a good grasp on how to use methods. The last thing that I want to show you is the cool part about method signatures. As you may have noticed we always end our method names with parenthesis. This also holds some pretty powerful capabilities. Because what we can do is we can add what are called input parameters into our method signature. Which looks like this:
String getMoneyAfterTaxes(double salary) {
return salary * 0.6;
}
As you can see we have added two things to our method signature. Within the parenthesis you can declare input parameters. In this case we declared that this method ‘takes’ a variable of type double as input. And that can be accessed within the method by calling ‘salary’. This means that when we call this method we can also give the method some input. Which would look like this:
double moneyAfterTaxes = getMoneyAfterTaxes(555.5);
So now our method will do the calculation salary * 0.6. And salary has been set to be 555.5. Because that is what we gave as input. Inside the parenthesis of our method we declare variables. Then when we call our method we set the value of those variables by adding some value inside the parenthesis. Another example would be:
void printInputs(String inputOne, String inputTwo, String inputThree) {
print(inputOne + inputTwo + inputThree);
}
You can add as many input parameters as you want. But again the convention is “less is more”. You should always use as few as you can get away with. In order to maintain readability.
Conclusion:
That is it for lesson 2. Now I hope you have a pretty good grasp on methods in programming. Feel free to leave a comment if there’s something you didn’t understand and I will try to explain it more clearly. Next lesson will be on if else and try catch statements!
If you missed lesson one (what are variables in programming) then check it out here: Flutter Zero to One – Lesson #1 What are variables in programming?
October 4, 2019
I love this bro
October 5, 2019
is dope explained well
October 6, 2019
Kalle, thank you very much for your lessons, it helps me to adopt from python to flutter (dart) , plz keep doing what you do, it’s very useful for other people like me, with best regards from Russia
October 7, 2019
Hi there
Yeah it was a good startup course with flutter been pythonic for a while although failed to find very good gui libraries or frameworks in py moreso for app deployment
Anyways keep up the lessons will be get in usually to check out
Best wishes
Thanks alot
October 8, 2019
String getMoneyAfterTaxes(double salary) {
return salary * 0.6;
}
shouldn’t it be, since it returns double
double getMoneyAfterTaxes(double salary) {
return salary * 0.6;
}