Methods

Introduction to Methods


Important Words

  • method
  • calling a method
  • modularity
  • maintain
  • scope
  • pass by value
  • pass by reference
  • method header
  • method body

Methods are a very useful tool in programming, and are simply blocks of code that perform a specific task. They allow us to write clear and concise programs that are easy to maintain, calling methods as they are needed as opposed to writing all the necessary code each time we want to use it. Methods are practically stand-alone blocks of code, allowing us to reuse methods we write for one program in another.


A method is composed of two parts, the method header and the method body. The generic template for a method looks like:




//program code



}  //closing brace for main method



Method header {



	//Method Body



}



}  //closing brace for class



Let's look at the method now, piece by piece. We'll start with the method header.


Method Header


The method header actually contains a lot of information about the method. It contains what modifiers the method has, the return value type (if any), the method’s name, and finally any parameters the method needs to work.


Consider the following sample method that will add two numbers together.




//program code



}  //closing brace for main method



   public int sumTwoNumbers(int number1, int numbe2) {



   	int result = number1 + number2;



	return result;



   }



}  //closing brace for class



Line 1 is the method header. This method only has one modifier, public. This tells us the visibility of the method, and in this case it is public, which has the broadest visibility. Other visibility modifiers you may see are protected or private.


Next, is the method return value type – which is int for this method. This tells the program that it will get something back from the method when it is finished, and that the information will be an integer. The return type can be anything: int, double, string, or even an object. If the method does not send anything back to the program, the return type is void.


After the return value type is the method name. We named this method sumTwoNumbers following Java naming conventions. We made the name of the method as descriptive as we could so we would know what the method does (we could have named it applePie if we wanted, but later would we have remembered that applePie added two numbers together?). The first letter is lowercase, and then the first letter of each additional word is uppercase. Easy enough, right?


Finally we have the number and type of parameters that the method requires in order to work, and these are located in the parentheses after the name. For this method, it requires two integers, int number1 and int number2, in order to work correctly. The method will only be able to add two integer numbers. If you wanted to add two double numbers, you would need to write another method and change int number1 and int number2 to double number1 and double number 2 (while keeping everything else the same). This process is called method overloading. If the method does not need any additional information from the program to work, nothing is put between the parentheses.


Method Body


The rest of the code that is not the method header is the method body. This contains all the code to all the method to perform its intended task. For this method, on line 3 we create an int variable named result, and assign it the value that results from number1 + number2. On line 5, we send the result back to the program.


Calling Methods


Now that we know what a method looks like in a program, how do we use it. There are two ways that you will see methods being “called”. Calling a method is how we tell a program we want to use a method. If the method is a stand alone method like the one we just talked about, we would use it in the following way:




int x = 2;

int y = 3;



int sum = sumTwoNumbers(2, 3);



System.out.prinln(sum);



This program creates two integer variables x and y, and then uses the sumTwoNumbers method to add them together, and the result is then stored in another variable sum. Later on, we print the variable sum to display the result of the method.


The other way you will see methods being called is if they are part of a class. They look almost the same, except that you will have a variable following by a period, then the method. So it might look something like classVariable.sum(1,2).

Stacks


What if you have multiple methods, or one method calls another one and so on? The computer uses what is called a “stack” to handle it. The stack works on a last in first out principle so that the most recent method called will be completed first. For example, say you call method 1, which then calls method 2, and method 2 calls method 3. Method 3 is at the top of the stack and will be completed first, then method 2, and finally method 1. //add some sort of diagram


Pass by Value and Pass by Reference

Methods that have information sent to them from the program get this information in one of two ways, pass by value and pass by reference. Pass by value is just like it sounds, the value of the variable is sent to the method and the original variable value will remain unchanged. Pass by reference sends the address of value of the variable to the method to be used. This may result in the value of the original variable being changed, but there are programming techniques you can put in place to prevent that from happening. //diagram of some sort


Scope


Methods are also a good way to introduce and provide an example of Scope. Scope refers to the areas of a program in which a variable can and cannot be accessed. Lets use the method and program we did before look at an example of scope.




int x = 2;

int y = 3;



int sum = sumTwoNumbers(2, 3);



System.out.prinln(sum);



   public int sumTwoNumbers(int number1, int numbe2) {



	int methodVariable = 10;



   	int result = number1 + number2;



	return result;



   } 



The variable methodVariable was declared in the method. It can only be used within the method and cannot be accessed from outside the method. This is an example of scope. A variable’s scope is the block it is declared and any blocks that may be contained within that block.