Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 8: File Searcher App
Lecture: Core concept: Recursion

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Here is a mathematical computation that is defined iteratively and that makes it very natural to define it as a recursive function,
0:09 the factorial of number n is just n times the factorial of the smaller number until you get down to a pre defined number that has a factorial known
0:19 and where the factorial of 1 is defined to be 1. We are taking this function factorial and we are calling it again
0:25 within itself with slightly different parameters and we need to make sure that there is somewhere along the way
0:32 some kind of test where we no longer call the function recursively so that is kind of like our if we are if we are remodeling this as a loop
0:39 this is how we would like break out out of the loop, but in recursion, we just return from the function without doing more of recursion,
0:46 let's look at this visually. Here we are going to take factorial of 3, that's going to return 3 times whatever the factorial of 2 is,
0:54 and when we call factorial of 2 we actually are back in the same function but now entirely different data
0:59 so now we are going to say well factorial of 2 is going to be defined to be 2 times factorial of 1 when I pass one down but remember,
1:06 the factorial of 1 is just 1 and as we work our way back we pass 1 up we do 2 * 1 and that's 2 and we pass the 2 back
1:14 and we say 3 * 2 and that is 6 which gives us our answer. This is one of the core concepts of computer science and computer programming,
1:21 it's not used super often but when it is used it so perfectly aligns with the types of problems you are solving,
1:29 if you are dealing with hierarchical data structures or you are dealing with these iteratively defined algorithms, recursion may be what you need.


Talk Python's Mastodon Michael Kennedy's Mastodon