Introduction to programming, Lesson 4


Let's talk about methods and functions now. A method is a piece of code that does a specific task. Normally, the task is repetitive, taking input and producing output that is useful. Here's how a method is defined:

  public static void myMethod()
  {
     System.out.println("Here is my method, yo.");
  }

The familiar 'public static void main(String [] args)' is an example of a method. It has the same basic structure as myMethod() does. Let's define a class that includes myMethod().

  public class MyClass
  {
    public static void main(String [] args)
    {
       System.out.println("I'm going to call my method");
       myMethod();    
    }	
    
    public static void myMethod()
    {
       System.out.println("I called it.");
    }  
  }
  

A method is like a 'jump point' for the program execution. In the program above, the computer will start at main() and perform the System.out.println statement. Then it sees that it needs to jump to myMethod(). It will do so and find the second println statement. Note that myMethod() could have been written anywhere before or after main() - the order is not important, so long as it is within the MyClass curly braces {}:

  public class MyClass
  {
    public static void myMethod()
    {
       // this is legal, although main is always executed first
       System.out.println("I called it.");
    }  
    
    public static void main(String [] args)
    {
       System.out.println("I'm going to call my method");
       myMethod();    
    }
  }
  

Here's a slightly more complex example:

  private static int addition(int number1, int number2)
  {
      int result = number1 + number2;
      
      return result;	  
  }
  

You'll notice here that the 'void' has been replaced with 'int', and there are two more integers, number1 and number2. These integers are called parameters, and are the input. The variable 'result' is called the 'return value', and is the output. Note that the return value must always be the same type as what is written in the header. (The header is the 'private int addition' portion.)

So what's the difference between a function and a method? Well, functions are methods that return a value. You can tell the difference by looking at the header. A method always returns a 'void', that is, no value. A function returns anything besides void. So, the main() method returns no value, but addition() is a function because it is returning an integer value. Both a method and a function can have parameters, but parameters are not necessary for either. Here's a concrete example:

  public class Example1
  {
      public static void main(String [] args) // a method
      {
         int myAnswer = 0;
         
         myAnswer = computeSum(45, 5);
         
         System.out.println("The sum is " + myAnswer);   
      }	  
      
      public static int computeSum(int value1, int value2) // a function
      {
         return value1 + value2;   
      }
  }
  

Although you are free to name your methods and functions according to your whims, you can tell by the name of the function computeSum() that it probably is an addition function. computeSum() has two parameters (input values), and returns (outputs) an integer value. Inside of the main() method, we are assigning the 'myAnswer' variable to the output of the computeSum method.

Big deal, right? We could have just written 'myAnswer = 45 + 5;' and received the same result. True, but this is just an example. These functions can get really complex. You'll find that its easier to break your program into managable parts when you use methods and functions.

It is very important that the data types of the parameters passed to the function match what is declared in the function header. For instance, the following is NOT legal:

 1 public class Example1
 2 {
 3     public static void main(String [] args) // a method
 4     {
 5        int myAnswer = 0;
 6        
 7        myAnswer = computeSum(45.7, 5);
 8        
 9        System.out.println("The sum is " + myAnswer);   
 10    }	  
 11     
 12    public static float computeSum(int value1, float value2) // a function
 13    {
 14       return value1 + value2;   
 15    }
 16 }

This is another example of sloppy programming. In fact, this program won't even compile correctly. First of all, at line 7 we are passing a float value (45.7) and an integer value (5), to computeSum(). While this inherently is OK, take a look at computeSum()'s header. Whoops! It is expecting to receive an int following by a float. The sloppy programmer has mixed up the sequence of the parameters. Additionally, we can see that on line 7, myAnswer is being assigned to the result of computeSum(). The problem is that myAnswer is an integer, but computeSum is returning a float data type. Horrible ERROR!


Ready for some homework?

Describe the problems with the following two programs:

  public class Example1
  {
      public static void main(String [] args) // a method
      {
         int myAnswer = 0;
         
         myAnswer = computeSum(45, 5);
         
         System.out.println("The sum is " + myAnswer);   
      }	  
      
      public static void computeSum(int value1, int value2) // a function
      {
         return value1 + value2;   
      }
  }
  

  public class Example1
  {
      public static void main(String [] args) // a method
      {
         int myAnswer = 0;
         
         myAnswer = computeSum();
         
         System.out.println("The sum is " + myAnswer);   
      }	  
      
      public static int computeSum(int value1, int value2) // a function
      {
         return value1 + value2;   
      }
  }
  

What is the result of this program?

  public class Example1
  {
      public static void main(String [] args) // a method
      {
         int myAnswer = 0;
         
         myAnswer = subtract(45, 5);
         
         System.out.println("The answer is " + myAnswer);   
      }	  
      
      public static void subtract(int value1, int value2) // a function
      {
         return value1 - value2;   
      }
  }
  

What about this one?

  public class Example1
  {
      public static void main(String [] args) // a method
      {
         int myAnswer = 0;
         
         myAnswer = subtract(45, 5);
         
         System.out.println("The answer is " + myAnswer);   
      }	  
      
      public static void subtract(int value2, int value1) // a function
      {
         return value1 - value2;   
      }
  }
  
Hosted by www.Geocities.ws

1