Hello coders,
I have a final tomorrow, and no matter how many tutorials I have read, I cannot figure out recursive functions. What a nightmare. Can anyone give me some insight? - in plain language, please! I don't understand any of the following:
-how to return the number of digits in a passed in function
-how to return the first digit of a positive passed in number
-how to return the sum of the digits in a passed in number
Those are basic ones I should start with. I understand the basecase part of the function, but I can't figure out the rest of it. I keep wanting to put in a loop, but the whole point is to NOT use a loop, right? I'm stumped. Please help me. I don't want to fail tomorrow. :(
How do you figure out what to put in the second part of the function (after the basecase)?
In my problem, for example, I'm having trouble mainly with the return statement. Why is that working? Lol. I don't get it. I understand why I did what I did in the else clause, but I had to play around with the return statement a bit until it worked....also, for some reason, when I run this program, it works, but the output file is showing something different. Why would it do that?
This increments "n" to '1' at each pass and totals them because you require the formula to execute before returning. Since it adds the '1' to the returned value every time it executes it essentially counts how many times the function was called.
So does it call it again when the outfile statement executes? Why does my initial call return 5, but the outfile returns 9? I've never had the outfile not match the cout....weird. How do I fix that?