Professional Documents
Culture Documents
Welcome To Recursion!: (Tutorial) Recursion: Behind The Scenes
Welcome To Recursion!: (Tutorial) Recursion: Behind The Scenes
1x | edX
Welcome to Recursion!
Hi! Welcome to this introduction to recursion, it’s great to have you here. 😀 This is a
very powerful concept in programming… a function that calls itself! I know what
you must be thinking: What?! The function calls itself? Why would you want to do
that? 😅
Recursion is used when the result of a problem depends on the result of smaller
versions of the same problem. Sounds strange? Don't worry! You will learn what it
means with this tutorial. 😀
Meet Factorial 👋
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 1/21
22/06/2020 Discussão | 6.00.1x | edX
The factorial of a positive integer n is the result multiplying all the integers less than
or equal to n. For example, the factorial of 3 is 3 * 2 * 1 (by convention, the factorial
of 0 is 1) Factorial is denoted as n! in mathematics.
This is the code we will be working with. Please take a moment to analyze it.
Base Case
I’m sure that you noticed that there is a call to factorial within the de nition of the
function itself. (please see the diagram below).
You might think, won’t this cause an in nite series of function calls? 😫
Well, it won’t because we have what it’s called a base case, and we update the
argument every time we call the function. The base case is when you specify a value
that will be returned when a condition is met, and no more function calls will be
made. In this example, when n is 0 or 1, the value 1 will be returned, and no more
function calls will be made.
💡 Note: It's very important to update the argument with each recursive function
call. In this case, factorial(n-1) is calling the same function but with the argument
reduced by 1 each time. This way, we guarantee that the argument will eventually
reach our base case, 1 or 0.
But what happens behind the scenes when the function calls itself? Let’s say that you
call the factorial function with 3 as the argument (illustrated below).
If statement
The if condition will be checked. If n is either 0 or 1, the integer 1 will be returned.
Since this is not the case, the else clause is executed.
Else is Executed
In this else clause, you see a return statement in which the parameter n is
multiplied by the result of calling a function. But in this case, the function calls
itself.
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 3/21
22/06/2020 Discussão | 6.00.1x | edX
Once this function call is executed, the program “waits” for the sequence of function
calls to complete.
When the function is called within this function with n-1 as the new argument, you
need to nd the value returned by this new function call before returning a value.
Otherwise, how would the computer know what value to return if the
expression 3 * factorial(3-1) is not complete? We don’t know the value of
factorial(3-1) yet.
As you can see in the diagram below, the program “waits” until a value is returned.
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 4/21
22/06/2020 Discussão | 6.00.1x | edX
The program needs to wait again until a value for factorial(1) is found. The
function call factorial(1) is executed, and the value 1 is returned.
This is the sequence of return statements that are executed during the process. It
continues until we reach the base case, when n is either 1 or 0. When the condition
evaluates to True , the integer 1 is returned.
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 5/21
22/06/2020 Discussão | 6.00.1x | edX
Once a value is returned, those spots that were “waiting” for a result are lled up the
chain.
When the nal function call returns a value, 1, that value is returned to the function
call factorial(2) and the resulting value, 2, is returned to the rst function call
factorial(3) .
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 6/21
22/06/2020 Discussão | 6.00.1x | edX
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 7/21
22/06/2020 Discussão | 6.00.1x | edX
This is the code and the nal result, 6. I added print statements to illustrate the
process. 👍
I really hope that this has been helpful. If you have any questions, please do not
hesitate to ask. Your classmates and Community TAs will always be there to help you.
🙂
Estefania.
15 responses
Add a Response
mlin17
11 days ago
Thank you for the tutorial, I especially like the use of the print() statements at the end.
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 8/21
22/06/2020 Discussão | 6.00.1x | edX
I'm very glad it helped you 🙂
Estefania
Add a comment
Jennymajara
10 days ago
Its very well expained ...thank you so much for it
Thank you very much. I'm very glad it helped you 🙂
Estefania
Add a comment
inovakovic
10 days ago
Thanks a bunch Estefania!
I'm very glad it helped you 🙂
Estefania
Add a comment
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/5… 9/21
22/06/2020 Discussão | 6.00.1x | edX
jamadagneya
9 days ago
i really appreciate all of your work, Estefania... thanks for breaking it down
Thank you very much 🙂 I'm very glad it helped you.
Estefania
Add a comment
dominikwroblewski
9 days ago
Thank you.
I'm very glad it helped you. 🙂
Estefania
Add a comment
school_zxj2020
9 days ago
I'm wondering how recursion works at the hardware level: can you explain how scope is
represented physically?
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 10/21
22/06/2020 Discussão | 6.00.1x | edX
Mmm... I don't know if you can represent the scope physically... but I'm sure creating a new scope needs
some memory...
Python is a garbage collected language which means one you don't need to remember a variable
(because you are not referencing it later in your code) it completely forgets it and leave that portion of
memory free again.
I see in the lecture Grimson says that recursion is an elegant solution yet it is not e cient... I guess
creating new scopes not only uses memory to "remember" the bindings of the variables but it also
consumes memory to actually create and manage the scopes themselves so in the end a recursive
solution uses more resources than an iterative solution; yet in some cases it is easier to read so it is
more 'Pythonic' (Python is a language that is supposed to be explicit which is a nice thing when reading
and understanding code... if you want to know more about this, type 'import this' in your Python shell)
Hi, this is a great resource on this topic: Recursion and Memory Visualization.
Estefania.
Add a comment
hexsoul
9 days ago
wonderfully clear example. thanks!
Thank you very much 🙂 I'm very glad it helped you.
Estefania
Add a comment
keitheit
8 days ago
Thanks. Nicely illustrated examples.
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 11/21
22/06/2020 Discussão | 6.00.1x | edX
Thank you very much 🙂 I'm very glad it helped you.
Estefania
Add a comment
Aquanauta
8 days ago
Thanks for this!. What I still don't understand is, for example in factorial(5), when you
multiply factorial(3) times 4, how is factorial(3) evaluated? In other words, where is the
code to evaluate factorial(3)? I hope I am clear enough. Thanks!
Hi, the code used to evaluate factorial(3) is the factorial function that is already de ned, you are
just calling it one more time with a di erent argument (3 in this case). The same code will run, but now
the value of the parameter will be 3 and the process will continue until the base case is reached.
Hope this helps. Please do not hesitate to ask if you have any further questions. I will be very glad to
help you. 👍
Estefania.
Add a comment
BoyerTim
8 days ago
Awesome!! Any tips for determining how to decide the base case and how to calculate
the appropriate recursive step to make?
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 12/21
22/06/2020 Discussão | 6.00.1x | edX
Thank you very much 🙂 I'm very glad it helped you. My tips would be:
For the base case - ask yourself: where do I want the process to stop? What has to be true for the
process to stop? What values do I already know in advance? For example, we know that 0! and 1!
are 1, so we don't need another recursive call to nd these values. That will give an indication
that they are perfect candidates for the base case.
For the recursive step - ask yourself: what do I want to do with the rst two values? For example,
in factorial(4) , I want to multiply 4 by the result of factorial(3) so that is a hint that the
recursive step should be n*factorial(n-1) where n is the value of the argument. It is very
important to ask yourself what you want to do with each pair of values and a good place to start
analyzing this is taking the rst two values of the recursive sequence and asking: what do I need
to do with these values to get the result I want?
Hope it helps.
Estefania
Add a comment
itsamadeus
6 days ago
Perfectly explained thank youuu!!!
Thank you very much 🙂 I'm very glad it helped you.
Estefania.
Add a comment
demetrilog
6 days ago
Hi Estefania, rstly thanks for the tut!! and in second place my doubt: why the statement
return 1 gives me the result of the function? Thanks in advance !! 🙂
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 13/21
22/06/2020 Discussão | 6.00.1x | edX
Hi demetrilog, I'm very glad it helped you 🙂 When the recursive process reaches this function call
factorial(1) , the process stops because a xed value is returned to the previous call, the value
returned by factorial(1) . That result is then returned to the previous call and so on until all the
values have been returned to the previous call and the nal result has been calculated.
Hope it helps. Please do not hesitate to ask if you have any questions. I will be very glad to help you.
Estefania.
Add a comment
NelsonB
5 days ago
Thank you Estefania. Recursion is the subject that always makes my brain hurt when
trying to come up with a base case and the rest of the recursion function.
I'm very glad it helped you 🙂 Recursion can be a little bit abstract at rst but my tips would be:
For the base case - ask yourself: where do I want the process to stop? What has to be true for the
process to stop? What values do I already know in advance? For example, we know that 0! and 1!
are 1, so we don't need another recursive call to nd these values. That will give an indication
that they are perfect candidates for the base case.
For the recursive step - ask yourself: what do I want to do with the rst two values? For example,
in factorial(4), I want to multiply 4 by the result of factorial(3) so that is a hint that the recursive
step should be n*factorial(n-1) where n is the value of the argument. It is very important to ask
yourself what you want to do with each pair of values and a good place to start analyzing this is
taking the rst two values of the recursive sequence and asking: what do I need to do with these
values to get the result I want?
Hope it helps 👍
Estefania.
Add a comment
fgwnobregae8fb
5 days ago
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 14/21
22/06/2020 Discussão | 6.00.1x | edX
Thank you very much. I'm very glad it helped you 🙂
Estefania.
Add a comment
dmye12576
a day ago
Thank you. These are really helpful.
Add a comment
Add a response:
Pré-visualizar
Enviar
Filtrar tópicos
ltrar tópicos
Todas as Discussões
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 15/21
22/06/2020 Discussão | 6.00.1x | edX
General
Install
Download Python
Philosophy
Graders
Resources
Lecture
Lecture 1
Video: Introduction
Exercises 2
Exercise 5
edX
Sobre
edX for Business
Legal
Connect
Blog
Contate-nos
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 16/21
22/06/2020 Discussão | 6.00.1x | edX
Central de ajuda
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 17/21
22/06/2020 Discussão | 6.00.1x | edX
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 18/21
22/06/2020 Discussão | 6.00.1x | edX
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 19/21
22/06/2020 Discussão | 6.00.1x | edX
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 20/21
22/06/2020 Discussão | 6.00.1x | edX
https://courses.edx.org/courses/course-v1:MITx+6.00.1x+2T2020/discussion/forum/4dd535980d7d486c3b7ec928077572302d857a88/threads/… 21/21