🟡 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.
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
🟡 Control Flow and Core Programming
Example File
recursion.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| base case | Base case used in Recursion in C++ programming. |
| recursive case | Recursive case used in Recursion in C++ programming. |
| stack | Stack used in Recursion in C++ programming. |
| factorial | Factorial used in Recursion in C++ programming. |
| call | Call 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
- Write recursive sum of natural numbers.
- 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.
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
🟡 Control Flow and Core Programming
Example File
recursion.cppMain Focus
Concept + syntax + practical C++ program
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
| Term | Meaning / Use |
|---|---|
| base case | Base case used in Recursion in C++ programming. |
| recursive case | Recursive case used in Recursion in C++ programming. |
| stack | Stack used in Recursion in C++ programming. |
| factorial | Factorial used in Recursion in C++ programming. |
| call | Call 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
- Write recursive sum of natural numbers.
- 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.