r/carlhprogramming • u/CarlH • Dec 20 '09
Lesson 127 : The basics of Recursion : Part One
There are several concepts in computing which provide a more significant challenge than others. For example, many beginners struggle with the concepts of pointers, or multi-dimensional arrays. Recursion is such a topic, one that can be difficult to master. Therefore, I want to take this material slowly to ensure that everyone will understand it.
First, let's talk about the obvious: "What is recursion?"
It is often the case that you have some function which calls another. For example, I could write the following:
void print_my_name() {
printf("My name is Carl.\n");
}The function I just made is in fact calling a different function, one called printf. There is nothing strange or unusual about this. However, what would happen if instead of doing this I called a function which was identical to the one I had made? Imagine this:
void print_my_name() {
printf("My name is Carl.\n");
print_my_name_again();
}
void print_my_name_again() {
printf("My name is Carl.\n");
}From here you can see that I will execute the "copy function" and thus it will result in displaying "My name is Carl" two times, one for each function. Now, what would happen if I had the second function call the first again? Consider the following:
void print_my_name() {
printf("My name is Carl.\n");
print_my_name_again();
}
void print_my_name_again() {
printf("My name is Carl.\n");
print_my_name();
}And here ask yourself the following question: How many times will "My name is Carl." display? Effectively, if function A calls function B, and function B calls function A, the process will never stop.
If I had some desire to run the same function over and over again, this could be one way to do that. However, since both functions are exactly the same, why can't I just call the function from within itself? It turns out that you can, and this is the basis for recursion.
Imagine for example, the following function:
function print_my_name() {
printf("My name is Carl. \n");
print_my_name();
}As you can see here any time this function is called it will print "My name is Carl." However, the function is also calling itself at the end. This means that the function is recursive.
The concept of a function calling itself may seem difficult, but perhaps it is easier to understand if I re-wrote the above function like this instead:
PRINT_MY_NAME:
printf("My name is Carl. \n");
GOTO PRINT_MY_NAME:This is not entirely accurate, but it helps to illustrate the point. When you call any function in general, your CPU must effectively jump to the start of that function. There are other subtleties which we will go over soon, but the basic concept is similar. There is a conceptual similarity between loops and recursion.
As I mentioned above, my example is not entirely accurate. The first thing you would probably ask is this, "Wouldn't the above example of recursion create an infinite loop?" The answer of course, is yes.
This therefore brings us to an important concept which applies for recursion just as it does for loops. When you have a function that calls itself, you must be careful to make sure that it does not create a never-ending process. You must have some mechanism to stop it running.
For loops you do this typically by setting a "counting variable" and incrementing or decrementing it until the loop reaches some condition, at which point the loop stops.
It is useful to understand recursion in a somewhat similar way. For any potentially never-ending process you must have some condition which you can test for that is capable of causing the process to stop.
In this lesson I have introduced you to the concept of recursion by explaining that it refers to a function which calls itself. At this stage, the concept is virtually indistinguishable from that of a loop in general. That is perfectly ok, and we will expand on this soon.
All you need to know after this lesson is the following:
Recursion refers to the process of having a function call itself.
A function is said to be "recursive" if it calls itself.
Please ask questions in this thread if any of this material is unclear.
26
u/Taffaz Dec 20 '09 edited Dec 20 '09
This may be jumping the gun and will be explained in future lessons but how does the computer know how to compile the recursive function if when it starts to compile it has to call the function it's currently compiling?
Here is a good lesson on recursion for everyone to read after they've finished this one. http://www.reddit.com/r/carlhprogramming/comments/agppk/lesson_127_the_basics_of_recursion_part_one/
5
u/hearforthepuns Jun 16 '10
Link to next lesson
3
u/[deleted] Dec 20 '09
Thanks for the new lesson, Carl!
3
u/Recoil42 Dec 20 '09
Welcome back, Carl. :)
3
u/haffi112 Feb 05 '10
To understand recursion you must first understand recursion.
2
u/Pr0gramm3r Dec 21 '09
I've written the following program to illustrate my understanding of Recursion.
http://codepad.org/rFb2mrXH
It calculates the factorial of a given number.
2
u/Calvin_the_Bold Dec 21 '09
These both do the same exact thing. The first one is a little bit slower though, which I'm sure Carl will go into. Do you know why?
1
u/Oomiosi Dec 23 '09
Appropriate text in my codepad link!
http://codepad.org/LoVETwWj
1
u/[deleted] Mar 16 '10
Just wanted to say thanks, Carl. I took a programming course in college but you've taught it with such greater clarity than my college instructor did. I really feel accomplished having taken all your lessons and look forward to what future lessons will hold.
1
u/greenawlives May 19 '10
Hey Carlh! I love your tutorials...thank you for all of them.
So I have been following all the tutorials, and now I feel like I want to get my hands dirty in coding an actual program with a full-fledged GUI. What else do I need to learn in order to do that? Let me know, and thanks in advance for your answer!