danger/u/
This thread is permanently archived
I can't wrap my head around recursive functions

| And it makes me very angery. I'm trying to write a function that would solve the Towers of Hanoi puzzle, outputting which disk it moves from where to where each turn. I've seen the solution, I kinda get how it's supposed to work, but the function calling itself in its body is like magic to me. Like, how does it continue executing if it calls itself, wouldn't it just be stuck in a loop?


| Wait. Actually. Does a function that is called recursively stack up calling itself until it reaches the condition and only then start executing the other code in itself in appropriate order?


| Usually there's a condition in the function somewhere that determines when it stops calling itself recursively yes.

Your first call to that recursive function is essentially the last to finish executing. Every call after that is put on the stack and executed from the most recent call to the very first. Does this make sense?


| >>83093
This post is correct. The functions calls itself as deep in levels as till the condition is met. Then the result of the calculations are returned in reverse order from the deeper levels and upwards.


| >>83093
Yeah, sort of. It certainly takes some getting used to, even though I did have that little revelation after venting. I still can't say I'm okay with using recursive functions (especially when the solution for Towers of Hanoi requires to call the function recursively in 2 places, shifting arguments around at the sime time), but I at least don't desperately stuck anymore.


| *don't feel desperately stuck anymore.


| >>83171
If you ever need help with concepts or paradigms, just reach out to this board and I'll jump in to help as soon as I can.

If you ever need a second set of eyes on your code as well, or ears to discuss code and problems, just let me know.

As you can see, you explaining what you knew about recursive functions helped you. Talking it out is a big way to help us learn programming. Keep up the good work man. Would love to see your program when you're "done" with it, aka runs:)


| OP I know the lads above me gave some great answers but what really helped me with learning recursion is this link, I suggest you read it to fully understand (the author goes through step by step at the end of the page) http://blog.moertel.com/posts/2013-05-11-recursive-to-iterative.html


| >>83266
There isn't really much to see there, it's pretty much just a little assignment from the course I'm taking. I'll still post the code as soon as I get back to my PC I guess.

I'll surely visit this board in particular more in the future.

Thanks a lot.


| >>83112
Unless it's a tail recursion and your language supports tail recursion optimization in which case next function call will replace current one. But that's some deep stuff.


| In recursive methods, there are two parts. A recursive case, and a base case. A recursive case is usually what activates when you know you're on the last iteration of your method, to avoid an infinite loop. The base case is what's going to handle the rest of the work, and to make your method recursive it must call itself on the process. IIRC there are three pillars, so moving one disk at a time should simply need an if/else if/else section to suffice.


| I think you got the names of the cases mixed up


| Okay, so, this was my solution in PHP. It's perhaps not perfect, but it kinda works
<?php
function Tower($height, $start, $finish, $temp){
//condition
if ($height != 0) {
//from start to temp
Tower($height-1, $start, $temp, $finish);
//moving the disc
echo "Move disk number $height from $start pole to $finish pole<br />";
//from temp to finish
Tower($height-1, $temp, $finish, $start);
}
}

Tower(5, "first", "second", "third");
?>


| ^
OP btw


| >>84580

Welp, now that you mention it I think I did.


| did you mean to do it like this, op? I get a weird jumbled output and I'm not sure if your code fully works yet. Also, what are you trying to do? https://pastebin.com/9Q9E3XJ8


| >>86174
Nope! It's important to have the function accept the arguments, because when it recursively calls itself we swap them around. Then it will output the correct sequence of moves to solve Tower of Hanoi.
I'll try putting it into pastebin, I guess, because it kinda breaks here.


| >>86174
https://pastebin.com/hMNkzRFq
This is how I did it, and it does indeed work. I guess I could put it on a free hosting to actually show it off, but I'm not that sure and/or confident in myself.


| Uhh, I think I forgot a line break there somewhere, but it doesn't matter much.


| Take a look on program languages with the declarative paradigm, like prolog. Recursive functions are usually much more easy and efficent in this kind of environment.


| >>86444
ah I see I was way off hahaha

I would still use global for those variables though since you may be creating a new location in memory for each call, especially when you swap them around. I also do not recommend making more than one recursive call in a recursive function because that is definitely using 2x as much memory to perform that function as it would normally use. I don't know much about how php allocates memory I could be wrong but I'm certain 2 calls is no good


| >>86609
Yeah, using globals is probably not that bad of and idea. I thought it might break a little, but prolly not. I'll try it tomorrow.
And I'm pretty sure the second recursive call shouldn't cause many issues (at least not at this scale), as it usually executes and exits pretty much as soon as it's called. But regardless I can't really come up with an algorithm that would require only one recursive call. I barely came up with this, and even that required quite some help, haha.


| >>86681
it really isn't that big of a deal for your situation I agree. Honestly you can just stick with what you have and it'll work but in my scenario I worked php fullstack for ~1 year and I constantly had to think about memory allocation. If I had a function taking up 3x as much memory or just more resources than normal, it usually multiplied like that for 1000+ users. You'd be surprised how much an entire site can slow down because of it.


| >>86806
Yeah, no, I do understand that completely. I guess I just need more practice to come up with better solutions.


| >>86857
Yeah well if you're trying to get into php fullstack sure (which I don't recommend; I quit :P) but as long as your code works, it works.


| >>87592
I've heard a lot of people say that they don't recommend it. But I'd say I'm more interested in backend than fullstack, though that probably matters little, realistically speaking. Most importantly though, PHP gives the most job offers around here out of any web-related language and/or service.

Total number of posts: 26, last modified on: Fri Jan 1 00:00:00 1514505294

This thread is permanently archived