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"
Top comments (0)