Download presentation
Presentation is loading. Please wait.
1
Recursion Output Input
Recursion is a programming concept whereby a function invokes itself. Recursion is typically used to solve problems that are decomposable into subproblems that are just like the original problem, but a step closer to being solved.
2
Recursion w/out a Base Case
void foo(string str) { printf(“%s\n”, str); foo(str); } Here’s a recursive function called foo. What happens when this function is called? foo will print a string and call itself, which will print another string and call itself again, which will print another string and call itself yet again... and on and on infinitely. However, every time a function is called, some space is set aside in the stack called a frame and every function call results in a new frame being created. So, if foo calls itself infinitely, we’ll eventually run out of stack memory and try to access memory that doesn’t belong to us, in which case our program will fail with a segmentation fault. To avoid this situation, we need to make sure there’s always a way to break out of recursive calls -- we’ll call this the base case. When the base condition is met, the function should return without making a recursive call. Let’s make sure we have a base case in the next example :)
3
Factorial n! = n * (n - 1) * (n - 2) * … * 1
For our next example, let’s think about the factorial operation. The factorial operation is a perfect candidate for recursion because it is a problem that can easily be broken up into similar smaller problems! 5! = 5 * 4 * 3 * 2 * 1 So if we want to calculate the factorial of 5, we can think of it as multiplying 5 by the factorial of 4: 5! = 5 * 4! Similarly, to calculate the factorial of 4, we multiply 4 by the factorial of 3: 4! = 4 * 3! And so on... Let’s implement a factorial function in C!
4
unsigned int factorial(unsigned int n) { if (n <= 1) return 1; }
else return n * factorial(n - 1); This recursive function calculates the factorial of its integer argument, and unlike the last function we saw, this one has a base case! Think through what happens when you pass this function an argument of 3: factorial(3) returns 3 * factorial(2) factorial(2) returns 2 * factorial(1) factorial(1) is the base case and returns 1
5
factorial(3) = 3 * factorial(2)
1 Once the base case is reached, all of the factorial() calls will return in succession to give us an answer of 6. Let’s see what’s going on in the stack as this recursive function executes!
6
heap factorial(3) main() stack
Let’s zoom in on the stack as this function executes: main() calls factorial(3) Every function call results in a new frame being created. These frames are arranged on top of each other in the stack, with the frame for the most recently called function (the active frame) at the top. factorial(3) main() stack
7
heap factorial(2) factorial(3) main() stack main() calls factorial(3)
factorial(3) calls factorial(2) factorial(2) is now the active frame. factorial(3) main() stack
8
heap factorial(1) factorial(2) factorial(3) main() stack
main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) is now the active frame. factorial(3) main() stack
9
heap 1 factorial(2) factorial(3) main() stack
main() calls factorial(3) factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(1) returns 1 to factorial(2). Its frame is destroyed, and the one for the function below (in this case factorial(2)) becomes active again. factorial(3) main() stack
10
heap 2 factorial(3) main() stack main() calls factorial(3)
factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(2) multiplies 1 * 2 and returns 2 to factorial(3) factorial(2) returns 2 to factorial(2). Its frame is destroyed, and the one for the function below (in this case factorial(3)) becomes active again. factorial(3) main() stack
11
heap 6 main() stack main() calls factorial(3)
factorial(3) calls factorial(2) factorial(2) calls factorial(1) factorial(1) returns 1 to factorial(2) factorial(2) multiplies 1 * 2 and returns 2 to factorial(3) factorial(3) multiplies 2 * 3 and returns 6 to main() factorial(3) returns 6 to main(). Its frame is destroyed, and the one for the function below (in this case main()) becomes active again. Nice! Our function calculated the correct answer! 3! = 3 * 2 * 1 = 6 6 main() stack
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.