SlideShare a Scribd company logo
Intro to
Recursion
What is
recursion?
Throughout your computer science journey so
far, you’ve most likely heard of iteration and
multiple iteration, which is repeating a
particular block of code for a certain amount of
times until you stop
These are represented as FOR loops and
WHILE loops
But iteration may not be the only way to solve
certain problems, and there may even be a
better way
An alternative way for programming problem
solving is called recursion
What is recursion?
Recursion happens when a function calls
ITSELF. This can be a strange concept for you
to wrap your head around. But it CAN be done
We will be building a recursive function to return
the factorial of a number. A factorial is the
product of a number and all the positive integers
that are smaller than it
(For all intents and purposes, this will be in
pseudocode so you can use any language you
want for your examples)
We will call this function factorial (int
number)
Recursion
Recursion
Recursion
Recursion
Recursion
Recursion
Recursion
Recursion
What is recursion?
The factorial function takes in ONE
parameter and that’s the number we are
trying to find the factorial of. Let’s assume
that no-one will input a number less than 1
There are 2 parts to a recursive function:
The first is the base case, which is critical for
getting our recursive function to EXIT
if(number == 1){
return 1;
}
We already know that the factorial of 1 is 1,
so we can say:
What is recursion?
We can write the recursive step as:
return number * factorial(number – 1).
Our completed function is:
int factorial (int number) {
if number == 1
return 1
return number * factorial(number – 1);
}
The next part is the recursive
step. This is where we get the
function to call itself. What do we
know about factorials? That the
factorial of a number CAN be
equal to the number itself
[multiplied by] the factorial of the
previous number
What is recursion?
Now let’s put a number into this function
as an example and see how we do, and
see how recursive functions operate
Factorial (4) 4 · Factorial (3)
=
3 · Factorial (2)
2 · Factorial (1)
Let’s try 4:
Every time we call the recursive function,
it gets pushed to the top of the stack until
we hit a base case (number = 1). Then
that value gets returned and all the
recursive calls get popped off the stack
one by one. 4 * 3 * 2 * 1 = 24, which IS 4!
This was a small intro to recursion!
CREDITS: This presentation template was
created by Slidesgo, and includes icons by
Flaticon, infographics & images by Freepik
and contents by Swetha Tandri
Thanks
Do you have any questions?
youremail@freepik.com
+91 620 421 838
yourwebsite.com
Please keep this slide for attribution
Alternative resources
Here’s an assortment of alternative resources whose style fits that of this template:
Vectors
● Desktop and smartphone app development
Resources
Did you like the resources on this template? Get them on our other websites.
Vectors
● Flat cms concept in blue shades
Instructions for use
If you have a free account, in order to use this template, you must credit Slidesgo by keeping the Thanks slide. Please
refer to the next slide to read the instructions for premium users.
As a Free user, you are allowed to:
● Modify this template.
● Use it for both personal and commercial projects.
You are not allowed to:
● Sublicense, sell or rent any of Slidesgo Content (or a modified version of Slidesgo Content).
● Distribute Slidesgo Content unless it has been expressly authorized by Slidesgo.
● Include Slidesgo Content in an online or offline database or file.
● Offer Slidesgo templates (or modified versions of Slidesgo templates) for download.
● Acquire the copyright of Slidesgo Content.
For more information about editing slides, please read our FAQs or visit our blog:
https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school
As a Premium user, you can use this template without attributing Slidesgo or keeping the Thanks slide.
You are allowed to:
● Modify this template.
● Use it for both personal and commercial purposes.
● Hide or delete the “Thanks” slide and the mention to Slidesgo in the credits.
● Share this template in an editable format with people who are not part of your team.
You are not allowed to:
● Sublicense, sell or rent this Slidesgo Template (or a modified version of this Slidesgo Template).
● Distribute this Slidesgo Template (or a modified version of this Slidesgo Template) or include it in a database or in
any other product or service that offers downloadable images, icons or presentations that may be subject to
distribution or resale.
● Use any of the elements that are part of this Slidesgo Template in an isolated and separated way from this
Template.
● Register any of the elements that are part of this template as a trademark or logo, or register it as a work in an
intellectual property registry or similar.
For more information about editing slides, please read our FAQs or visit our blog:
https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school
Instructions for use (premium users)
This presentation has been made using the following fonts:
Alata
(https://fonts.google.com/specimen/Alata)
Archivo
(https://fonts.google.com/specimen/Archivo)
Fonts & colors used
#041e52 #ffffff #073899 #4f7bc8
#80b2f1 #f4f6f8 #dbe8f1
Create your Story with our illustrated concepts. Choose the style you like the most, edit its colors,
pick the background and layers you want to show and bring them to life with the animator panel!
It will boost your presentation. Check out how it works.
Storyset
Pana Amico Bro Rafiki Cuate
You can easily resize these resources without losing quality. To change the color, just ungroup the resource and click
on the object you want to change. Then, click on the paint bucket and select the color you want. Group the resource again
when you’re done. You can also look for more infographics on Slidesgo.
Use our editable graphic resources...
Recursion with details Implementation.pptx
Recursion with details Implementation.pptx
JANUARY FEBRUARY MARCH APRIL MAY JUNE
PHASE 1
PHASE 2
Task 1
Task 2
Task 1
Task 2
JANUARY FEBRUARY MARCH APRIL
PHASE 1
Task 1
Task 2
Recursion with details Implementation.pptx
Recursion with details Implementation.pptx
You can resize these icons without losing quality.
You can change the stroke and fill color; just select the icon and click on the paint bucket/pen.
In Google Slides, you can also use Flaticon’s extension, allowing you to customize and add even more icons.
...and our sets of editable icons
Educational Icons Medical Icons
Business Icons Teamwork Icons
Help & Support Icons Avatar Icons
Creative Process Icons Performing Arts Icons
Nature Icons
SEO & Marketing Icons
Recursion with details Implementation.pptx

More Related Content

Recursion with details Implementation.pptx

  • 2. What is recursion? Throughout your computer science journey so far, you’ve most likely heard of iteration and multiple iteration, which is repeating a particular block of code for a certain amount of times until you stop These are represented as FOR loops and WHILE loops But iteration may not be the only way to solve certain problems, and there may even be a better way An alternative way for programming problem solving is called recursion
  • 3. What is recursion? Recursion happens when a function calls ITSELF. This can be a strange concept for you to wrap your head around. But it CAN be done We will be building a recursive function to return the factorial of a number. A factorial is the product of a number and all the positive integers that are smaller than it (For all intents and purposes, this will be in pseudocode so you can use any language you want for your examples) We will call this function factorial (int number) Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion
  • 4. What is recursion? The factorial function takes in ONE parameter and that’s the number we are trying to find the factorial of. Let’s assume that no-one will input a number less than 1 There are 2 parts to a recursive function: The first is the base case, which is critical for getting our recursive function to EXIT if(number == 1){ return 1; } We already know that the factorial of 1 is 1, so we can say:
  • 5. What is recursion? We can write the recursive step as: return number * factorial(number – 1). Our completed function is: int factorial (int number) { if number == 1 return 1 return number * factorial(number – 1); } The next part is the recursive step. This is where we get the function to call itself. What do we know about factorials? That the factorial of a number CAN be equal to the number itself [multiplied by] the factorial of the previous number
  • 6. What is recursion? Now let’s put a number into this function as an example and see how we do, and see how recursive functions operate Factorial (4) 4 · Factorial (3) = 3 · Factorial (2) 2 · Factorial (1) Let’s try 4: Every time we call the recursive function, it gets pushed to the top of the stack until we hit a base case (number = 1). Then that value gets returned and all the recursive calls get popped off the stack one by one. 4 * 3 * 2 * 1 = 24, which IS 4! This was a small intro to recursion!
  • 7. CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon, infographics & images by Freepik and contents by Swetha Tandri Thanks Do you have any questions? youremail@freepik.com +91 620 421 838 yourwebsite.com Please keep this slide for attribution
  • 8. Alternative resources Here’s an assortment of alternative resources whose style fits that of this template: Vectors ● Desktop and smartphone app development
  • 9. Resources Did you like the resources on this template? Get them on our other websites. Vectors ● Flat cms concept in blue shades
  • 10. Instructions for use If you have a free account, in order to use this template, you must credit Slidesgo by keeping the Thanks slide. Please refer to the next slide to read the instructions for premium users. As a Free user, you are allowed to: ● Modify this template. ● Use it for both personal and commercial projects. You are not allowed to: ● Sublicense, sell or rent any of Slidesgo Content (or a modified version of Slidesgo Content). ● Distribute Slidesgo Content unless it has been expressly authorized by Slidesgo. ● Include Slidesgo Content in an online or offline database or file. ● Offer Slidesgo templates (or modified versions of Slidesgo templates) for download. ● Acquire the copyright of Slidesgo Content. For more information about editing slides, please read our FAQs or visit our blog: https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school
  • 11. As a Premium user, you can use this template without attributing Slidesgo or keeping the Thanks slide. You are allowed to: ● Modify this template. ● Use it for both personal and commercial purposes. ● Hide or delete the “Thanks” slide and the mention to Slidesgo in the credits. ● Share this template in an editable format with people who are not part of your team. You are not allowed to: ● Sublicense, sell or rent this Slidesgo Template (or a modified version of this Slidesgo Template). ● Distribute this Slidesgo Template (or a modified version of this Slidesgo Template) or include it in a database or in any other product or service that offers downloadable images, icons or presentations that may be subject to distribution or resale. ● Use any of the elements that are part of this Slidesgo Template in an isolated and separated way from this Template. ● Register any of the elements that are part of this template as a trademark or logo, or register it as a work in an intellectual property registry or similar. For more information about editing slides, please read our FAQs or visit our blog: https://slidesgo.com/faqs and https://slidesgo.com/slidesgo-school Instructions for use (premium users)
  • 12. This presentation has been made using the following fonts: Alata (https://fonts.google.com/specimen/Alata) Archivo (https://fonts.google.com/specimen/Archivo) Fonts & colors used #041e52 #ffffff #073899 #4f7bc8 #80b2f1 #f4f6f8 #dbe8f1
  • 13. Create your Story with our illustrated concepts. Choose the style you like the most, edit its colors, pick the background and layers you want to show and bring them to life with the animator panel! It will boost your presentation. Check out how it works. Storyset Pana Amico Bro Rafiki Cuate
  • 14. You can easily resize these resources without losing quality. To change the color, just ungroup the resource and click on the object you want to change. Then, click on the paint bucket and select the color you want. Group the resource again when you’re done. You can also look for more infographics on Slidesgo. Use our editable graphic resources...
  • 17. JANUARY FEBRUARY MARCH APRIL MAY JUNE PHASE 1 PHASE 2 Task 1 Task 2 Task 1 Task 2 JANUARY FEBRUARY MARCH APRIL PHASE 1 Task 1 Task 2
  • 20. You can resize these icons without losing quality. You can change the stroke and fill color; just select the icon and click on the paint bucket/pen. In Google Slides, you can also use Flaticon’s extension, allowing you to customize and add even more icons. ...and our sets of editable icons
  • 23. Help & Support Icons Avatar Icons
  • 24. Creative Process Icons Performing Arts Icons