🟡 Control Flow  ·  Lesson 24

Recursion in C++

Recursion in C++

What is Recursion in C++?

Recursion in C++
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Level
🟡 Control Flow and Core Programming
Example File
recursion.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
base caseBase case used in Recursion in C++ programming.
recursive caseRecursive case used in Recursion in C++ programming.
stackStack used in Recursion in C++ programming.
factorialFactorial used in Recursion in C++ programming.
callCall used in Recursion in C++ programming.

Syntax / Pattern

function calls itself with smaller input.

Example Program

#include <iostream>
using namespace std;
int fact(int n){
    if(n == 0) return 1;
    return n * fact(n-1);
}
int main(){
    cout << fact(5);
    return 0;
}

Expected Output

120

Program Explanation

  • fact(5) becomes 5*fact(4).
  • Base case fact(0) returns 1.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • factorial
  • tree traversal
  • divide and conquer

Common Mistakes

  • Missing base case.
  • Using recursion where simple loop is better.

Practice Tasks

  1. Write recursive sum of natural numbers.
  2. Trace fact(4) step by step.

Summary

Recursion in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

What is Recursion in C++?

Recursion in C++
Recursion occurs when a function calls itself. It needs a base case to stop and a recursive case to reduce the problem.
Level
🟡 Control Flow and Core Programming
Example File
recursion.cpp
Main Focus
Concept + syntax + practical C++ program

Why should you learn this?

  • It helps you write correct and readable C++ programs.
  • It is used repeatedly in school practicals, projects and competitive programming.
  • It builds the base for advanced topics such as OOP, STL and data structures.

Important Terms

TermMeaning / Use
base caseBase case used in Recursion in C++ programming.
recursive caseRecursive case used in Recursion in C++ programming.
stackStack used in Recursion in C++ programming.
factorialFactorial used in Recursion in C++ programming.
callCall used in Recursion in C++ programming.

Syntax / Pattern

function calls itself with smaller input.

Example Program

#include <iostream>
using namespace std;
int fact(int n){
    if(n == 0) return 1;
    return n * fact(n-1);
}
int main(){
    cout << fact(5);
    return 0;
}

Expected Output

120

Program Explanation

  • fact(5) becomes 5*fact(4).
  • Base case fact(0) returns 1.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.

Where will you use it?

  • factorial
  • tree traversal
  • divide and conquer

Common Mistakes

  • Missing base case.
  • Using recursion where simple loop is better.

Practice Tasks

  1. Write recursive sum of natural numbers.
  2. Trace fact(4) step by step.

Summary

Recursion in C++ is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.

← Back to C++ Tutorial