C++ में Recursion
In this page:
return_type function_name(parameters) {
if (base_case_condition) {
return base_value;
}
return function_name(smaller_input); // recursive case
}
Recursion का Introduction
Recursion तब होता है जब एक function खुद को call करता है, repetition control करने के लिए एक loop की condition की बजाय function calls इस्तेमाल करते हुए।
हर call original problem के एक छोटे हिस्से पर काम करती है, और progress एक loop counter increment होने की बजाय हर call पर बदलते arguments के जरिए होता है।
उदाहरण: Introduction to Recursion
// Include std::cout and std::cin
#include <iostream>
int countdown(int n) {
if (n == 0) return 0;
// Print to the console with cout
std::cout << n << " ";
// Send countdown(n - 1) back to the caller
return countdown(n - 1);
}
// Program execution starts in main()
int main() {
countdown(5);
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Base Case और Recursive Case
हर recursive function को एक base case चाहिए — एक condition जहाँ यह खुद को call करना बंद कर देता है और बस सीधे एक value return करता है — इसके बिना, function stack memory खत्म होने और crash होने तक indefinitely खुद को call करता रहेगा।
Recursive case वह हिस्सा है जो function को दोबारा call करता है, हमेशा उस base case के करीब move करते हुए।
उदाहरण: Base Case and Recursive Case
#include <iostream>
int factorial(int n) {
if (n == 0) return 1; // base case
return n * factorial(n - 1); // recursive case
}
int main() {
std::cout << factorial(5) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Fibonacci Series
Fibonacci sequence, जहाँ हर number इससे पहले के दो का sum है, naturally recursion पर map होता है: fib(n) = fib(n-1) + fib(n-2), fib(0) और fib(1) base cases के साथ।
यह लगभग exactly mathematical definition को mirror करता है, यही एक कारण है कि यहाँ recursion elegant महसूस होता है भले ही यह सबसे efficient implementation न हो।
उदाहरण: Fibonacci Series
// Include std::cout and std::cin
#include <iostream>
int fib(int n) {
if (n <= 1) return n;
// Send fib(n - 1) + fib(n - 2) back to the caller
return fib(n - 1) + fib(n - 2);
}
// Program execution starts in main()
int main() {
// Loop: repeat while the condition holds
for (int i = 0; i < 8; i++) {
// Print to the console with cout
std::cout << fib(i) << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Stack Safety
हर recursive call उस call के local variables और बाद में कहाँ resume करना है यह track करने के लिए program के call stack में एक नया frame add करती है, और बहुत सारे unfinished calls (deep recursion) stack करना available stack memory खत्म कर सकता है, एक stack overflow crash cause करते हुए।
यही वजह है कि recursive solutions को एक base case चाहिए जो एक reasonable संख्या में calls के अंदर reliably पहुंचा जाए।
उदाहरण: Stack Safety
#include <iostream>
int sumTo(int n) {
if (n == 0) return 0;
return n + sumTo(n - 1);
}
int main() {
std::cout << sumTo(1000) << std::endl;
// sumTo(1000000); // risks stack overflow: too many stacked call frames
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Iterative बनाम Recursive
लगभग कोई भी recursive algorithm इसकी बजाय एक iterative loop की तरह दोबारा लिखा जा सकता है, और iterative versions आमतौर पर तेज़ होते हैं और काफी कम memory इस्तेमाल करते हैं, क्योंकि वे repeated function calls और stack frames के overhead से बचते हैं।
Recursion फिर भी अक्सर तब चुना जाता है जब यह code की logic को dramatically clearer बनाता है, जैसे tree traversal के साथ।
उदाहरण: Iterative vs Recursive
// Include std::cout and std::cin
#include <iostream>
int factorialRecursive(int n) {
if (n == 0) return 1;
// Send n * factorialRecursive(n - 1) back to the caller
return n * factorialRecursive(n - 1);
}
int factorialIterative(int n) {
// Declare result and set it to 1
int result = 1;
// Loop: repeat while the condition holds
for (int i = 1; i <= n; i++) {
result *= i;
}
// Send result back to the caller
return result;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << factorialRecursive(5) << " " << factorialIterative(5) << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- base case भूल जाना, ताकि function हमेशा के लिए खुद को call करता रहे और stack overflow हो जाए।
- base case की ओर move न करना, उदाहरण के लिए
f(n - 1)की बजायf(n)call करना। - बड़े n के साथ Fibonacci जैसी किसी चीज़ के लिए naive recursion इस्तेमाल करना, जो exponentially काम दोहराता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions