Created
December 20, 2020 01:17
-
-
Save ayubmetah/417269bba14889351bd5fc5556d4ae27 to your computer and use it in GitHub Desktop.
An example of recursion in Python at work.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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