C++ Programming Tutorials - 32 - C++ Recursion and Recursive Functions - Eric Liang

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what is up guys my name is Erick and in today's episode I'm gonna be teaching you about a recursion or also known as creating a recursive function in C++ let's do this so what is recursion anyways recursion is the concept of having a function call itself so for instance if we were to create a void function for example void hello and then inside the hello function we would see out hello world and then if we were to call that function inside our main function compile and run this program it would display hello world however this is not recursion in order to make the function hello a recursive function we have to make it call itself so in order to do that underneath the C outline we would type in hello again so what happens here is when the program runs it would run the main function and it runs the hello function through this statement right here so once it goes to the hello function its C outs hello road once it's the outside world it would call itself again which makes it go back up here and then C out hello world and then basically it'll go on in an infinite endless cycle and tell your computer runs out of memory which crashes the program now in order to make recursive functions truly useful you have to define a stopping point so in order to do that let's use an example that involves math now in math there is something called a factorial now this example is widely used whenever discussing the recursive function topic in case you don't remember how to do factorials in math or probably haven't learned them so basically in order to find the factorial of a value it's basically like this so in for instance if you were asked to simplify or find a value for 5 factorial factorial is represented by the exclamation mark after the number that you are trying to find a factorial of it basically means 5 factorial equals 5 times 4 times 3 times 2 times 1 and then equals and then you laughs and by doing the math you get 120 so if we're to do three factorial then you would do 3 times 2 times 1 which equals to 6 however in the case of zero factorial it equals 1 that's just a special scenario and then when it's 1 factorial well it's simply 1 okay so keeping this in mind let's create a factorial function so for this entire program for the entire lesson we're going to focus on creating a factorial calculator so I'm just gonna leave these 4 lines here and comment them out just for your reference so to create a factorial function let's create int factorial and then curly brackets now remember this is going to be a recursive function inside the parameters of your factorial function we're going to have it accept an integer of num or if we want to make it math friendly let's call it into X so inside our main function let's see out factorial and then let's say 5 factorial in line that way when the main function calls the factorial function it would see out the return value from our integer data type factorial function okay so in order to create a factorial function calculator learn how to somehow create a formula that will allow us to decrement the values every time the factorial calls itself and returns a value so I can multiply by the previous value so to do this we're gonna take advantage of the if-else statement that we've learned a while back so if else and then else do this okay so what are we going to put in the parentheses well we're gonna check for a 1 case so if X is not equal to 1 then I want you to return the value of x times the factorial of X minus 1 so this is what will allow us to do the multiplication with the incoming number times it by the decremented value and then otherwise if the value is one we wanted to stop so in that case we'll return the value of one because if we don't do this return value of one it'll keep decrementing all the way down to zero and that will just make our entire answer zero which is incorrect okay so if we're the compiler around this program just to test it out we should see 120 as a result and as you can see in the console window the answer is 120 perfect okay so if we were to change this value from a 5 to let's say 20 we could solve the value of 20 factorial so hit the compile and run this program let's see what we will get we would get a negative value which is incorrect the reason why this is happening is because of the integer datatype the integer datatype can only store so much values before it hits a certain limit and your computer's memory so in that case 20 is way too big of a number however one way to solve the issue is to change the into a double so let's say double and then if we're to compile and run this program again we should see the result and as you can see it shows 2.4 3 2 9 e times 10 to the 18 or something whatever it represents which is correct because anything that is a factorial should result in a positive value if you get a negative value or a zero then you're probably did something wrong in your code or it the computer's memory ran out of space for that data type in that case you would want to create something like a double or perhaps a long data type so it could store more values with that in mind thank you for watching today's tutorial on how to create recursive functions in C++ and if you liked this video be sure to hit that thumbs up button and if you have any questions leave it in the comments below as usual and I'll see you next time [Music]
Info
Channel: Eric Liang
Views: 2,046
Rating: 5 out of 5
Keywords: c++, c++ for beginners, C++ programming tutorials, C++ programming tutorials for beginners, C++ spoonfed, C++ tutorial, c++ recursion, recursion, recursive functions, c++ recursive functions, how to create recursive functions in c++, c++ visual studio, learn c++ programming, how to code in c++, eric liang, c++ tutorial for beginners, c++ programming tutorial, recursive function, c++ programming video tutorials for beginners complete series
Id: t3J445uVgeA
Channel Id: undefined
Length: 6min 39sec (399 seconds)
Published: Tue Aug 29 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.