In programming there might be a situation where a function needs to invoke itself. The C++ language supports recursive feature, that is a function calls itself repetitively. In recursion a function calls itself and the control goes to the same function and it executes repeatedly until some condition is satisfied. In this type of recursive calls a function starts with a new value every time.
Rules for Recursive Function
- In recursion, it is essential to call a function by itself; otherwise recursion would not take place.
- Only the user-defined function can be involved in the recursion. Library function cannot involve in recursion because their source code cannot be viewed.
- A recursive function can be invoked by itself or by other function. It saves return address with the intention to return at proper location when return to a calling statement is made.
- Recursion is turning out to be increasingly important in non-numeric applications and symbolic manipulations.
- To stop the recursive function, it is necessary to base the recursion on test condition, and proper terminating statement such as exit() or return() must be written using the if() statement.
7.24 Program to find the factorial of a entered number.
7.25 Program to calculate the gcd of two numbers.