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.