r/programminghelp • u/Akllira • Sep 06 '22
C++ [C++] How do you use recursive functions in programs? Any advice?
I've been trying to use recursive in my project, but I have no idea how could I include it on an app (?)
I don't have a code since I don't even know how to start. I've tried with addition and subtraction, things like that. But, for example, an app where you have to login, etc. it just feels like it doesn't fit in.
So, that's why I was asking for an advice. ;;
2
u/lcc0612 Sep 06 '22
Recursion is a way of thinking about certain problems that can make them easier to solve, but ultimately it's just a tool, and you need to use the right tool for the job.
For an analogy: Think of it like it's a hammer, great for solving specific problems, but if you want to build an entire house, you're gonna need more than a hammer. Instead of going to every part of your house and hitting everything with your hammer to see what happens, look for specific problems that can be solved with it, and then go from there.
Assuming that your school is competent, some part of your assignment would have been designed such that recursion is applicable. Alternatively, if this was a fully free-form project, then you'd have to come up with a problem that recursion can solve. The latter is of course a little more complex, since you'll need to be able to recognize such problems. Like /u/Goobyalus said, such problems would typically contain repeated steps that "look like itself".
If you want to practice recursion more, you may want to consider watching How to Reason about Recursion. Full disclosure, I made this video so this is self-promotion (sorry), but I do think it's applicable here. While the video is geared more towards using recursion to problems you already have, hopefully it can help you identify relevant patterns so that you can come up with a problem you can solve with recursion.
1
1
u/gmes78 Sep 06 '22
But, for example, an app where you have to login, etc. it just feels like it doesn't fit in.
That's because it doesn't.
Recursive functions are of limited usefulness. They're useful for doing stuff like iterating a directory tree, but not much else.
1
u/Akllira Sep 06 '22
Yeees, I was thinking the same, but my teacher is asking for recursive in my app ;;
2
u/xlanor Sep 08 '22
In theory - and I strongly don’t recommend this but it seems like your assignment requires recursion in some form to be included in a web application, you probably could use this in a number of places.
Based on the example you gave, signing up could have a password ruleset, where the password has to be a certain length. You could slot recursion in here to count the length of the string, even though it’s incredibly inefficient
Pseudocode (might be off by a bit, gist is there)
func length(password) if password is empty return 0 return 1+ length(password[1:])
1
u/TheSkewsMe Sep 06 '22
Recursion uses more stack space than loops leading to overflow errors if not properly implemented.
3
u/Goobyalus Sep 06 '22
Are you asking how to shoehorn recursion into something as an exercise, or are you asking where recursion is useful?
Recursion is useful when a problem or structure can be broken down into smaller versions of itself.
Recursion can also be used to replace loops.
Lots of data structures are defined recursively, like trees and linked lists.
For computation, recursion is typically more useful for thinking about a problem than for production implementation. E.g. when we use "dynamic programming" to solve a problem, the top-down recursive approach has the same class of complexity as the bottom-up iterative solution, but the iterative solution will have better constants.
Modern compilers will do tail call optimization to turn tail-recursive functions back into loops under the hood for efficiency.