Methods: Subroutines in Java

5.6 Methods: Functions and Subroutines in Java

Up 'till now, I have been calling units of Java code "subroutines". Actually, they are called methods - this is object-oriented programming jargon, but you might as well get used to it since you will be seeing terms like this in the future.

The first method that appears in the Newton class is called f. This is the function that we are finding roots of. Each method starts with a declaration that describes the method. Then, between a pair of curly braces are the lines that execute when the method is called. Somewhere in the method there should be a line that return's a value from the method. Here is the method from our example code:

    static double f(double x) {
	return Math.sin(x);
    }
This is a very simple Java method - we'll go through it step by step.
static
A Java program can have more than one copy of a class created at any one time. The static keyword indicates that this method doesn't depend on anything particular to a given copy of the class. For now, this can be thought of as a form of optimization. No matter how many copies of this class exist, only one copy of this method will, so this saves space. You should get in the habit of declaring all methods as static unless you know of a reason why it shouldn't be. (The compiler will normally tell you if you can't call a method static.)
double
Java is a strongly typed language. This means that every variable must have a type, and every method must have a return type. In this case, the return type is double, which is a double precision floating point number.
f
This is the name of the method.
double x
After the method name comes the list of arguments in parentheses. Our method takes a single argument, called x. Like all other variables, it has a type - in this case it is also a double. If our method took more than one argument, they would be listed here separated by commas, as in f(double x, double y)
return Math.sin(x)
The return keyword signals an exit point for the method. If the program reaches this point of the code, it calculates the return value (in this case Math.sin(x)), exits the method, and gives the return value to the calling method. Notice that since our method f is of type double, the return value must also be of type . In this case, the return value of the mathematics method Math.sin happens to be double as well, so we are safe.

So, the method f isn't all that interesting. It just returns the value of sin(x). The idea here is that you will be able to change this method to some other more interesting function of you choosing.

Remember that the formula for Newton's method requires the derivative of the function that we are finding roots for. The next method in our Newton's method code gives us exactly that:

    static double fprime(double x) {
        return Math.cos(x);
    }
This method works just like f, except that it returns the value of cos(x), which is, of course, the derivative of sin(x). If you change f, you should also change fprime to reflect this change.

Next 5.7 The Main Method


David Maxwell, who is still writing this, would like to hear your comments and suggestions.