The elements of an inductively defined domain can be easily manipulated through recursion.
A method is said to be recursive if it contains an activation of itself (either directly, or indirectly through the activation of other methods).
Let us see some examples of mathematical functions on natural numbers that are defined inductively, exploiting the fact that these functions operate on elements of an inductively defined domain.
Example: Factorial:
The recursive definition of a function reflects the structure of the inductive definition of the domain on which the function operates; hence we have:
Starting from the recursive definition of a function, we can usually provide rather easily an implementation by means of a recursive method.
Example: Implementation of the factorial by means of a recursive method:
public static long factorial(long n) { if (n == 0) return 1; else return n * factorial(n-1); }