Hi! These functions are both recursive, meaning they call themselves.
The first function, rec_count
, takes an integer as input and decrements it until it becomes 0. If we take 5 as an example input, the function first prints out 5 as it is, then calls itself again with 4 as an input (i.e. number - 1
). This function call now prints out 4 and calls itself again, this time with 3 as an input. This continues until it hits 0, where the base case becomes true, after which it returns into the previous function call. Every function call returns in a reverse fashion, with the latest call returning first, all the way up to the very first call with input 5.
The second function returns numbers in the Fibonacci series. The Fibonacci series is a sequence of numbers where each number is the sum of the two numbers before it (0, 1, 1, 2, 3, 5, 8…). The first two numbers are 0 and 1 respectively, and these form the base case for our function. If we take 3 as an input to our function (i.e. the third number in the Fibonacci series), the fib
function will make calls to fib(2)
and fib(1)
. Both these calls are handled in the base cases, returning 1 and 0 respectively. These two are then added and returned, giving 1 as the final answer.