Consider the following:
public static void showFirstRecursion (int num) { System.out.println("Entering method. num = " + num); if (num > 1) { showSecondRecursion(num - 1); } System.out.println("Leaving method. num = " + num); } public static void showSecondRecursion (int num) { System.out.println("Entering method. num = " + num); if (num > 1) { showThirdRecursion(num - 1); } System.out.println("Leaving method. num = " + num); }
No problem, right? You expect to see the first method entered, the second one introduced (the third is not entered because num == 0), the second on the left, the first on the left.
There is nothing special about recursion. It simply calls a function call that occurs to call the function from which the call is part. A recursive call behaves conceptually in every way, like any other function call. The trick is the design of a recursive algorithm, i.e. The reason you want to call the same function that you are already in.
Karl Knechtel
source share