🟡 Control Flow  ·  Lesson 24

Recursion in C++

What is recursion?

Recursion is when a function calls itself. Instead of a loop, the function solves a big problem by handing a slightly smaller version of it to another copy of itself — until the problem is small enough to answer directly.

The base case

Every recursion needs a base case — the simplest input, answered without calling again. It is what stops the chain. The other part, the recursive case, calls the function on a smaller input.

💡 Two parts, always

1) Base case — when to stop. 2) Recursive case — call yourself on something smaller. Miss the base case and it never stops.

Example: factorial

Factorial: 5! = 5 × 4 × 3 × 2 × 1. Notice n! = n × (n-1)! — that is the recursion.

C++
#include <iostream>
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;          // base case
    return n * factorial(n - 1);   // recursive case
}

int main() {
    cout << factorial(5) << "\n";
    return 0;
}
Output:
120

Tracing the calls

It helps to see how the calls stack up and then unwind:

Trace of factorial(4)
factorial(4) = 4 * factorial(3)
             = 4 * (3 * factorial(2))
             = 4 * (3 * (2 * factorial(1)))
             = 4 * (3 * (2 * 1))
             = 24

The calls go down to the base case, then the answers multiply back up.

Example: Fibonacci

Each Fibonacci number is the sum of the previous two, with two base cases:

C++
int fib(int n) {
    if (n < 2) return n;             // base: fib(0)=0, fib(1)=1
    return fib(n - 1) + fib(n - 2);  // recursive case
}
// fib(6) → 8
💡 Careful

This simple Fibonacci recalculates the same values many times, so it is slow for large n. A loop or memoisation is far faster there.

Recursion vs iteration

RecursionIteration
UsesFunction calls itselfLoops
MemoryExtra (call stack)Less
Best forTrees, divide & conquerCounting, simple repetition

Anything recursive can be written with a loop, and vice versa — pick whichever reads more clearly for the problem.

Common mistakes

  • Forgetting the base case, causing infinite recursion and a stack overflow.
  • A recursive call that does not move toward the base case.
  • Using naive recursion for Fibonacci on large n (very slow).
  • Forgetting to return the recursive call's result.
🏋️ Practice

Write a recursive function to sum numbers from 1 to n, and another to compute the power base^exp. For each, write down the base case first, then the recursive case.

Summary

  • Recursion is a function calling itself on a smaller input.
  • Every recursion needs a base case to stop, plus a recursive case.
  • Factorial and Fibonacci are classic examples.
  • Recursion uses stack memory; a missing base case causes stack overflow.
  • Use recursion for naturally recursive problems; loops for simple repetition.

Recursion क्या है?

Recursion तब है जब कोई function खुद को call करता है। Loop के बजाय, function एक बड़ी समस्या को उसका थोड़ा छोटा रूप अपनी ही एक और copy को सौंपकर हल करता है — जब तक समस्या इतनी छोटी न हो जाए कि सीधे उत्तर दिया जा सके।

Base case

हर recursion को एक base case चाहिए — सरलतम input, बिना फिर call किए उत्तर दिया गया। यही श्रृंखला रोकता है। दूसरा हिस्सा, recursive case, function को एक छोटे input पर call करता है।

💡 हमेशा दो हिस्से

1) Base case — कब रुकना है। 2) Recursive case — खुद को किसी छोटी चीज़ पर call करना। Base case छूटा तो यह कभी नहीं रुकता।

उदाहरण: factorial

Factorial: 5! = 5 × 4 × 3 × 2 × 1। ध्यान दें n! = n × (n-1)! — यही recursion है।

C++
#include <iostream>
using namespace std;

int factorial(int n) {
    if (n <= 1) return 1;          // base case
    return n * factorial(n - 1);   // recursive case
}

int main() {
    cout << factorial(5) << "\n";
    return 0;
}
Output:
120

Calls को trace करना

यह देखना मददगार है कि calls कैसे जमा होती हैं और फिर समेटती हैं:

factorial(4) का trace
factorial(4) = 4 * factorial(3)
             = 4 * (3 * factorial(2))
             = 4 * (3 * (2 * factorial(1)))
             = 4 * (3 * (2 * 1))
             = 24

Calls base case तक नीचे जाती हैं, फिर उत्तर वापस ऊपर गुणा होते हैं।

उदाहरण: Fibonacci

हर Fibonacci number पिछले दो का योग है, दो base cases के साथ:

C++
int fib(int n) {
    if (n < 2) return n;             // base: fib(0)=0, fib(1)=1
    return fib(n - 1) + fib(n - 2);  // recursive case
}
// fib(6) → 8
💡 सावधान

यह सरल Fibonacci वही values कई बार दोबारा गिनता है, तो बड़े n के लिए धीमा है। वहाँ loop या memoisation कहीं तेज़ है।

Recursion बनाम iteration

RecursionIteration
इस्तेमालFunction खुद को call करता हैLoops
Memoryअतिरिक्त (call stack)कम
किसके लिएTrees, divide & conquerगिनती, सरल दोहराव

कोई भी recursive चीज़ loop से लिखी जा सकती है, और उल्टा भी — जो समस्या के लिए ज़्यादा साफ़ पढ़ा जाए वही चुनें।

आम गलतियाँ

  • Base case भूलना, अनंत recursion और stack overflow पैदा करना।
  • ऐसा recursive call जो base case की ओर न बढ़े।
  • बड़े n पर Fibonacci के लिए naive recursion इस्तेमाल करना (बहुत धीमा)।
  • Recursive call का परिणाम return करना भूलना।
🏋️ अभ्यास

1 से n तक numbers का योग करने को एक recursive function लिखें, और power base^exp निकालने को दूसरा। हर एक के लिए पहले base case लिखें, फिर recursive case।

सारांश

  • Recursion एक function का खुद को छोटे input पर call करना है।
  • हर recursion को रुकने के लिए base case चाहिए, साथ में recursive case।
  • Factorial और Fibonacci classic उदाहरण हैं।
  • Recursion stack memory इस्तेमाल करता है; base case छूटने पर stack overflow होता है।
  • स्वाभाविक रूप से recursive समस्याओं के लिए recursion; सरल दोहराव के लिए loops।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में recursion क्या है?
Recursion तब है जब कोई function किसी समस्या को उसी समस्या के छोटे रूपों में तोड़कर हल करने को खुद को call करता है। हर call एक सरल input पर काम करता है जब तक वह एक base case तक न पहुँचे जो calls की श्रृंखला रोकता है और परिणामों को वापस समेटने देता है।
recursion में base case क्या है?
Base case वह condition है जो recursive function को आगे खुद को call करने से रोकती है, सरलतम input के लिए सीधा उत्तर देते हुए। सही base case के बिना function अंतहीन रूप से खुद को call करता रहेगा और अंततः stack overflow से crash होगा।
C++ में recursion और iteration में क्या अंतर है?
Recursion बार-बार function calls से समस्या हल करता है, जबकि iteration loops इस्तेमाल करता है। Recursion tree traversal जैसी स्वाभाविक रूप से recursive समस्याओं के लिए ज़्यादा सुंदर हो सकता है, पर stack पर हर call के लिए अतिरिक्त memory लेता है; iteration आमतौर पर ज़्यादा memory-कुशल है।
recursion में stack overflow किससे होता है?
Stack overflow तब होता है जब recursion बहुत गहरा चला जाए — अक्सर क्योंकि base case गायब है या कभी नहीं पहुँचता — तो हर call call stack में एक frame जोड़ता रहता है जब तक जगह खत्म न हो और program crash न हो।
C++ में recursion कब इस्तेमाल करूँ?
Recursion उन समस्याओं के लिए उपयुक्त है जो स्वाभाविक रूप से समान उप-समस्याओं में बँटती हैं, जैसे factorials, tree और graph traversal, और merge sort जैसे divide-and-conquer algorithms। सरल गिनती या संचय के लिए loop आमतौर पर ज़्यादा साफ़ और तेज़ है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.