 Here's the definition of recursion from the book. We're going to take a problem and break it down into smaller and smaller sub-problems until we get one small enough that we can solve it trivially and then put all the results together. And this usually involves a method that we'll call itself. That's sort of abstract. Let's look at some examples of recursion. You may have seen a picture like this somewhere along the line that shows a vase on a table in front of a painting that shows a vase in front of a table which is in front of a painting of a vase on a table which is in front of a painting of a vase on a table and so on and so forth until finally it gets too small to be worth drawing and that's when we stop. Okay, clever visual trick. Let's consider another example that might be more close to home and that would be a 16-person competition bracket. You can think of this 16-person bracket as two 8-person brackets. Each of those 8-person brackets is considered to be two 4-person brackets and the 4-person bracket can be considered to be two 2-person brackets until we finally get down to a single person and that's the lowest level. Getting on to what we're doing in this course let's consider a linked list. If I were to remove the head of the list what would I have? I'd have another smaller list. If I got rid of the head of that list I'd have yet a shorter list and I could keep doing this until I finally got down to the empty list and that's where I'd have to stop because there's no place else to go. When we have data structures that are defined in terms of themselves like the tournament bracket or the linked list recursion is a good way to deal with these structures. Now, here's the key of this. Usually recursion involves a method calling itself. So first we need to investigate what happens when a method calls another method and this is going to be a review of something that we talked about in computer science 75. So here I have a main method that is going to... excuse me... I have a main method that's going to call the calc1 method and the calc1 method will call the calc2 method and each of them is going to return some value. Let's see what happens when we put this into action. When we call calc1 in this line we have to go on hold because we can't decide to result until we know what calc1 returns to us. This puts another... what's called a stack frame on the Java's internal stack and now we know what a stack is so it's beginning to make sense when we call it a stack frame and is now 10 and now we get to this statement and now in order to figure out what the product is we have to call calc2. We have to put this statement on hold and call calc2 and still being 10. The sum becomes 15 and now we're going to return the sum. When we return the sum, two things happen. First, the value that we're returning replaces the function call and I'm calling them functions instead of method but it's the same thing. And the second thing that happens is the stack frame for the method we're returning from goes away. Now I have 15 times 6 which is 90. I can do the assignment and I can return product. Again, two things happen. The 90 that I'm returning replaces the method call. The stack frame disappears and I'm ready to proceed with my main method which is the only one that's left on the stack. So that's a review of how method calls work every time we call a method. We have to go on hold open up a new stack frame and then when we return we return the value and we pop off the stack frame to return to where we left off. So now let's see how this works when a method calls itself. What we're going to do is we're going to give the sum of this list. The sum of the list 5, 3, 7 that's 5 plus the sum of the remaining list 3 and 7. Well what's the sum of 3 and 7? That's 3 plus the sum of whatever 7 is and that's 7 plus the sum of the empty list and that's 0. So we have 0 plus 7 plus 3 plus 5 that will give us the sum of the whole thing. So that's the idea of what we want to do. And here's the Java that does that. Here's our sum method and it's going to be given the head of a list. If the head of the list is null we return 0 because an empty list has a sum of 0, total of 0 for those elements. Otherwise we'll get the data from the head node and then add that to the sum of everything else, the rest of the list which we access by saying head.getNext. So let's look at main where we're going to say find the sum of the starting at the head node of this list 5, 3, 7. I can't figure out what total is until I evaluate the right hand side here, which means I have to call the sum method. Head will be 5 because that's what getHead gave us. That's not null. So I'm going to return 5 plus whatever the sum is of the rest of the list. That means I have to go on hold because I can't return the whole thing until I find out what this second operand works out to. That means I'm going to have to call sum again. This time with head.getNext and so now the head is going to be 3. The sum of this list is going to be 3 plus the sum of whatever comes after 3. That means I have to go on hold again because I can't return it until I know what this second operand works out to. I call sum one more time and this time 7 is the head and I'm going to return 7 plus the sum of whatever the next element is. Again I have to go on hold because I have to figure out this second part. I call one more time. This time the head is the null element and finally I have something I can return. I can return as 0 because I know what the sum of an empty list is. Again return does two things. First thing it does is it replaces the method call with the return value which is 0 and it pops off the stack frame. Now that I have the 7 plus 0 I can return that to the person who called me. This 7 plus 0 is going to fill in this thing that I went on hold for. The 7 plus 0 gets returned replaces the function call and now I'm back in this call where I had 3 plus 7. Now I have a result that I can return. I return that to the person who called me which is the 10. Now I'm back here. I have 5 plus 10 which is 15 and finally I can return that to main and the 15 replaces all of that and that becomes my total. That's the idea of what happens when a function calls itself. We go on hold and we keep calling the function over and over again until we get down to a case where we know what the answer is and then we climb back down the stack replacing the values one at a time until we get to the bottom level where we started.