Section 12.4 Recursion
It is possible for a function to call itself! While this might sound like a strange idea, the concept of a function calling itself can make for some really cool features. We call this recursion. Let’s take a look at an example. Please pretend to be the computer as you study the code in the below window.
What does the function
factorial()
do? Try to answer this question ’by hand’ first, by going through the code in your head for multiple different user inputs. Once you think you have an idea, hit the Runbutton to see what the computer does. Is this what you thought would happen? Why or why not?