Foundations
Functions
Reusable Code
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.
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);
}
}
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]total ← 0
pass 1 of 23static int sum(int[] arr) {4 int total→ 0 = 0;5 for (int i = 0; i < arr.length; i++) {total ← 10
pass 1 of 84int 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 pass iarr.lengtharr[i]total1 0 3 10 0 → 10 2 1 3 20 10 → 30 3 2 3 30 30 → 60 4 0 5 1 0 → 1 5 1 5 2 1 → 3 6 2 5 3 3 → 6 7 3 5 4 6 → 10 8 4 5 5 10 → 15 return total;
7 }8 return total60;9}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]total ← 0
pass 2 of 23static int sum(int[] arr) {4 int total→ 0 = 0;5 for (int i = 0; i < arr.length; i++) {return total;
7 }8 return total15;9}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.
Temperature conversion
Convert between Fahrenheit and Celsius. Two functions, opposite directions.
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);
}
}
f1 ← 98.6
11public static void main(String[] args) {12 double f1→ 98.6 = 98.6; //@f1=32, 21213 double c1 = fahrenheitToCelsius(f198.6);static double fahrenheitToCelsius(double f)
3static double fahrenheitToCelsius(double f98.6) {4 return (f98.6 - 32) * 5 / 9;5}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}static double celsiusToFahrenheit(double c)
7static double celsiusToFahrenheit(double c0.0) {8 return c0.0 * 9 / 5 + 32;9}f2 ← 32.0
15 double c2 = 0; //@c2=100, 3716 double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
f1 ← 32.0
11public static void main(String[] args) {12 double f1→ 32.0 = 32;13 double c1 = fahrenheitToCelsius(f132.0);static double fahrenheitToCelsius(double f)
3static double fahrenheitToCelsius(double f32.0) {4 return (f32.0 - 32) * 5 / 9;5}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}static double celsiusToFahrenheit(double c)
7static double celsiusToFahrenheit(double c0.0) {8 return c0.0 * 9 / 5 + 32;9}f2 ← 32.0
15 double c2 = 0;16 double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
f1 ← 212.0
11public static void main(String[] args) {12 double f1→ 212.0 = 212;13 double c1 = fahrenheitToCelsius(f1212.0);static double fahrenheitToCelsius(double f)
3static double fahrenheitToCelsius(double f212.0) {4 return (f212.0 - 32) * 5 / 9;5}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}static double celsiusToFahrenheit(double c)
7static double celsiusToFahrenheit(double c0.0) {8 return c0.0 * 9 / 5 + 32;9}f2 ← 32.0
15 double c2 = 0;16 double f2→ 32.0 = celsiusToFahrenheit(c20.0);17}
f1 ← 98.6
11public static void main(String[] args) {12 double f1→ 98.6 = 98.6;13 double c1 = fahrenheitToCelsius(f198.6);static double fahrenheitToCelsius(double f)
3static double fahrenheitToCelsius(double f98.6) {4 return (f98.6 - 32) * 5 / 9;5}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}static double celsiusToFahrenheit(double c)
7static double celsiusToFahrenheit(double c37.0) {8 return c37.0 * 9 / 5 + 32;9}f2 ← 98.6
15 double c2 = 37;16 double f2→ 98.6 = celsiusToFahrenheit(c237.0);17}
f1 ← 98.6
11public static void main(String[] args) {12 double f1→ 98.6 = 98.6;13 double c1 = fahrenheitToCelsius(f198.6);static double fahrenheitToCelsius(double f)
3static double fahrenheitToCelsius(double f98.6) {4 return (f98.6 - 32) * 5 / 9;5}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}static double celsiusToFahrenheit(double c)
7static double celsiusToFahrenheit(double c100.0) {8 return c100.0 * 9 / 5 + 32;9}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.
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));
}
}
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));fib[1] ← 1
3static int[] fibonacci(int n10) {4 int[] fib = new int[n];5 fib[0]0 = 0;6 fib[1]→ 1 = 1;fib[i] ← 1
pass 1 of 88for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 3 4 5 3 2 0 → 5 5 6 5 3 0 → 8 6 7 8 5 0 → 13 7 8 13 8 0 → 21 8 9 21 13 0 → 34 return fib;
12 return fib;13}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]
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));fib[1] ← 1
3static int[] fibonacci(int n5) {4 int[] fib = new int[n];5 fib[0]0 = 0;6 fib[1]→ 1 = 1;fib[i] ← 1
pass 1 of 38for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 3 return fib;
12 return fib;13}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]
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));fib[1] ← 1
3static int[] fibonacci(int n15) {4 int[] fib = new int[n];5 fib[0]0 = 0;6 fib[1]→ 1 = 1;fib[i] ← 1
pass 1 of 138for (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 pass ifib[i - 1]fib[i - 2]fib[i]1 2 1 0 0 → 1 2 3 1 1 0 → 2 3 4 2 1 0 → 3 4 5 3 2 0 → 5 5 6 5 3 0 → 8 6 7 8 5 0 → 13 7 8 13 8 0 → 21 8 9 21 13 0 → 34 9 10 34 21 0 → 55 ⋯ 2 more passes ⋯ 12 13 144 89 0 → 233 13 14 233 144 0 → 377 return fib;
12 return fib;13}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.
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);
}
}
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]result ← 23
3static int max(int[] arr) {4 int result→ 23 = arr[0]23;5 for (int i = 1; i < arr.length; i++) {for (int i = 1; i < arr.length; i++)
pass 1 of 64int 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 pass i1 1 2 2 3 3 4 4 5 5 6 6 result ← 45
pass 1 of 35for (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 pass arr[i]iresult1 45 1 23 → 45 2 67 3 45 → 67 3 89 5 67 → 89 return result;
9 }10 return result89;11}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.
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);
}
}
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]total ← 0
pass 1 of 23static int sum(int[] arr) {4 int total→ 0 = 0;5 for (int i = 0; i < arr.length; i++) {total ← 85
pass 1 of 104int 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 pass iarr[i]total1 0 85 0 → 85 2 1 92 85 → 177 3 2 78 177 → 255 4 3 95 255 → 350 5 4 88 350 → 438 6 0 85 0 → 85 7 1 92 85 → 177 8 2 78 177 → 255 9 3 95 255 → 350 10 4 88 350 → 438 return total;
7 }8 return total438;9}total ← 438
18 System.out.println("scores=" + java.util.Arrays.toString(scores));19 int total→ 438 = sum(scores);20 double avg = average(scores);21}static double average(int[] arr)
11static double average(int[] arr) {12 return (double) sum(arr) / arr.length5;13}total ← 0
pass 2 of 23static int sum(int[] arr) {4 int total→ 0 = 0;5 for (int i = 0; i < arr.length; i++) {return total;
7 }8 return total438;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.