GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||
GeezerVison is showing www.tutorialspoint.com — C++ Recursion (Recursive Function)
Selected Reading
C++ Recursion (Recursive Function)Recursion is a programming technique where a function calls itself over again and again with modified arguments until it reaches its base case, where the recursion stops. It breaks a problem down into smaller, more manageable sub-problems, recursion allows for elegant and better solutions to complex problems. Recursive FunctionA recursive function is a function which is particularly used for recursion where a function calls itself, either directly or indirectly, to address a problem. It must include at least one base case to terminate the recursion and one recursive case where the function invokes itself.
Creating a Recursive FunctionThe following syntax is used to implement a recursive function in C++ − function name(param_1, param_2..){
<base condition>
<function body>
<return statement>
}
Here,
Calling a Recursive FunctionCalling a recursive function is just like calling any other function, where you will use the function's name and provide the necessary parameters in int main() body. To call a recursive function, use the following syntax − func_name(value); Example of RecursionBelow is an example of a recursion function in C++. Here, we are calculating the factorial of a number using the recursion − #include <iostream>
using namespace std;
// Recursive Function to Calculate Factorial
int factorial(int num) {
// Base case
if (num <= 1) {
return 1;
}
// Recursive case
else {
return num * factorial(num - 1);
}
}
int main() {
int positive_number;
cout << "Enter a positive integer: ";
cin >> positive_number;
if (positive_number < 0) {
cout << "Wrong Input, Factorial is not Defined for Negative Integer" << endl;
} else {
cout << "Factorial of " << positive_number << " is " << factorial(positive_number) << endl;
}
return 0;
}
OutputEnter a positive integer: 4 (input) Factorial of 4 is 24 ExplanationIf take input int positive_number as 4, It will send integer to function name 'factorial' as factorial(4) Initial Call: factorial(4)This function will check base case (n<=1), as its not satisfying the base case, therefore move forward to recursive case and will compute as "4 * factorial(3)". Second call: factorial(3)This function will again check base case, as its not satisfying it, therefor will again move forward to recursive case , and compute as "3 * factorial(2)". Third Call: factorial(2)Checks base case and computes "2 * factorial(1)" Fourth call: factorial(1)Checks base case, now since function satisfying this base case condition thats less than or equal to 1, So it will return 1. Unwinding the StackNow, the recursive calls will start returning: After the 4th call now it will again start from back, returning to the Third Call first. Return to Third Call: factorial(2)We already have factorial(1) = 1, therefor factorial(2) will return, "2 * factorial(1)", thats "2 * 1" , which returns as factorial(2) equals to 2. Return to Second Call: factorial(3)Now, factorial(2) is 2, therefore factorial(3) equals to "3 * 2", thats 6. Return to Initial Call: factorial(4)We have factoria(3) which returns 6, therefore, factorial(4) returns "4 * 6 = 24". Types of RecursionRecursion can be categorized into two main types where each with its own sub-categories − 1. Direct RecursionDirect recursion occurs when a function calls itself directly − Simple Direct RecursionThe function calls itself with a simpler or smaller instance of the problem. It is used for solving problems like factorial calculation, fibonacci sequence generation, etc. Tail RecursionA form of direct recursion where the recursive call is the last operation in the function. It is used for solving accumulative calculations and list processing problems. int factorial(int n, int result = 1) {
if (n <= 1) {
return result;
} else {
return factorial(n - 1, n * result); // Tail recursive call
}
}
Head RecursionThe recursive call is made before any other operation in the function. Processing occurs after the recursive call returns. It is used for tree traversals and output generation. void printNumbers(int n) {
if (n > 0) {
printNumbers(n - 1); // Recursive call first
cout << n << " "; // Processing after recursive call
}
}
Linear RecursionEach function call generates exactly one recursive call, forming a linear chain of calls. It is used for simple counting or summing. int linearRecursion(int n) {
if (n <= 0) {
return 0;
} else {
return linearRecursion(n - 1) + 1; // Linear recursive call
}
}
2. Indirect RecursionIndirect recursion occurs when a function calls another function, which eventually leads to the original function being called. This involves two or more functions calling each other. Mutual RecursionIn mutual recursion, two or more functions call each other in a recursive manner, forming a cyclic dependency. It is used for even and odd number classification and grammar parsing. #include <iostream>
using namespace std;
void even(int n);
void odd(int n);
void even(int n) {
if (n == 0) {
cout << "Even" << endl;
} else {
odd(n - 1); // Calls odd
}
}
void odd(int n) {
if (n == 0) {
cout << "Odd" << endl;
} else {
even(n - 1); // Calls even
}
}
int main() {
even(4);
// Outputs: Even
odd(5);
// Outputs: Odd
return 0;
}
OutputEven Even Nested RecursionThe nested recursion is a form of indirect recursion where a recursive function makes another recursive call inside its own recursive call. It is used for solving complex mathematical and algorithmic problems. #include <iostream>
using namespace std;
int nestedRecursion(int n) {
if (n > 100) {
return n - 10;
} else {
return nestedRecursion(nestedRecursion(n + 11)); // Nested recursive calls
}
}
int main() {
cout << nestedRecursion(95) << endl; // Outputs: 105
return 0;
}
Output91 Advantages of Recursion
Recursion Vs. IterationRecursion is a method where a function calls itself over again and again with modified arguments until it reaches its base case which stops the recursion. Whereas, an iteration involves using loops (such as for, while or do-while) where it involves repeatedly executing blocks of code until a certain condition is met. Recursion or Iteration: When to Use?Recursion
Iteration
Comparison between Recursion and Iteration
Limitations of RecursionThe following are limitations of recursion −
Advertisements
| |||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |