Method Return Types

The method itself has a type:

public static double v (double t1, double    t2, double d1, double d2)
   { //The function's actual code
     return ((t2 - t1) / (d2 - d1));
   } 

This method returns a float value. Recall that main saved the value returned by v. The variable that it saved v’s value into HAD to also be a float. 

Methods can return values of all types: float, double, long double, short, int, long and char. In addition there is one other return type: void. Void cannot be used for variables because it means ‘nothing’. If the return type is ‘void’ it means that your method does not return a value. Here is an example of a void method that prints 10 stars on the screen:

public static void tenstar    ()
   { System.out.println(“*****”); 
     System.out.println(“*****”);
   } 

Note that a void method does not have any return statement, because it does not return anything. Another thing to note in this example is that it doesn’t need any information from the main program to run. No variables are passed to it. Methods of void type CAN have variables passed to them, but this one does not. To call a method without variables, the main program merely needs to say: 

tenstar(); 

Because the method return type is void, main does not need to store its result in a variable.