Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can display the final result returned by your recursive function in two ways within a sub function:

  1. You can use the print() function to print the final result returned by your recursive function. For example:
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

def calculate_factorial(n):
    result = factorial(n)
    print("The factorial of ", n, " is: ", result)

calculate_factorial(5)

Output:

The factorial of  5  is:  120
  1. You can return the final result from the recursive function and assign it to a variable within the sub function, and then use the print() function to print the result. For example:
def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)

def calculate_factorial(n):
    result = factorial(n)
    return result

final_result = calculate_factorial(5)
print("The factorial of 5 is: ", final_result)

Output:

The factorial of 5 is: 120