Recursive Function

A recursive function is a function that calls itself repeatedly. So, within the body of the declared function, we call that function itself. Recursion is essentially a form of looping within a program. However, recursive looping is quite different from typical loops like while and for. While their purpose is the same—performing iteration or looping—the difference lies in how they operate. If for and while are loops that use a condition or Boolean (true/false), recursion occurs within a function or method that calls itself. Therefore, we can describe recursion as a form of looping that invokes itself to perform an iteration.

Recursive functions can solve various problems, such as calculating Fibonacci numbers and factorials.

1
2
3
4
5
faktorial(5) = 5 * faktorial(4)
faktorial(4) = 4 * faktorial(3)
faktorial(3) = 3 * faktorial(2)
faktorial(2) = 2 * faktorial(1)
faktorial(1) = 1

Thus, factorial(5) = 5 4 3 2 1, which results in 120.

MODULE

Download Module 8