Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised September 1, 2025)

Previous: 1. Functions and Testing

Functions

//
// Functions.java
//


public class Functions
{
    public static void hello()
    {
        System.out.println("Hello, world!");
    }

    public static double sum(double a, double b)
    {
        return a+b;
    }

    public static String twice(String s)
    {
        return s+s;
    }

    public static boolean isOdd(int n)
    {
        if (n%2 == 1)
        {
            // another line
            return true;
        }
        else
            return false;
    }

    public static void main(String[] args)
    {
        hello(); 

        double a = 5.0;
        double b = 7.0;
        System.out.println("a+b: " + sum(a, b));

        double c = sum(6.23, 7.3412345);
        System.out.println("a+b: " + c);

        String result = twice("Hello, APCS! ");
        System.out.println(result);

        System.out.println("isOdd(5): " + isOdd(5));
        System.out.println("isOdd(7): " + isOdd(7));
        System.out.println("isOdd(8): " + isOdd(8));

    }
}

Output:

Hello, world!
a+b: 12.0
a+b: 13.5712345
Hello, APCS! Hello, APCS! 
isOdd(5): true
isOdd(7): true
isOdd(8): false

Next: