Mastery 21. Use of recursion for repetitive algorithms… Mastery 22. When to use what type of repetition in a program… And here my video:
Hello this is #Mastery21 about using recursion in algorithms. A recursion refers to the action of when a function calls itself. It is kind of like creating a loop.
96
Normal
0
21
false
false
false
ES-TRAD
X-NONE
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:”Tabla normal”;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:””;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:12.0pt;
font-family:Calibri;
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-fareast-language:EN-US;}
Why? Calling the function inside the function creates a series of events which lead to the result that is the same using a lop with while or for.
- In recursion, a function calls itself but you shouldn’t assume these two functions are same function.
- Local variables are defined inside a function and has scope only inside that function. In recursion, a function call itself.
Recursion makes it easier to express ideas in which the result of the recursive needs to complete the task.
WATCH MY VIDEO https://youtu.be/i9wo3N8tNzk
Here are masteries 17 and 21. Actually easy for me. I learned how to use “switch” to do this mastery and it was easy to use and useful for me. Enjoy: https://www.youtube.com/watch?v=zr2WxayvjoI
http://www.python-course.eu/recursive_functions.php
Use of recursion for repetitive algorithms
On this mastery I will show you what is recursion in C++ and how to use it.
Recursion is a form where a function calls itself. When you call the same function, you are doing a loop, and you have to give a condition in order to make this loop stop.
Let’s create the same program we did on mastery 20 but now using recursion and a function.
So, do as follows:
- Create a function of type long (this allows us to have bigger outputs of the numbers) and add a n variable to it.
- Create an if and else if condition as we learn on masteries 15 & 16
- If n equals to 0, factorial = 1. (That’s a rule)
- Else if, n multiplied by factorial of (n-1). [This is the recursion part of the program, we are calling the function inside the function n times and subtracting 1 every time the loop is done, and it will stop until n equals to 0.]
- Inside int main (), declare a variable and ask the user for it, then simply call the function.
Your program should look something like this:
Now let’s test the program. Factorial of 5 should be equal to 120.
And that’s about it, you just learned how to use recursion with a function, congrats!
Hey, here is the video: https://youtu.be/3k9CgjcuUwg
Thanks for watching ! n.n
(Credit of the image goes to https://flic.kr/p/4AvpZH) Mastery #21 is about using recursion and here is a Power Point explaining what is recursion and how to use it. I hope you find it useful!!! MASTERY 21
Mastery #21 is about using recursion and here is a Power Point explaining what is recursion and how to use it.
I hope you find it useful!!!
MASTERY 21