Hey what's up Dawg? (Generic Chat Thread)

General discussion.
User avatar
Saladin
Spider Mastermind
Posts: 8367
Joined: Mon Aug 11, 2003 22:39
Consoles: PS4 PS3 Wii Wii-U PC
Location: DANCE MASTAH!
Contact:

Re: Hey what's up Dawg? (Generic Chat Thread)

Post by Saladin » Thu Dec 07, 2017 02:22

Oh yeah, I see the issue here. So, you have to remember that return only returns the value *to the function that called it*. Not all the way up the call stack.

Think of it like a dinner plate handoff down a line of people. One person passes it to the next, then to the next, one at a time, until the front person sets it on the table. The person at the end can't just pass it to the person at the front.

So, in your original code, you call the recursive stuff, but it never matters, because it only returns that value *to that function*, which you don't set to anything, so it just throws it away! It doesn't pass it along, it passes *its own value*.

For example, if someone were to call your function, they might say...

int myvar = find(a, b);

Right?

But what if they didn't set it to anything? They just put.

find(a, b);

What would be the result? Sure, the function runs, but nothing recorded its value. So what's the point?

The *same thing* happens in your first code.

You call find(index, gcs); , but you don't *set* it to anything, which means that when you do index = 2 * index + 1; and then find(index, gcs); ... you might as well have not even written "find(index, gcs);" because nothing recorded it! You could have written anything, like index = index, and it would have the same effect.

It *does* do the recursion, which I'm sure you saw as you stepped through it, but when it returned the value lower down, the upper functions didn't grab the value, so they just continued on.

So it steps through and everyone returns their own node's value *to no one*.

The one thing I don't understand is what foundIndex is. Is it a global variable? It should be a super weird value if that's the case, and not 0.

But if it were locally defined, and it *started* at zero, then it makes perfect sense your function always returns 0.

A simple run-through:

Start function

Is left child null? No, so let's go left.

....does a shitload of recursion, all irrelevant because no value is recorded....

Ok, done going left, what's next? Well, we're done with this if statement, what's after that?

Oh, return foundIndex, which, in this context, is zero! Wheeeee

Hopefully that made sense. Let me know if you need it explained more clearly.

The key here is that return foundIndex at the end *combined* with the fact that you call find(index, gcs);

If it makes it easier to understand, you could also have said. int foundthething = find(index, gcs); And thennn return foundthething;

return find(index, gcs); is just a shorter, snippier way of writing the same thing.
Quack wrote:ok but so now what do I do besides masturbate

User avatar
kob
CyberDemon
Posts: 12768
Joined: Sun Nov 14, 2004 14:40
Consoles: PS3 - PC
Location: get slam'd
Contact:

Re: Hey what's up Dawg? (Generic Chat Thread)

Post by kob » Thu Dec 07, 2017 03:51

that makes a lot of sense thank you. i guess my expectation was that once the recursive call was done x amount of times, the return at the end of the function would then return it all the way back up, but i can see now why that doesn't work. it'll probably take more attempts at recursion for it to truly stick in my head. like it makes sense now but when you start programming what you learned goes out the window sometimes until you do it enough.

and yeah foundIndex is a local variable, although i realize now that i'm not initializing it to 0 anymore. it used to be until we started messing with the function and then i just forgot to add it back. glad you caught it haha.

User avatar
Saladin
Spider Mastermind
Posts: 8367
Joined: Mon Aug 11, 2003 22:39
Consoles: PS4 PS3 Wii Wii-U PC
Location: DANCE MASTAH!
Contact:

Re: Hey what's up Dawg? (Generic Chat Thread)

Post by Saladin » Thu Dec 07, 2017 04:25

There's just a lot of tiny details that can really screw you over. I cannot tell you how many times I've fucked something up, and a really crazy bug happens, and the core reason was an incredibly minor thing like that. As you program more and more, that'll happen less.

But yeah, that's why it helps to have that low level understanding. It makes it easier to catch why something went wrong. If you go too high level, you'll keep looking for logic errors when the problem is typically simpler than that.
Quack wrote:ok but so now what do I do besides masturbate

Post Reply

Who is online

Users browsing this forum: Exabot [Bot] and 19 guests