Recursion is a major tool in the toolbox of the software engineer.
It can allow to describe a problem from a different perspective.
In the context of Functional Programming this is also a major trick to avoid using mutable variables.
This is a very basic example to be able to understand the concept of recursion fully. In the future, we will see more complex application of recursion.
To build a successful recursion, you need several things:
StackOverflowError
.loop
function is called inside of itself.loop
is called, it is called with modified arguments.acc
.acc
equals to 0
.The most famous example of recursion in books are always factorial
or Fibonacci
. I would love to see your implementation, feel free to submit a Scastie link in the comment section or on Discord.