Lesson 24 of 34

Recursion: தன்னைத் தானே அழைக்கும் functionRecursion: Functions that Call Themselves

10 min 35 XP

ஒரு function தன்னையே அழைத்தால் அது recursion. ஒரு பெரிய பிரச்சனையை அதே மாதிரியான சிறிய பிரச்சனையாக மாற்றுகிறோம். நிறுத்த ஒரு base case கட்டாயம் வேண்டும்.

When a function calls itself, that's recursion. We turn a big problem into a smaller one of the same kind. A base case to stop is a must.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Interview-களில் அதிகம் கேட்கப்படுவது: n! (factorial). 5! = 5 × 4 × 3 × 2 × 1 = 120. அதாவது 5! = 5 × 4!

An interview favourite: n! (factorial). 5! = 5 × 4 × 3 × 2 × 1 = 120, which means 5! = 5 × 4!

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

base case இல்லையென்றால் function நிற்காமல் அழைத்துக்கொண்டே இருக்கும்; Python RecursionError தரும்.

Without a base case the function keeps calling itself forever, and Python stops it with a RecursionError.

Challenge

+35 XP

recursion பயன்படுத்தி factorial(n) என்ற function எழுதுங்கள். factorial(0) = 1, factorial(5) = 120.

Write factorial(n) using recursion. factorial(0) = 1 and factorial(5) = 120.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.