Recursion is a technique where a function calls itself. It is usually used to replace repetitive structures in problems of a recursive nature, creating short and elegant algorithms.
# Calling itself
A recursive function contains a call to itself within its body. It is vital to establish a stop condition (base case) to avoid infinite executions.
# Exercise: The Factorial
The factorial of a number N is defined as N * Factorial(N-1). The base case is Factorial(0) = 1.
Graphical process (3!)
# Exercise: Fibonacci
Sequence: 1, 1, 2, 3, 5, 8, 13... The function is based on adding the two previous terms.
Summary
Recursion allows for elegant and short algorithms. Although it consumes more memory due to stack management, it is fundamental for navigating structures like trees and graphs.
In the next article, we will see flowcharts, a tool to visualize algorithms.