Your calculator app needs to convert temperatures in three different places. Instead of copying the formula three times, you create a function toCelsius(f) and call it wherever needed. Fix a bug once, it's fixed everywhere.

Sum of array function

Wrap the sum logic in a reusable function.

Sum.java
Replay: real traced execution (multi-file project)
public class Sum {

    static int sum(int[] arr) {
        int total = 0;
        for (int i = 0; i < arr.length; i++) {
            total = total + arr[i];
        }
        return total;
    }

    public static void main(String[] args) {
        int[] nums1 = {10, 20, 30};
        int[] nums2 = {1, 2, 3, 4, 5};

        System.out.println("nums1=" + java.util.Arrays.toString(nums1));
        int result1 = sum(nums1);

        System.out.println("nums2=" + java.util.Arrays.toString(nums2));
        int result2 = sum(nums2);
    }
}
  1. public static void main(String[] args)

    11public static void main(String[] args) {12    int[] nums1 = {10, 20, 30};13    int[] nums2 = {1, 2, 3, 4, 5};14    15    System.out.println("nums1=" + java.util.Arrays.toString(nums1));16    int result1 = sum(nums1);
    outputnums1=[10, 20, 30]
  2. total ← 0

    pass 1 of 2
    3static int sum(int[] arr) {4    int total→ 0 = 0;5    for (int i = 0; i < arr.length; i++) {
  3. total ← 10

    pass 1 of 8
    4int total = 0;5for (int i0 = 0; i < arr.length3; i++) {6    total→ 10 = total + arr[i]10;7}
    All 8 passes — pass 1 is the card above
    passiarr.lengtharr[i]total
    103100 10
    2132010 30
    3233030 60
    40510 1
    51521 3
    62533 6
    73546 10
    845510 15
  4. return total;

    7    }8    return total60;9}
  5. result1 ← 60

    15    System.out.println("nums1=" + java.util.Arrays.toString(nums1));16    int result1→ 60 = sum(nums1);17    18    System.out.println("nums2=" + java.util.Arrays.toString(nums2));19    int result2 = sum(nums2);20}
    outputnums2=[1, 2, 3, 4, 5]
  6. total ← 0

    pass 2 of 2
    3static int sum(int[] arr) {4    int total→ 0 = 0;5    for (int i = 0; i < arr.length; i++) {
  7. return total;

    7    }8    return total15;9}
  8. result2 ← 15

    18    System.out.println("nums2=" + java.util.Arrays.toString(nums2));19    int result2→ 15 = sum(nums2);20}

The function takes an array, returns the sum.

method In Java, functions inside classes are called methods.
parameter Input values passed to a function when calling it.
return The value a function sends back to the caller.

Temperature conversion

Convert between Fahrenheit and Celsius. Two functions, opposite directions.

example
Temperature.java
Replay: real traced execution (multi-file project)
public class Temperature {

    static double fahrenheitToCelsius(double f) {
        return (f - 32) * 5 / 9;
    }

    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double f1 = 98.6;
        double c1 = fahrenheitToCelsius(f1);

        double c2 = 0;
        double f2 = celsiusToFahrenheit(c2);
    }
}
public class Temperature {

    static double fahrenheitToCelsius(double f) {
        return (f - 32) * 5 / 9;
    }

    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double f1 = 32;
        double c1 = fahrenheitToCelsius(f1);

        double c2 = 0;
        double f2 = celsiusToFahrenheit(c2);
    }
}
public class Temperature {

    static double fahrenheitToCelsius(double f) {
        return (f - 32) * 5 / 9;
    }

    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double f1 = 212;
        double c1 = fahrenheitToCelsius(f1);

        double c2 = 0;
        double f2 = celsiusToFahrenheit(c2);
    }
}
public class Temperature {

    static double fahrenheitToCelsius(double f) {
        return (f - 32) * 5 / 9;
    }

    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double f1 = 98.6;
        double c1 = fahrenheitToCelsius(f1);

        double c2 = 37;
        double f2 = celsiusToFahrenheit(c2);
    }
}
public class Temperature {

    static double fahrenheitToCelsius(double f) {
        return (f - 32) * 5 / 9;
    }

    static double celsiusToFahrenheit(double c) {
        return c * 9 / 5 + 32;
    }

    public static void main(String[] args) {
        double f1 = 98.6;
        double c1 = fahrenheitToCelsius(f1);

        double c2 = 100;
        double f2 = celsiusToFahrenheit(c2);
    }
}
  1. f1 ← 98.6

    11public static void main(String[] args) {12    double f1→ 98.6 = 98.6;  //@f1=32, 21213    double c1 = fahrenheitToCelsius(f198.6);
  2. static double fahrenheitToCelsius(double f)

    3static double fahrenheitToCelsius(double f98.6) {4    return (f98.6 - 32) * 5 / 9;5}
  3. c1 ← 37.0, c2 ← 0.0

    12    double f1 = 98.6;  //@f1=32, 21213    double c1→ 37.0 = fahrenheitToCelsius(f198.6);14    15    double c2→ 0.0 = 0;  //@c2=100, 3716    double f2 = celsiusToFahrenheit(c20.0);17}
  4. static double celsiusToFahrenheit(double c)

    7static double celsiusToFahrenheit(double c0.0) {8    return c0.0 * 9 / 5 + 32;9}
  5. f2 ← 32.0

    15    double c2 = 0;  //@c2=100, 3716    double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
  1. f1 ← 32.0

    11public static void main(String[] args) {12    double f1→ 32.0 = 32;13    double c1 = fahrenheitToCelsius(f132.0);
  2. static double fahrenheitToCelsius(double f)

    3static double fahrenheitToCelsius(double f32.0) {4    return (f32.0 - 32) * 5 / 9;5}
  3. c1 ← 0.0, c2 ← 0.0

    12    double f1 = 32;13    double c1→ 0.0 = fahrenheitToCelsius(f132.0);14    15    double c2→ 0.0 = 0;16    double f2 = celsiusToFahrenheit(c20.0);17}
  4. static double celsiusToFahrenheit(double c)

    7static double celsiusToFahrenheit(double c0.0) {8    return c0.0 * 9 / 5 + 32;9}
  5. f2 ← 32.0

    15    double c2 = 0;16    double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
  1. f1 ← 212.0

    11public static void main(String[] args) {12    double f1→ 212.0 = 212;13    double c1 = fahrenheitToCelsius(f1212.0);
  2. static double fahrenheitToCelsius(double f)

    3static double fahrenheitToCelsius(double f212.0) {4    return (f212.0 - 32) * 5 / 9;5}
  3. c1 ← 100.0, c2 ← 0.0

    12    double f1 = 212;13    double c1→ 100.0 = fahrenheitToCelsius(f1212.0);14    15    double c2→ 0.0 = 0;16    double f2 = celsiusToFahrenheit(c20.0);17}
  4. static double celsiusToFahrenheit(double c)

    7static double celsiusToFahrenheit(double c0.0) {8    return c0.0 * 9 / 5 + 32;9}
  5. f2 ← 32.0

    15    double c2 = 0;16    double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
  1. f1 ← 98.6

    11public static void main(String[] args) {12    double f1→ 98.6 = 98.6;13    double c1 = fahrenheitToCelsius(f198.6);
  2. static double fahrenheitToCelsius(double f)

    3static double fahrenheitToCelsius(double f98.6) {4    return (f98.6 - 32) * 5 / 9;5}
  3. c1 ← 37.0, c2 ← 37.0

    12    double f1 = 98.6;13    double c1→ 37.0 = fahrenheitToCelsius(f198.6);14    15    double c2→ 37.0 = 37;16    double f2 = celsiusToFahrenheit(c237.0);17}
  4. static double celsiusToFahrenheit(double c)

    7static double celsiusToFahrenheit(double c37.0) {8    return c37.0 * 9 / 5 + 32;9}
  5. f2 ← 98.6

    15    double c2 = 37;16    double f2→ 98.6 = celsiusToFahrenheit(c237.0);17}
  1. f1 ← 98.6

    11public static void main(String[] args) {12    double f1→ 98.6 = 98.6;13    double c1 = fahrenheitToCelsius(f198.6);
  2. static double fahrenheitToCelsius(double f)

    3static double fahrenheitToCelsius(double f98.6) {4    return (f98.6 - 32) * 5 / 9;5}
  3. c1 ← 37.0, c2 ← 100.0

    12    double f1 = 98.6;13    double c1→ 37.0 = fahrenheitToCelsius(f198.6);14    15    double c2→ 100.0 = 100;16    double f2 = celsiusToFahrenheit(c2100.0);17}
  4. static double celsiusToFahrenheit(double c)

    7static double celsiusToFahrenheit(double c100.0) {8    return c100.0 * 9 / 5 + 32;9}
  5. f2 ← 212.0

    15    double c2 = 100;16    double f2→ 212.0 = celsiusToFahrenheit(c2100.0);17}

Functions can take one value and return another. Each function does one specific job.

Fibonacci function

Get the nth Fibonacci number.

n
Fibonacci.java
Replay: real traced execution (multi-file project)
public class Fibonacci {

    static int[] fibonacci(int n) {
        int[] fib = new int[n];
        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        return fib;
    }

    public static void main(String[] args) {
        int n = 10;
        int[] result = fibonacci(n);
        System.out.println("result=" + java.util.Arrays.toString(result));
    }
}
public class Fibonacci {

    static int[] fibonacci(int n) {
        int[] fib = new int[n];
        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        return fib;
    }

    public static void main(String[] args) {
        int n = 5;
        int[] result = fibonacci(n);
        System.out.println("result=" + java.util.Arrays.toString(result));
    }
}
public class Fibonacci {

    static int[] fibonacci(int n) {
        int[] fib = new int[n];
        fib[0] = 0;
        fib[1] = 1;

        for (int i = 2; i < n; i++) {
            fib[i] = fib[i - 1] + fib[i - 2];
        }

        return fib;
    }

    public static void main(String[] args) {
        int n = 15;
        int[] result = fibonacci(n);
        System.out.println("result=" + java.util.Arrays.toString(result));
    }
}
  1. n ← 10

    15public static void main(String[] args) {16    int n→ 10 = 10;  //@n=5, 1517    int[] result = fibonacci(n10);18    System.out.println("result=" + java.util.Arrays.toString(result));
  2. fib[1] ← 1

    3static int[] fibonacci(int n10) {4    int[] fib = new int[n];5    fib[0]0 = 0;6    fib[1]→ 1 = 1;
  3. fib[i] ← 1

    pass 1 of 8
    8for (int i2 = 2; i < n10; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    All 8 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
    45320 5
    56530 8
    67850 13
    781380 21
    8921130 34
  4. return fib;

    12    return fib;13}
  5. int[] result = fibonacci(n);

    16    int n = 10;  //@n=5, 1517    int[] result = fibonacci(n10);18    System.out.println("result=" + java.util.Arrays.toString(result));19}
    outputresult=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  1. n ← 5

    15public static void main(String[] args) {16    int n→ 5 = 5;17    int[] result = fibonacci(n5);18    System.out.println("result=" + java.util.Arrays.toString(result));
  2. fib[1] ← 1

    3static int[] fibonacci(int n5) {4    int[] fib = new int[n];5    fib[0]0 = 0;6    fib[1]→ 1 = 1;
  3. fib[i] ← 1

    pass 1 of 3
    8for (int i2 = 2; i < n5; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    All 3 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
  4. return fib;

    12    return fib;13}
  5. int[] result = fibonacci(n);

    16    int n = 5;17    int[] result = fibonacci(n5);18    System.out.println("result=" + java.util.Arrays.toString(result));19}
    outputresult=[0, 1, 1, 2, 3]
  1. n ← 15

    15public static void main(String[] args) {16    int n→ 15 = 15;17    int[] result = fibonacci(n15);18    System.out.println("result=" + java.util.Arrays.toString(result));
  2. fib[1] ← 1

    3static int[] fibonacci(int n15) {4    int[] fib = new int[n];5    fib[0]0 = 0;6    fib[1]→ 1 = 1;
  3. fib[i] ← 1

    pass 1 of 13
    8for (int i2 = 2; i < n15; i++) {9    fib[i]→ 1 = fib[i - 1]1 + fib[i - 2]0;10}
    13 passes — pass 1 is the card above
    passifib[i - 1]fib[i - 2]fib[i]
    12100 1
    23110 2
    34210 3
    45320 5
    56530 8
    67850 13
    781380 21
    8921130 34
    91034210 55
    ⋯ 2 more passes ⋯
    1213144890 233
    13142331440 377
  4. return fib;

    12    return fib;13}
  5. int[] result = fibonacci(n);

    16    int n = 15;17    int[] result = fibonacci(n15);18    System.out.println("result=" + java.util.Arrays.toString(result));19}
    outputresult=[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]

The function handles all the array setup and calculation internally. The caller just says "give me the 10th Fibonacci number."

Find maximum function

Wrap the "find max" logic in a reusable function.

Max.java
Replay: real traced execution (multi-file project)
public class Max {

    static int max(int[] arr) {
        int result = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > result) {
                result = arr[i];
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {23, 45, 12, 67, 34, 89, 41};

        System.out.println("nums=" + java.util.Arrays.toString(nums));
        int m = max(nums);
    }
}
  1. public static void main(String[] args)

    13public static void main(String[] args) {14    int[] nums = {23, 45, 12, 67, 34, 89, 41};15    16    System.out.println("nums=" + java.util.Arrays.toString(nums));17    int m = max(nums);18}
    outputnums=[23, 45, 12, 67, 34, 89, 41]
  2. result ← 23

    3static int max(int[] arr) {4    int result→ 23 = arr[0]23;5    for (int i = 1; i < arr.length; i++) {
  3. for (int i = 1; i < arr.length; i++)

    pass 1 of 6
    4int result = arr[0];5for (int i1 = 1; i < arr.length7; i++) {6    if (arr[i] > result) {
    All 6 passes — pass 1 is the card above
    passi
    11
    22
    33
    44
    55
    66
  4. result ← 45

    pass 1 of 3
    5for (int i = 1; i < arr.length; i++) {6    if (arr[i]45 > result23) {7        result→ 45 = arr[i]45;8    }
    All 3 passes — pass 1 is the card above
    passarr[i]iresult
    145123 45
    267345 67
    389567 89
  5. return result;

    9    }10    return result89;11}
  6. m ← 89

    16    System.out.println("nums=" + java.util.Arrays.toString(nums));17    int m→ 89 = max(nums);18}

Now any code can find the maximum of an array with a single call.

Multiple function calls

Use functions together to solve a problem.

Combined.java
Replay: real traced execution (multi-file project)
public class Combined {

    static int sum(int[] arr) {
        int total = 0;
        for (int i = 0; i < arr.length; i++) {
            total = total + arr[i];
        }
        return total;
    }

    static double average(int[] arr) {
        return (double) sum(arr) / arr.length;
    }

    public static void main(String[] args) {
        int[] scores = {85, 92, 78, 95, 88};

        System.out.println("scores=" + java.util.Arrays.toString(scores));
        int total = sum(scores);
        double avg = average(scores);
    }
}
  1. public static void main(String[] args)

    15public static void main(String[] args) {16    int[] scores = {85, 92, 78, 95, 88};17    18    System.out.println("scores=" + java.util.Arrays.toString(scores));19    int total = sum(scores);20    double avg = average(scores);
    outputscores=[85, 92, 78, 95, 88]
  2. total ← 0

    pass 1 of 2
    3static int sum(int[] arr) {4    int total→ 0 = 0;5    for (int i = 0; i < arr.length; i++) {
  3. total ← 85

    pass 1 of 10
    4int total = 0;5for (int i0 = 0; i < arr.length5; i++) {6    total→ 85 = total + arr[i]85;7}
    All 10 passes — pass 1 is the card above
    passiarr[i]total
    10850 85
    219285 177
    3278177 255
    4395255 350
    5488350 438
    60850 85
    719285 177
    8278177 255
    9395255 350
    10488350 438
  4. return total;

    7    }8    return total438;9}
  5. total ← 438

    18    System.out.println("scores=" + java.util.Arrays.toString(scores));19    int total→ 438 = sum(scores);20    double avg = average(scores);21}
  6. static double average(int[] arr)

    11static double average(int[] arr) {12    return (double) sum(arr) / arr.length5;13}
  7. total ← 0

    pass 2 of 2
    3static int sum(int[] arr) {4    int total→ 0 = 0;5    for (int i = 0; i < arr.length; i++) {
  8. return total;

    7    }8    return total438;9}
  9. avg ← 87.6

    19    int total = sum(scores);20    double avg→ 87.6 = average(scores);21}

Functions can call other functions. Build complex logic from simple pieces - this is called composition.