{d} returns 1, which puts 1 into {c} where x is 2 and so returns 2, which can now put this into {b} where x is 3 and returns 6, which can now be put into {a} where x is 4 which returns 24, which can now return to the original factorialFinder and finally multiply the original 5 by 24, returning 120. Hope that makes sense.
I don't get this. Where is factorialFinder getting the final answer from? I can see that its multiplying 5 by 4 becuase of (x-1). BUT how does it know to multiply 20 by 3 and then 60 by 2? How is it storing the answer from before and multiplying it by the new one?
@artashes999 There are certain algorithms that takes you 3 lines of code using recursion and 200 lines of code using iteration. You'll meet them someday and you'll thank Bucky for this tutorial.
@ryangr0 Yes, this exact case would be easier using a loop.There are things tho that cant be made with regular loops.Say the user has to enter a number x and a number n and the program has to compute x^n (x*x*x n times).If you tried to do it with a loop like
for (int i=1; i<=n;i++) {
x=x*x } .Lets say x is 3 and n is 2.
1 is less than 2, so now x=3*3 (9)
2 is equal to 2, so now x(which is 9)=x*x, where both of them are 9 and you get 81 as a result.This can be done with recursion though.
Yup I'm confused. I thought I understood this but I guess not. In this function, first of all it has two return statements. So when we hit the return x*factorialFinder(x-1) statement, isn't that going to send a value back to the function call statement? And let's say it doesn't, because it clearly isn't doing that. It runs through the function 5 times (for a call of 5) and then it gets down to x == 1 being true so it returns 1. Why isn't this just sending 1 back to the function call statement?
@Chriscs7 lol. The reason why it's easy is because of bucky's explanation. Some teachers actually teach the wrong way and start with odd examples like fibonacci sequence etc.
how do i call a class member inside a class member? such as: Sexy::Gorrila(){ cout<<"omgwtfbbq"<<endl; Sexy::Kitten();//part i have a question about } Sexy::Kitten(){ cout<<"High Five BREH!"<<endl; Sexy::Gorrila();//part i have a question about } ...and i want it to basically say: omgwtfbbq High Five BREH! omgwtfbbq High Five BREH! omgwtfbbq High Five BREH! ...and so on
@SchizoFilms Basically, I think (don't kill me if I'm wrong >_<) recursion means that the fuction will keep on running and running until your computer's memory is used up, and then it will result to a "Segmentation Fault".
@spidey678 Recursion means, that within a method/function, you call the same method/function (or one that calls the first one) to accomplish a backwards loop. If done right, it'll do anything you could do with a While or For loop, but backwards. If done wrong, it'll do anything you could do with a While(true) loop.
But since it calls itself within itself, I made the inception joke.
@SchizoFilms you're not alone! You don't need to use recursive functions, as other programmers say, it's memory hog and nightmare to debug. So avoid using it ;P
That was the most fun I've had doing a Bucky tutorial! I set mine up to say "5*4*3*2*1 aka 5! = 120". And had the same format work for whatever number I put! Microsoft here I come.... after several years of more learning.
@cm999 It's not really the size of the program that determines when it's a good idea to use recursion. Recursion can actually improve the efficiency of your program and the time it takes to write that program if used in the proper circumstances. That is to say, there are certain algorithms that can be improved by using recursion, and there are certain algorithms that will not be improved.
All recursive functions can be written as an iterative function so you never HAVE to write recursive functions but it sometimes makes the code much more easy to write and read. The factorial function can easily be written using a loop. If you want to count the words in all files inside some directory it is very intuitive to use a recursive algorithm.
@freako9595 User interfaces are very hard to create. You typically have to pick a language, an SDK, a UI Kit, and then design from there. They're usually not very cross-platform and those that are tend to suck.
That's why Bucky is presenting all these basic concepts using the built-in console. And every operating system will have a (very) different console, with very different features and presentation.
if(factorialUnderstanding == 0){cout << "FML" << endl;}
KinzokuKoibito 1 day ago in playlist C++ Programming Tutorials Playlist
I learned how to do this in my brain before I knew what it was... sweet brag.
Mooska 5 days ago in playlist C++ Programming Tutorials Playlist
look at the call stack debug window and step through the code. that will help you see how the compiler interprets this code.
surferdudevideo 6 days ago
i try and explain this to you:
factorial(5) returns 5*factorial(4)
factorial(4) returns 4*factorial(3)
factorial(3) returns 3*factorial(2)
factorial(2) returns 2*factorial(1)
factorial(1) returns 1
and then it goes backwards to top like this 1*2*3*4*5 = 120 :) hope you understood this guys .
p.s. thumbs up for Bucky :D
raizen991ra 1 week ago
inception++?
AJJSDN 1 week ago
INCEPTION: C++ Edition.
Rmoore08 1 week ago
What is this program name? Is it Dev C++?
theubunny 2 weeks ago
@theubunny if you look at the first video of this tutorial series, you'll see this is Code::Blocks
darkness7479 1 week ago in playlist C++ Programming Tutorials Playlist
@theubunny codeblocks
skeletondude1234 1 day ago in playlist C++ Programming Tutorials Playlist
factorialFinder(0);
XKillerBoy 2 weeks ago
Lets write it in other way:
int fact(int x){ return (x<=1?1:x*fact(x-1));
}
cachwahed 2 weeks ago in playlist C++ Programming Tutorials Playlist
hi there sir bucky, i'm a computer science student from the Philippines and I thank you for making these tutorials. ^_^
gheen123 1 month ago
bucky, if i graduate i'm sending you my first salary. you sir, rule :)
muresandani 1 month ago 2
@johnthesman
factorialFinder(5){a}
{a}return x*factorialFinder(4){b};
{b}return x*factorialFinder(3){c};
{c}return x*factorialFinder(2){d};
{d}return x*factorialFinder(1);
{d} returns 1, which puts 1 into {c} where x is 2 and so returns 2, which can now put this into {b} where x is 3 and returns 6, which can now be put into {a} where x is 4 which returns 24, which can now return to the original factorialFinder and finally multiply the original 5 by 24, returning 120. Hope that makes sense.
MrJepcats 1 month ago
I don't get this. Where is factorialFinder getting the final answer from? I can see that its multiplying 5 by 4 becuase of (x-1). BUT how does it know to multiply 20 by 3 and then 60 by 2? How is it storing the answer from before and multiplying it by the new one?
johnthesman 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
No, this couldn't be possible. I've understood recursion in 3 minutes, while was not able to in a semester...No, really, you are a PRO.
r00tring 1 month ago
Your tutorials are great, crystal clear, keep the good work.
davidsfc9 1 month ago
Oh my god... what the fuck, barbecue?
TheXRealXBrapp 1 month ago in playlist Buckys C++ Programming Tutorials Playlist
yeah... can't i just use a for loop and times the integer by a decrementing value?
artashes999 2 months ago
@artashes999 There are certain algorithms that takes you 3 lines of code using recursion and 200 lines of code using iteration. You'll meet them someday and you'll thank Bucky for this tutorial.
LaughingShinoo 2 months ago
FACT : No math skills , ==> No C++ Skills .
the First Programming language that you should start with is Mathematics .
Mr1naruto 2 months ago 2
a function within a function,
FUNCEPTION
honeypot11 2 months ago
Thanks!!
rotaaag 2 months ago
Thought about it for 5 minutes then got it.
Aaronster8 2 months ago
Wouldn't this be way easier just using a for loop?
ryangr0 3 months ago 4
@ryangr0 Yes, this exact case would be easier using a loop.There are things tho that cant be made with regular loops.Say the user has to enter a number x and a number n and the program has to compute x^n (x*x*x n times).If you tried to do it with a loop like
for (int i=1; i<=n;i++) {
x=x*x } .Lets say x is 3 and n is 2.
1 is less than 2, so now x=3*3 (9)
2 is equal to 2, so now x(which is 9)=x*x, where both of them are 9 and you get 81 as a result.This can be done with recursion though.
wh33l0fd00m 2 weeks ago
@ryangr0 ye but not half as sexy
chewtaggeer 1 week ago in playlist c++
wtf did i just watch?
t4nk0rn0t 3 months ago
@t4nk0rn0t Apparently a programming tutorial? Didn't you even see the title? :P
Rigardoful 3 months ago
Is there a reason why you would use this and not a While or For loop?
NorthboundFox 3 months ago in playlist TheNewBoston - C++
1st watch = wait what
2nd watch = nope
3 watch = FUUUUUUUUUUUUUUU
gabrielusa911 3 months ago
i hate recursionnn!
bondservant4Him 3 months ago
Is it wrong that I'm 12?
Dombamatic 4 months ago
brain... explosion..
onelerv1 4 months ago in playlist Buckys C++ Programming Tutorials Playlist
BUCKY PLEASE TEACH ME MATLAB! IT STINKS! thank you..
lissau3 4 months ago
Comment removed
shadowfrost91 4 months ago
oh my god. w. t. f. barbecue.
threeclock 5 months ago
Linux doesn't crash... it goes and then says 'Segmentation Fault' No ending process or nothing... Linux > Windows alll day!
The206referee 5 months ago
Comment removed
MrStealYourMom 5 months ago
run bucky, run!
jakewebbing 5 months ago
OMG! thats how they made The Matrix XD
TheF0rthHokage 5 months ago
Its a waving flag!! :D
gofish8195 6 months ago
Well explained! Could u please show how to program the divide-and-conquer algorithm on a game plan. It also uses recursion!
Johannes9482 6 months ago
I watch this video for no-reason. I don't wanna learn C++; I wanna learn C. But anyways thank you for all your tutorials! Keep up great work!
steff385 6 months ago
OMGWTFBBQTACOSAUCEWALL
lol
Muckpuff 6 months ago
Yup I'm confused. I thought I understood this but I guess not. In this function, first of all it has two return statements. So when we hit the return x*factorialFinder(x-1) statement, isn't that going to send a value back to the function call statement? And let's say it doesn't, because it clearly isn't doing that. It runs through the function 5 times (for a call of 5) and then it gets down to x == 1 being true so it returns 1. Why isn't this just sending 1 back to the function call statement?
ResidentBiscuit 6 months ago
@ResidentBiscuit
I have the exact same question...
x34chaa 5 months ago
@x34chaa Me too. And I'll add to that the question: Where is the result stored between the recursions? Clearly "x" cannot store it.
patlecat 5 months ago
is there a way to count how much time it ran the function before it crashed? lol
DrQu4ntum 7 months ago
For an explaination of recurrsion see the bottom of this comment...
For an explaination of recurrison see the top of this comment...
mgstanger 7 months ago 51
@mgstanger Haha :P
very clever
lamalas60 7 months ago
What IDE is he using?
Mewigi 7 months ago
@Mewigi CodeBlocks
thesickbeat 6 months ago
i love recursion but they are realy hard to debug ^_^
Swamp1191 7 months ago
Watermelon Hahaha!
eminemchu43 7 months ago
now try to explain recursive maze solving algorithm. Damn yea.
shebotnov 8 months ago
if i want to calculate factorials of numbers from 1 to 16 everything is alright
but if i go higher, it gets buggy
it shows me negative numbers or just 0
how can i fix that?
JakobRobert00 8 months ago in playlist Buckys C++ Programming Tutorials
@JakobRobert00 Try using double instead of int, if you want bigger numbers. Full output of double you will get using this line in main function:
cout << fixed << fact(170);
Full function:
double fact (int x) { if (x == 1) return 1; else return x*fact(x-1);
}
In the other hand, with double you won't be able to find factorials of numbers bigger than 170, because you would get 'inf' text. :)
nWANTED 7 months ago
This is easy.I want to learn game programming
Chriscs7 8 months ago
@Chriscs7
then you should learn something like allegro, dark gdk, opengl or directx
linkinl1 8 months ago
@linkinl1 Thanks :)
Chriscs7 8 months ago
@Chriscs7 lol. The reason why it's easy is because of bucky's explanation. Some teachers actually teach the wrong way and start with odd examples like fibonacci sequence etc.
willzurmacht 8 months ago
@willzurmacht Actually I knew this before xD,but bucky explains this good also.
Chriscs7 8 months ago
This has been flagged as spam show
paligamy93 8 months ago
Couldn't you just use a for loop with a negative step?
TyrianSword 8 months ago
@TyrianSword
for ( int x = 5; x > 1 ; x-- )
spidey678 8 months ago
@TyrianSword In the case of looping backwards through a string, but there are times when that does the same thing as a positive increment case loop.
SchizoFilms 8 months ago
A method within a method.
RECURSION
Three years of Computer Science, learning Java, and I still don't fully grasp recursion. (And I still completely hate it)
SchizoFilms 8 months ago
@SchizoFilms Basically, I think (don't kill me if I'm wrong >_<) recursion means that the fuction will keep on running and running until your computer's memory is used up, and then it will result to a "Segmentation Fault".
spidey678 8 months ago
@spidey678 Recursion means, that within a method/function, you call the same method/function (or one that calls the first one) to accomplish a backwards loop. If done right, it'll do anything you could do with a While or For loop, but backwards. If done wrong, it'll do anything you could do with a While(true) loop.
But since it calls itself within itself, I made the inception joke.
SchizoFilms 8 months ago
@SchizoFilms you're not alone! You don't need to use recursive functions, as other programmers say, it's memory hog and nightmare to debug. So avoid using it ;P
willzurmacht 8 months ago
@willzurmacht Of course I'm not alone. Everyone hates recursion. But, for some reason, every programming curriculum ever covers it.
SchizoFilms 8 months ago
This has been flagged as spam show
F A C T O C E P T I O N
noobenlol 8 months ago
what if you pass in -1??
Randude14 9 months ago
@Randude14 Program will crash
kaktusas86 9 months ago
That was the most fun I've had doing a Bucky tutorial! I set mine up to say "5*4*3*2*1 aka 5! = 120". And had the same format work for whatever number I put! Microsoft here I come.... after several years of more learning.
DjZephy 9 months ago
Remember LIFO (last-in, first-out) and you'll understand recursion.
23rdpsalm 9 months ago
bucky i ♥ u :)
mamutualex3xxx 9 months ago
Oh NVM, ignore my last comment.
Factorial is pretty easy, i've never done it before, but is just simple multiplication :)
I get this now, thanks :D
subs2meplease 9 months ago in playlist C++ Tutorial
This is the first tutorial of yours that I don't really get so far :(
Does anyone know of any other tutorials that explain Recursion (ppossibly without confusing math) ?
subs2meplease 9 months ago in playlist C++ Tutorial
WHATS GOING ON GUYS
robot or automatic repeat at beginning of your tuts:P?
mrbeantje1 10 months ago
someone didn't understand it. LOLThat thing looked awesome!
PoizonFart 10 months ago
i semi-understand this... wtf
MrHankkey93 10 months ago
MY BRAIN IS MELTING........
retromelon123 10 months ago
OMG WTF BBQ
imDan11l 10 months ago
please CONTINUE the tutorials !!!
wonderbug10 10 months ago
Most computer science students like recursion.. idk what bucky is talking about.
xFayte 10 months ago
@xFayte Well with regard to coding most dont as it reduces the efficiency of your program, depending on the size of your program.
cm999 10 months ago
@cm999 It's not really the size of the program that determines when it's a good idea to use recursion. Recursion can actually improve the efficiency of your program and the time it takes to write that program if used in the proper circumstances. That is to say, there are certain algorithms that can be improved by using recursion, and there are certain algorithms that will not be improved.
xFayte 10 months ago
r u ever going to continue this c++ series ?????
rgrainger09 10 months ago
@rgrainger09 he only posted this yesterday???
jony1710 10 months ago
@jony1710 i know but he coverd all this 2 years ago...
rgrainger09 10 months ago
So could you use recursion in the main function? Call the main function again?
mangopearandapples 10 months ago
i dunno, recursion sounds kinda awesome, its tricky
tuskan1386 10 months ago
Wouldn't an iterative solution be faster?
Turbolord 10 months ago
Where could I download tis c++ he is using?
tibiaowned1 10 months ago
@tibiaowned1 Google "code::blocks". It's an IDE and it also comes with a compiler.
Turbolord 10 months ago
@Turbolord he isn't using code::blocks ...
tibiaowned1 10 months ago
@tibiaowned1 yes he is
Phl3xable 10 months ago
@tibiaowned1
Do you mean the IDE?
DeadSteal 10 months ago
@DeadSteal codeblcok found it thx anyway
tibiaowned1 10 months ago
@DeadSteal idk i mean everything so i could start programming like him :D
tibiaowned1 6 months ago
All recursive functions can be written as an iterative function so you never HAVE to write recursive functions but it sometimes makes the code much more easy to write and read. The factorial function can easily be written using a loop. If you want to count the words in all files inside some directory it is very intuitive to use a recursive algorithm.
Peterolen 10 months ago
is this tuts going to tackle gui's???
MrJutein 10 months ago
is there a benefit to that against having a while loop do the factorial?
wolterh6 10 months ago
I don't see why can't you just use a while/for/ statment.
Cezarijus 10 months ago
I love recursion.
I use it whenever I wouldn't need an incrimenter.
andrewyaoauatauabaea 10 months ago
@freako9595 User interfaces are very hard to create. You typically have to pick a language, an SDK, a UI Kit, and then design from there. They're usually not very cross-platform and those that are tend to suck.
That's why Bucky is presenting all these basic concepts using the built-in console. And every operating system will have a (very) different console, with very different features and presentation.
w0mblemania 10 months ago
oddly enough i loved recursion back in school, found it easy but tricky at first
gonzo191 10 months ago
@freako9595 That requires much more code. Complicated if your a beginner.
thesmartdjmarsh 10 months ago
1:23 Soooo funny! :D
DaftPVF 10 months ago
I think we need to go deeper
eugeene42 10 months ago
Compilers can compile themselves.
daviddalbylive 10 months ago
So you are going to do assembly programing tutorials after this, right!?!?!??!??!? :P
SDTricker 10 months ago
I love recursions!!! They make things so much easier!
audiobahn404 10 months ago
zomg, i'm studying computer science for 2 years now... and this still confuses me oO
But this is SOO well explained!
This is the perfect way to make a sudoku-solver!
XRaYNL 10 months ago
newBoston, have you watched my video that i pmmed you..it is in mychannel
HardCoreProgrammer1 10 months ago
ok i didnt get that at all!
RaamAnkka 10 months ago
it's like a dream within a dream.
and the base case is "kick" otherwise ur program is in limbo state until you kill it.
toughlightyear 10 months ago 60
@toughlightyear wtf are u smoking
Streetdisciple27 10 months ago
@Streetdisciple27 It's a movie reference.
MakingGamesIsFun 10 months ago
@toughlightyear INCEPTION! MUAHAHAHAH!
tuntuni1000 10 months ago
@toughlightyear yeah my teacher said that movie reminded her of recursion
AlexanderLee1 9 months ago
What!?!?!?!
MaracTheCracker 10 months ago
thats cool
gtman2500 10 months ago
Im going to be doing Comp Sci next year and i find recursion very fun.
SackSean 10 months ago
Thank you Bucky.
faiyaz786khan 10 months ago 41
first comment 83
Xchaz730X 10 months ago