# Recursive Function (Recursion) in Python

In Python programming, a function is allowed to call itself. A function which calls itself directly or indirectly again and again until some specified condition is satisfied is known asRecursive Function.

A recursive function is a function defined in terms of itself via self-calling expressions. This means that the function will continue to call itself and repeat its behavior until some condition is satisfied to return a value .

**Recursion** is the process of defining something in terms of itself (Re Occuring). Recursion has many negatives. It repeatedly invokes the mechanism, and consequently increases the overhead of function calls. This repetition can be expensive in terms of both processor time and memory space.

Each recursive call causes another copies of the defined function, these set of copies can consume considerable amount of memory space.

## Recursive Function Example

#### Python program to find factorial of a given number recursively to illustrate recursive function

```
# Program to find factorial using recursive function
# Recursive function definition
def factorial(n):
if n==0:
return 1
else:
return n * factorial(n-1)
# Reading number from user
number = int(input('Enter number: '))
# Displaying factorial
if(number< 0):
print('Factorial does not exist!')
else:
print('Factorial of %d is %d' %(number,factorial(number)))
```

Output

Run 1: ---------------- Enter number: 13 Factorial of 13 is 6227020800 Run 2: ---------------- Enter number: -9 Factorial does not exist! Run 3: ---------------- Enter number: 0 Factorial of 0 is 1

## One Line Function Definition Code for Factorial Using Recursion in Python

```
# Program to find factorial using recursive function
# Recursive function definition
def factorial(n):
return 1 if n==0 else n* factorial(n-1)
# Reading number from user
number = int(input('Enter number: '))
# Displaying factorial
if(number< 0):
print('Factorial does not exist!')
else:
print('Factorial of %d is %d' %(number,factorial(number)))
```

Output

Enter number: 5 Factorial of 5 is 120