Recursion TRB CSI › Python Programming › Recursion

Recursion

Recursion is a programming technique in which a function calls itself to solve a problem by reducing it into smaller versions of the same problem. தமிழில்: ஒரு பெரிய problem-ஐ அதே வகையிலான சிறிய-small problem-களாக reduce செய்து, ஒரு functi…

Premium — locked Advanced 26 min 181 cards 25 programs 11 MCQs v3
This is a premium lesson. You can see the shape of every card — unlock to read them. Unlock

Recursion என்பது Python functions-ல் மிகவும் முக்கியமான concept. Simple……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இங்கே show() function உள்ளே மீண்டும் show() call ஆகிறது. இதுதான் recursion-ன் basic idea. ஆனா……

Premium This card is part of the premium lesson. Unlock
List

1. Recursive Call 2. Base Case…

Premium This card is part of the premium lesson. Unlock
Definition

Recursion is a programming technique in which a function calls itself to solve a problem by reducing it into ……

Premium This card is part of the premium lesson. Unlock
Simple Explanation

Suppose நாம்: 5 4 3 2 1 print செய்ய வேண்டும். Normal method: for i in……

Premium This card is part of the premium lesson. Unlock

இங்கே function என்ன செய்கிறது? count(5) ↓ count……

Premium This card is part of the premium lesson. Unlock
Concept
Main concept

Recursion-ஐ ஒரு staircase போல நினைத்துக்கொள்ளுங்கள். count(5) ↓ count(4) ↓ cou……

Premium This card is part of the premium lesson. Unlock
Important
Two most important parts

Recursion-ல் இரண்டு parts com……

Premium This card is part of the premium lesson. Unlock
Important

1. Base Case…

Premium This card is part of the premium lesson. Unlock
Important

Function எப்போது stop ஆக வேண்……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

இதுதான் stopping condition.…

Premium This card is part of the premium lesson. Unlock
List

1. Recursive Case…

Premium This card is part of the premium lesson. Unlock
Explanation

Function தன்னையே மீண்டும் call ச……

Premium This card is part of the premium lesson. Unlock
Memory Trick

Recursion = Base Case + Self Call Very important: Bas……

Premium This card is part of the premium lesson. Unlock
Syntax
General syntax
Explanation

function_name(smaller_value) ……

Premium This card is part of the premium lesson. Unlock
Explanation

return function_name(n - 1)…

Premium This card is part of the premium lesson. Unlock
Explanation

count(5)…

Premium This card is part of the premium lesson. Unlock
Flow

count(5) ↓ Print 5 ↓ count(4) ↓ Print 4 ↓ count(3) ↓ Prin……

Premium This card is part of the premium lesson. Unlock
Explanation
Why base case is needed

இதோ base case இல்லாத recursion: def count(n): print(n) count(n - 1) count(5) ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Recursion error

Common message concept: RecursionError: maximum recursion depth ex……

Premium This card is part of the premium lesson. Unlock
Important
Very important

Recursion என்பது technically infinite loop அல்ல. ஆனால் stopping condition இல்……

Premium This card is part of the premium lesson. Unlock
Flow
Call stack

Recursion புரிய Call Stack concept ரொம்ப முக்கியம். Suppose: def test(n): if n == 0: return print(n) test(n -……

Premium This card is part of the premium lesson. Unlock
Explanation
Call and return

Recursion-ல் இரண்டு phases இர……

Premium This card is part of the premium lesson. Unlock
Explanation

1. Going Down / Calling Phase……

Premium This card is part of the premium lesson. Unlock
Explanation

இதுதான் recursion-ன் most imp……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Example

def show(n): if n == 0: return pr……

Premium This card is part of the premium lesson. Unlock

இதுதான் recursion-ன் real bea……

Premium This card is part of the premium lesson. Unlock
Explanation
Why output reverses

Let's understand inch by inch. show(3) Print Before 3 Call show(2) After 3 இன்னும் execute ஆகவில்லை. Wait செய……

Premium This card is part of the premium lesson. Unlock
Flow
Visual stack

Calling phase: show(3) ↓ show(2) ↓ show(1) ↓ show(0) Returning……

Premium This card is part of the premium lesson. Unlock
Concept
Most important exam concept

Recursion uses: Stack data structure / Call Stack Reason: F……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Stack example

Calls: test(3) test(2) test(1) L……

Premium This card is part of the premium lesson. Unlock
Explanation
Factorial using recursion

Recursion-க்கு classic example:……

Premium This card is part of the premium lesson. Unlock
Output
Result

Recursive formula: n! = n × (……

Premium This card is part of the premium lesson. Unlock
Explanation

return n * factorial(n - 1)…

Premium This card is part of the premium lesson. Unlock
Concept

This line: return n * factorial(n - 1) means: factorial(5) = 5 ×……

Premium This card is part of the premium lesson. Unlock
Dry Run
Factorial dry run
Important
Very important

Recursion-ல் result immediately கிடைக்காமல் இருக்கலாம். Function calls first stack-ல் build ஆகு……

Premium This card is part of the premium lesson. Unlock
Flow
Factorial flow

factorial(5) ↓ 5 * factorial(4) ↓ 4 * factorial(3) ↓ 3 * ……

Premium This card is part of the premium lesson. Unlock
Explanation
Sum using recursion

Find: 1 + 2 + 3 + 4 + 5…

Premium This card is part of the premium lesson. Unlock
Explanation

return n + total(n - 1)…

Premium This card is part of the premium lesson. Unlock
Explanation
Print 1 to n

Recursion பயன்படுத்தி 1 to 5 print செய்யலாம். def p……

Premium This card is part of the premium lesson. Unlock
Explanation
Why ascending

Notice: print_numbers(n - 1) comes before: print(n) So function first dee……

Premium This card is part of the premium lesson. Unlock
Explanation
Print n to 1

def print_numbers(n): if n == 0: ……

Premium This card is part of the premium lesson. Unlock
Concept
Very important concept

இந்த இரண்டு programs compare செய்யுங்கள். Print before recursive call print(n) function(n - 1) Outp……

Premium This card is part of the premium lesson. Unlock
Explanation
Fibonacci using recursion

Fibonacci sequence: 0, 1, 1, ……

Premium This card is part of the premium lesson. Unlock
Formula

F(n) = F(n-1) + F(n-2) Base c……

Premium This card is part of the premium lesson. Unlock
Explanation

return fib(n - 1) + fib(n - 2)…

Premium This card is part of the premium lesson. Unlock
Explanation
Why two recursive calls

For: fib(5) Python calculates: fib(4) + f……

Premium This card is part of the premium lesson. Unlock
Concept

fib(5) / \ fib(4) fib(3) / \ / \ fib(3)……

Premium This card is part of the premium lesson. Unlock
Important

Simple recursive Fibonacci is easy to understand.……

Premium This card is part of the premium lesson. Unlock
Explanation
Example

fib(3) multiple times calculate ஆகலாம். ……

Premium This card is part of the premium lesson. Unlock
Explanation
String recursion

Recursion numbers மட்டும் அல்ல. String processing-க்கும் use செய்யலாம். Exampl……

Premium This card is part of the premium lesson. Unlock
Dry Run
reverse("ABC") = reverse("BC") + "A"
Explanation
Direct recursion

ஒரு function தன்னையே direct-ஆ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

show(n - 1)…

Premium This card is part of the premium lesson. Unlock
Explanation
Indirect recursion

Function A → Function B → மீண்டும் ……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

a(4) Flow: a() ↓ b() ↓ a() ↓ ……

Premium This card is part of the premium lesson. Unlock
Explanation
Tail recursion

Recursive call function-ன் last oper……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Recursive call: count(n - 1) ……

Premium This card is part of the premium lesson. Unlock
Important
Important python point

Some programming languages tail recursion optimization செய்யலாம். ஆனால் ……

Premium This card is part of the premium lesson. Unlock
Comparison
Recursion vs iteration

Recursion மற்றும் Loop இரண்டு……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

count(5) Both same output.…

Premium This card is part of the premium lesson. Unlock
Explanation
When recursion is useful

Recursion especially useful when problem itself recursive structure. Examples: Factoria……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Real time analogy

Folder inside Folder Computer folder: Main Folder │ ├── File ├── Sub Folder │ ├── File │ └── A……

Premium This card is part of the premium lesson. Unlock
Real Life Example
Tree example

Tree structure: A / \ B C / \ D E Each node-க்கும் child nodes இருக்கலாம். Node……

Premium This card is part of the premium lesson. Unlock
Comparison
Base case vs recursive case

Base Case Recursion stop cond……

Premium This card is part of the premium lesson. Unlock
Program
Example
Explanation

Problem smaller செய்து functi……

Premium This card is part of the premium lesson. Unlock
Explanation
Recursive progress

Base case மட்டும் போதாது. ஒவ்வொரு recursive call-லும் base case-க்கு closer ஆக வேண்டும். Correct: count(n - ……

Premium This card is part of the premium lesson. Unlock
Explanation
ஒரு good recursive function-க்கு

1. Base Case இருக்க வேண்டும் 2. Recursive ……

Premium This card is part of the premium lesson. Unlock
Explanation

இதையே முக்கியமான theory point……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 1

No Base Case def test(): test……

Premium This card is part of the premium lesson. Unlock
Output
Result
Common Mistake
Error 2

Wrong Direction def count(n): if n == 0……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 3

Base Case Never Reached def count(n): if n……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 4

Forgetting return Factorial wrong: def fact(n): if n == ……

Premium This card is part of the premium lesson. Unlock
Common Mistake
Error 5

Wrong Base Value Factorial: 0! = 1 So correct: if n == 0:……

Premium This card is part of the premium lesson. Unlock
Explanation
Memory usage

Every recursive call normally requires a new stack frame. Each frame stores things l……

Premium This card is part of the premium lesson. Unlock
Explanation

Each active call has its own ……

Premium This card is part of the premium lesson. Unlock
Explanation
Local scope connection

Previous Local Scope concept இங்கே connect ஆகிறது. def fact(n): ஒவ்வொரு recursive call-க்கும் தனி……

Premium This card is part of the premium lesson. Unlock
Explanation
Return value connection

Recursion return values-ஐ chain ஆக pass செய்யலாம். Factorial: fact(0) returns 1 fact(1) receives 1 return……

Premium This card is part of the premium lesson. Unlock
Explanation
Recursion limit

Python has recursion depth protection. Current limit பார்க்க: import sys print(sys.ge……

Premium This card is part of the premium lesson. Unlock
Important

Technically recursion limit change செய்ய: sys.setrecursionlimit(...) possible……

Premium This card is part of the premium lesson. Unlock
Comparison
Function composition vs recursion

Previous topic-க்கு comparison: Function Composition f(g(x)) ……

Premium This card is part of the premium lesson. Unlock
Comparison
Recursion vs nested function

Nested function: def outer(): def inner()……

Premium This card is part of the premium lesson. Unlock
Explanation
Advantages

Recursion advantages: Code short ஆகலாம் Mathematical definitions easy Tree traver……

Premium This card is part of the premium lesson. Unlock
Explanation
Disadvantages

Recursion disadvantages: Extra memory Function-call overhead Debugging some……

Premium This card is part of the premium lesson. Unlock
Explanation
When not to use

Simple: 1 முதல் 100 வரை print இதற்கு recursion தேவைய……

Premium This card is part of the premium lesson. Unlock
Explanation
When to use

Problem naturally breaks into same smaller problem: fact……

Premium This card is part of the premium lesson. Unlock
Memory Trick

BSR Recursion-க்கு: B → Base Case S → Sel……

Premium This card is part of the premium lesson. Unlock
Memory Trick
Another memory trick

Recursion: Call yourself, but know when to stop. தம……

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Recursion என்றால்?…

Premium This card is part of the premium lesson. Unlock
TRB Point

Function calls itself.…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Recursion stop செய்யும் co……

Premium This card is part of the premium lesson. Unlock
TRB Point

Base Case…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Function self-call பகுதி?…

Premium This card is part of the premium lesson. Unlock
TRB Point

Recursive Case / Recursive Ca……

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Recursion uses which memor……

Premium This card is part of the premium lesson. Unlock
TRB Point

Stack / Call Stack…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Stack principle?…

Premium This card is part of the premium lesson. Unlock
TRB Point

LIFO…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Base case இல்லையென்றால்?…

Premium This card is part of the premium lesson. Unlock
TRB Point

RecursionError ஏற்படலாம்.…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Function calls itself dire……

Premium This card is part of the premium lesson. Unlock
TRB Point

Direct Recursion…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Function A calls B, B call……

Premium This card is part of the premium lesson. Unlock
TRB Point

Indirect Recursion…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Recursive call last operat……

Premium This card is part of the premium lesson. Unlock
TRB Point

Tail Recursion…

Premium This card is part of the premium lesson. Unlock
TRB Point

1. Python tail-call optimizat……

Premium This card is part of the premium lesson. Unlock
TRB Point

Generally No.…

Premium This card is part of the premium lesson. Unlock
Interview Point

What is recursion? Recursion is a technique where a function calls itself……

Premium This card is part of the premium lesson. Unlock
Interview Point

1. Base Case 2. Recursive Case…

Premium This card is part of the premium lesson. Unlock
Interview Point

Additionally, each recursive call must make progress toward the base case. Why is a base case necessary? It p……

Premium This card is part of the premium lesson. Unlock
MCQ

Recursion means:…

Premium This card is part of the premium lesson. Unlock
MCQ

What stops recursion?…

Premium This card is part of the premium lesson. Unlock
MCQ

Recursion primarily uses:…

Premium This card is part of the premium lesson. Unlock
MCQ

Stack follows:…

Premium This card is part of the premium lesson. Unlock
MCQ

What may happen without a bas……

Premium This card is part of the premium lesson. Unlock
MCQ

2 1 1. What is the base case here? ……

Premium This card is part of the premium lesson. Unlock
MCQ

factorial(5) returns:…

Premium This card is part of the premium lesson. Unlock
MCQ

A function directly calling i……

Premium This card is part of the premium lesson. Unlock
MCQ

Recursive call at the end of ……

Premium This card is part of the premium lesson. Unlock
MCQ

Which is generally more memor……

Premium This card is part of the premium lesson. Unlock
MCQ

In recursion, each function c……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 1

Countdown def count(n): if n ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 2

Sum def total(n): if n == 0: ……

Premium This card is part of the premium lesson. Unlock
Dry Run

4 + 3 + 2 + 1 + 0…

Premium This card is part of the premium lesson. Unlock
Practice
Practice 3

Factorial def fact(n): if n == ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 4

Ascending Print def show(n): ……

Premium This card is part of the premium lesson. Unlock
Practice
Practice 5

Find the Error def show(n): if n == 0: return show(n + 1)……

Premium This card is part of the premium lesson. Unlock
Summary

Recursion basic structure: def function(n): if base_case: return function(smaller_problem) Core flow: Problem……

Premium This card is part of the premium lesson. Unlock
Summary

1. Base Case இருக்க வேண்டும் 2. Recursive C……

Premium This card is part of the premium lesson. Unlock
Explanation
Short description

Recursion என்பது ஒரு function தன்னையே call செய்து ஒரு பெரிய problem-ஐ அதே வகையிலான smaller problems-ஆக reduce……

Premium This card is part of the premium lesson. Unlock
Text size
17px
Theme
Contents
Print / PDF
Program