Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
Recursion
To demonstrate recursion, we need to define a function. The concept is where we have a function that calls itself until a condition is met. It's also easy to write infinite loops with recursion. The condition needing to be met is known as the Base Case. Once we reach that Base Case, we return all the data we've calculated.
Try it out here!
1
2
3
4
5
6
7
8
9
10
11
12
13
test_str = "Hello World!"
def recurse_this(n):
if n == 1:
print(test_str[len(test_str) - n])
print("This is the base case!")
else:
print(test_str[len(test_str) - n])
#print("iteration: " + str(n))
recurse_this(n-1)
recurse_this(len(test_str))
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content