top of page
recursion-with-depth.png

Recursion

Recursion makes the method call itself over and over again until reaching a bass case. 

It provides a way to break complicated problems down into simple problems which are easier to solve.

Adding up to 10

Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:

Capture.PNG

Bass Case

Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself.

 

Every recursive function should have a bass condition, which is the condition where the function stops calling itself. In the previous example, the bass condition is when the parameter k becomes 0.

​

In this example, the function adds a range of numbers between a start and an end. The bass condition for this recursive function is when end is not greater than start:

Capture.PNG
bottom of page