Lesson 24 of 34

Recursion: स्वतःलाच call करणारी Functions

10 min 35 XP

Function स्वतःलाच call करते तेव्हा ते 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.

Interview चा आवडता: 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 स्वतःला कायम call करत राहते, आणि 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.