What is recursion and how is it used in programming?
Recursion represents the main idea of programming. The method defined as being opposed to iteration, is called recursion. Recursion happens when a function calls itself. Imperative programming languages avoid recursion method because it could slow the process, although there exists techniques that are designed to prevent this problem.
The main example that can be used to define recursion is factorial computing. The basic property of factorial function is that the factorial of a number can be evaluated as being the number times the factorial of the number below it. Since the factorial function could run without stopping and could return results that do not make any sense, it is important to impose a condition that makes the function to stop at 1.
The recursive program structure is built by means of the following schema, such that:
- solver initialization;
- verify the current value to see if it respects the imposed condition regarding the basic value;
- answer re-definition;
- running of sub-program algorithm;
- results combination in a single answer;
- result returning.