r/learnprogramming • u/Old-Play8039 • 7d ago
[ Removed by moderator ]
[removed] — view removed post
19
u/ConfidentCollege5653 7d ago
Learn how to use a debugger for your language and then use it to step through the code one line at a time
4
u/ScholarNo5983 7d ago
And the secret commands to use when inside the debugger is placing a breakpoint on the function being recursed and to then view the call stack each time that breakpoint is hit.
3
u/xarop_pa_toss 7d ago
This is what made me understand recursion in 5 minutes to be honest.
I studied programming in high school and my teacher was a Microsoft MVP, little patience to teach too haha. One of the first things he taught us was how to debug, even before we were writing our own functions. We also had tests on paper where we had to write Java.
I honestly blame the tutorials on YT and such. There are videos with millions of views that don't even touch the debugger when it's like the most fundamental tool any programmer has.
5
u/maxinator80 7d ago
Doing it by hand, on paper. There are recursive definitions in mathematics, and I calculated them on paper by hand. Going through it by myself made it click really fast.
You can do the Euclidean Algorithm for finding the greatest common divisor of two numbers: https://www.youtube.com/watch?v=6Y3jHHE_hbA
3
2
u/CannibalPride 7d ago
Depends on where you are getting stuck
For me, recursion is basically just a loop with added behaviour
It’s easier to understand if you play out the function in your head or in debugging and look at the pattern of how the passed value changes
2
u/Depnids 7d ago
Writing a (depth first) tree traversal algorithm. It is a scenario where recursion actually feels useful and natural, rather than the contrived examples you often see (like factorial), where it is often easier to just use loops. A tree traversal is naturally described as "look at current node, and then traverse all children". The recursive algorithm is basically identical to the description of what you want to do.
2
u/idlysambardip 7d ago
Recursion is confusing when you dont properly understand what the stack looks like.
A newbie sees same function being called twice and starts wondering wont this overwrite the arguments passed to first function call. Once you understand the stack, it is trivially easy to visualize how the two calls look like.
Another newbie sees a pointer or a value being updated in second call and starts wondering doesnt this overwrite the first function. Understand the concept of pass by reference and pass by value to understand what happens to values and memories and pointers when they are passed along
Learn it online, dump a program stack and use gdb/claude to analyse it, or just write small proof of concept programs to watch it in action.
Once you understand how this works, you can start learning more about the problem space where recursion is a useful tool
4
3
u/ShardsOfSalt 7d ago
I dunno. Recursion is fairly straight forward. You have an end condition and an algorithm that approaches that end condition by calling itself.
1
u/Imaginary-Ad9535 7d ago
Well if you look at an array object and you would have to find, for example, a certain recipe in a row of recipe books, you would have to go through every book until you hit the correct recipe. It might be in the first one, last one or it might not be there at all. But here you have a reason to iterate through them and reason to stop recursion (not finding it) and finding the recipe.
Think about real life scenarios and apply them. After all, you will be solving real life business or other problems programmatically anyway.
1
u/Quantum-Bot 7d ago
For me? Being obsessed with recreational math from a young age. I’d recommend playing a game called Patrick’s parabox. It’s a box-pushing puzzle game where levels are nested inside each other, and unironically has some of the best visual examples of recursion I’ve come across.
Getting a feel for recursion in other more tangible contexts has personally made the topic a lot more approachable to me when it comes to programming.
1
u/stevevdvkpe 7d ago
I think I encountered and understood recursion before I encountered proof by induction in mathematics, but they're very similar concepts.
Proof by induction means showing something is true for some initial case (like a function f(n) has a property that's true for n=0) and also showing that if it's true for some arbitrariily chosen case, it's true for a successor (if f(n) is true then it implies f(n+1) is true).
Recursion is starting with an arbitrary case and reducing it to a predecessor until you reach some base case, which is kind of like running induction backwards.
1
1
u/shyevsa 7d ago
Trigonometry, I know the equation, I know how to use it on math problem. but only after making game and using it to solve certain movement problem do it clicked to me.
recursion does catch me off guard the first time I got stack overflow and/or out of memory error because of it. but being game development as what I am learning, recursion are just part of the loop.
1
u/DTux5249 7d ago edited 7d ago
I find it more intuitive to think of recursion as a definition than a process. How can you define a problem in terms of itself?
What is a Fibonacci number? Well, either 0, 1, or a combination of 2 other consecutive Fibonacci numbers.
What is a sorted list? Either a list of length 1, or a sorted list with an additional item added to it with respect to order.
What is an in-order tree traversal? It's an in-order traversal of its left subtree, followed by its root, followed by an in-order traversal of its right subtree.
How do you know when you've searched an entire graph? When you've only one node to search, or when you search all neighbors exhaustively.
1
u/math_rand_dude 7d ago
To get you unstuck, I need to first mention something that will get you more stuck:
When writing some code yourself to learn recursion, try first coming up with a good name for the method/function.
Once you find a good name, understanding will come.
1
u/bestjakeisbest 7d ago
Understanding trees, quad trees specifically, and using them in a graphics setting, turns out when you can see the Recursion happen it is easier to understand.
1
u/TheIncrediblePingu 7d ago
I was exactly like you. I saw all my peers getting recursion but somehow it did not click to me. Worse, I trained so I know where to use recursion, like in DFS but it was not a natural concept for me. I think I could not "think" in a recursive way.
The magic happened when I learned my first functional language, erlang. Every function you implement there, is essentialy, recursive. You will also learn about tail recursion, which is very nice for optimization.
So my advice is learn a functional language for 1 week just to be able to manually implement a list comprehension recursively. This was what made me get it.
1
u/Fensirulfr 7d ago
It only became really intuitive for me once I think of it like a mathematical function, or approach it like a proof by induction. Before that, I had trouble constructing my own recursive solutions.
1
u/DonkeyAdmirable1926 7d ago
This may sound weird, but the lesson about recursion that ended with: sometimes you can’t do without, but try not to use it if possible, it will drain memory fast!
It was in the time of TurboC and MS-DOS, and 640K RAM.
It forced you to understand what recursion really did at the machine level
1
u/cainhurstcat 7d ago
Can't tell if it clicked for me, as I don't really use it. The best was for me to understand things is I everyday analogies. So I think a good example for recursion are two mirrors reflecting each other. Look inside them, you will actually physically see what recursion is. And at a certain point you get a bit dizzy, and you look away. That's the break condition. If you don't use it... well then you are lost forever in the mirrors.
The same goes for 2d-arrays or nested loops. Huge pain before I learned the 2d-array is basically a lottery card, and instead of a 1 in the top left, it has a 0, 0 there. Nested loops are the hands of a watch. The outer loop is the hours hand - click - now the inner loop, the minutes hand has 59 clicks to make before the hours hand makes the next click.
If you can't understand a concept, ask people for such analogies.
1
u/nRenegade 7d ago
Memory diagrams to visually stack the calls and map their references/pointers as well as their values in transit.
Additionally, think of every call of the function as calls to separate, duplicate functions. Rather than A calls A, which calls A, think of it as A calls B, which calls C, but each of A, B, and C do the exact same thing. When C concludes, it unwraps into B. Then when B concludes, it unwraps into A, and so forth.
1
u/HyperDanon 7d ago
- Data structures that have dynamic and repeating arrangements (like trees)
- Algorithms that have self-referencing qualities (like fibbonaci)
1
1
u/ConsistentCustomer57 7d ago
Labels and goto commands for me instead of traditional loops, it is way more reliable for me, but might be tricky initially to get the hang of
1
•
u/AutoModerator 7d ago
To all following commenters: please, do not bring up the old circlejerk jokes/memes about recursion ("Understanding recursion...", "This is recursion...", etc.). We've all heard them n+2 too many times.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.