Topic Overview and Actualization

Welcome back to an exciting journey into the world of Java! Today's itinerary includes "Function Chaining," the practice of calling a function within another and handling multiple return values using Java's List class. Picture nested matryoshka dolls and a multi-handled briefcase, and you'll grasp the concepts!

Understanding Function Chaining

Let's demystify "Function Chaining". Have you ever prepared a cup of coffee? You sequentially boil water, brew coffee, and add cream. Now, imagine these steps as functions: doubleNumber() and addFive(). If we chain these functions together, we have our doubleAndAddFive() function — an apt illustration of function chaining!

Take a look:

public static int doubleNumber(int number) {
    return number * 2;  // doubles the input
}

public static int addFive(int number) {
    return number + 5;  // adds 5 to the input
}

public static int doubleAndAddFive(int number) {
    return addFive(doubleNumber(number));  // calls doubleNumber first, then addFive
}

In doubleAndAddFive(), doubleNumber() is called first, and then its result fuels addFive(). That's function chaining!

Hands-on with Function Chaining

Now, let's dip our toes into function chaining. Consider the task of finding the square root of the sum of two numbers. We call sum() inside sqrtOfSum(), feeding its result to sqrt().

public static void main(String[] args) {
    System.out.println(sqrtOfSum(25, 25));  // prints the square root of the sum
}

static int sum(int a, int b) {
    return a + b;  // returns the sum
}

static double sqrt(int number) {
    return Math.sqrt(number);  // returns the square root
}

static double sqrtOfSum(int a, int b) {
    return sqrt(sum(a, b)); // calls sum first, then sqrt
}
Dealing with Multiple Return Values

Consider this scenario: a board game where throwDice() simulates the throw of two dice and returns both results. But how do you return both values from the function? This is where Java's List class saves the day!

You can just return a List from the function, and it can handle any number of values in it. Let's see on example:

import java.util.*;

static List<Integer> throwDice() {
    Random rand = new Random();
    return Arrays.asList(rand.nextInt(6) + 1, rand.nextInt(6) + 1);  // returns a list of two element the dice throws
}

Here, we created an ArrayList of two elements and provided it as a return value - easy and simple! You can access returned elements using the ArrayList::get method after that.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal