DEV Community

Sunil16666
Sunil16666

Posted on

Recursive Functions in Python

A basic example of a recursive function in python. The function outputs the factorial of num and ignores additional arguments. If additional arguments are provided, the function returns "ACCEPTING ONLY 1 ARGUMENT".

The function calls itself over and over until the condition num == 1 is met. Using recursive functions when dealing with repeatable operations is very useful.

def factorial(num, *args):
    if len(args) == 0:
        if num == 1:
            return 1
        return num * factorial(num - 1)
    return "ACCEPTING ONLY 1 ARGUMENT"
Enter fullscreen mode Exit fullscreen mode

GitHub: https://github.com/Sunil16666/example-code-python/blob/main/Recursive-Factorial-with-ErrorCatching/main.py

Top comments (0)