You use recursion in functions for repetitive algorthms. Basically griting a function inside itself for a sort of loop, making your code smaller and cleaner.
my WSQ12 is also a great example of recursion.
in this case the program uses de function gcd to determine the greatest comon divisor, and ans is the gratest common divisor itself, in case that the numbers given by the user are not the same (not x==y), the fuction will call itself but with some changes:
else: | |
if(x>y): | |
ans=gcd((x–y),y) | |
else: | |
ans=gcd(x,(y–x)) | |
return ans |
#Mastery21 use of recursion by Eduardo Merino is licensed under a Creative Commons Attribution 4.0 International License.