Skip to content

Instantly share code, notes, and snippets.

@ayubmetah
Created December 20, 2020 01:17
Show Gist options
  • Save ayubmetah/417269bba14889351bd5fc5556d4ae27 to your computer and use it in GitHub Desktop.
Save ayubmetah/417269bba14889351bd5fc5556d4ae27 to your computer and use it in GitHub Desktop.
An example of recursion in Python at work.
#recursions example-2
def factorial(n):
print("Factoriaal called with " + str(n))
if n < 2:
print("Returning 1")
return 1
result = n * factorial(n - 1)
print("Returning " + str(result) + " for factorial of " + str(n))
return result
factorial(4)
#output
#Factoriaal called with 4
#Factoriaal called with 3
#Factoriaal called with 2
#Factoriaal called with 1
#Returning 1
#Returning 2 for factorial of 2
#Returning 6 for factorial of 3
#Returning 24 for factorial of 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment