GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Recursion in Python - GeeksforGeeks Recursion in PythonLast Updated : 19 May, 2026 Recursion is a programming technique where a function calls itself either directly or indirectly to solve a problem. It is commonly used for:
Working of RecursionA recursive function calls itself in its body. Let's see basic structure of recursive function:
![]() Recursive function contains two key parts:
ExamplesExample 1: This code defines a recursive function to calculate factorial of a number, where function repeatedly calls itself with smaller values until it reaches the base case.
Output 120 Explanation:
Example 2: This code defines a recursive function to calculate nth Fibonacci number, where each number is the sum of the two preceding ones, starting from 0 and 1.
Output 55 Explanation:
Types of RecursionRecursion can be broadly classified into two types: tail recursion and non-tail recursion. The main difference between them is related to what happens after recursive call.
Example: This code compares tail recursion and non-tail recursion using two versions of factorial function one with an accumulator (tail-recursive) and one with multiplication after recursive call (non-tail-recursive).
Output 120 120 Explanation:
When to Avoid Recursion
Recursion vs IterationRecursion and iteration are two common techniques used to repeat tasks in programming below table highlights the key differences between them:
Comment Explore | ||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |