Lesson 24 of 34

Recursion: ख़ुद को बुलाने वाले Functions

10 min 35 XP

जब function ख़ुद को ही बुलाता है, उसे recursion कहते हैं। बड़ी समस्या को उसी तरह की छोटी समस्या में बदलते हैं। रुकने के लिए base case ज़रूरी है।

Example: edit me and press Run

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

इंटरव्यू का पसंदीदा सवाल: n! (factorial)। 5! = 5 × 4 × 3 × 2 × 1 = 120, यानी 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 से रोक देता है।

Challenge

+35 XP

recursion से factorial(n) लिखें। factorial(0) = 1 और factorial(5) = 120।

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