Java: Functions

We have been using a lot of various functions that exist within Java already but haven’t created any of our own, other than using main().

Creating a function is simply assigning a name to a block of code that can then be run as a whole whenever you need that task performed. Functions can receive additional information to help with their task, and can return a result containing the information they generate. Writing functions lets us create our own commands that we can then use else where in our programs.

There are several elements to declaring a function in Java, so let’s work through a simple example to illustrate what is going on. We’ll begin with a simple function to calculate the area of a circle which we know from mathematics as being A = π r^2. This function may look like:

public static double areaOfCircle( double radius ) {
    return Math.PI * radius * radius;
}

Exercise 901: First function

Here is the full code for the example, including calling it from main().

package ch.isl.basics;
import java.lang.Math;

public class Exercise901 {
    public static double areaOfCircle( double radius ) {
        return Math.PI * radius * radius;
    }

    public static void main(String[] args) {
        java.util.Scanner reader = new java.util.Scanner(System.in);
        double r, answer;
        System.out.println("Area of circle calculator");
        System.out.print("Radius: ");
        r = reader.nextDouble();
        answer = areaOfCircle(r);       // Execute our function!
        System.out.println( "The area of this circle is: "+answer );
    }
}

Exercise 902: Second function

This example illustrates two ideas:

package ch.isl.basics;
import java.lang.Math;
import ch.isl.basics.Exercise901;

public class Exercise902 {

    public static double areaOfCylinder(double radius, double length) {
        return length * Exercise901.areaOfCircle(radius);
    }

    public static void main(String[] args) {
        java.util.Scanner reader = new java.util.Scanner(System.in);
        double rad, len, answer;
        System.out.println("Surface area of cylinder calculator");
        System.out.print("Radius: ");
        rad = reader.nextDouble();
        System.out.print("Length: ");
        len = reader.nextDouble();
        answer = areaOfCylinder(rad,len);
        System.out.println( "The surface area of this cylinder is: "+answer );
    }
}

The power of code re-use should hopefully underscore why it will become important to organise your class files into a well structured package naming scheme. In reality you are not going to want to be importing ch.isl.basics.Exercise901 to access a function that calculates the area of a circle, but something like ch.isl.helpers.Geometry instead.

Error compiling??

Getting error: cannot find symbol? You might need to recheck your command line reference for the javac command. You probably need to specify a classpath for javac to search in order to find the files you want to import. Your solution likely looks like:

$ javac -d bin -cp bin src/ch/isl/basics/Exercise902.java