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);
}
}
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
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 = ;
double c1 = fahrenheitToCelsius(f1);
double c2 = ;
double f2 = celsiusToFahrenheit(c2);
}
}
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 = ;
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 = ;
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 = ;
int[] result = fibonacci(n);
System.out.println("result=" + java.util.Arrays.toString(result));
}
}
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);
}
}
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);
}
}
Functions can call other functions. Build complex logic from simple pieces - this is called composition.