Page 9 of 10

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

Posted: Thu Dec 07, 2017 02:22
by Saladin
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.

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

Posted: Thu Dec 07, 2017 03:51
by kob
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.

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

Posted: Thu Dec 07, 2017 04:25
by Saladin
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.

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

Posted: Wed Jan 31, 2018 19:09
by kob
hey saladin could you help me understand dynamic binding more? i've been to the lecture, done the lab work and talked to my professor, but i don't really get the usefulness of it.

for example in our assignment the user is supposed to select 3 different translation options (texting, formal, and one we decide - i chose spanish). so e.g. a user wants to text someone "be right back" and then the program should spit out "brb", or if they chose formal it'll spit out "Be right back." etc. we're supposed to use a doubly linked list of base class pointers that point to the type of translation they want. so i'm guessing we'd have an abstract base class called Language or whatever and then derived classes called Texting, Formal and Spanish.

alright so i get the basic idea. you have a class hierarchy and in the base class you declare a function virtual which lets you use a base class pointer to access derived class functions through that pointer. but why is this useful? to my understanding the pointer needs to know what it's pointing to to call the right function e.g.

Language *ptr = new Texting

so now if i call ptr->fn() it'll call Texting's version of fn(). why not just create a Texting object and then say object->fn()? if i have to declare what class the pointer points to what's the advantage?

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

Posted: Wed Jan 31, 2018 22:53
by Saladin
You're at the juncture in CS where you're being taught advanced things for advanced things sake.

I would say this *specific* thing doesn't matter. In fact, you may never even use C++ again in your entire career.

But, programming frequently does get weird and abstract like this under certain circumstances. Occasionally, you'll be interfacing with systems that need to be extremely flexible and generic because you'll have limited or no control over the source material or how people call your code.

I know that's not super encouraging, I'm basically telling you there's not necessarily a good reason to do what you're doing.

But you will occasionally run into problems in which you'll have to think like this, so I recommend soldiering through it, and not sweating the details.

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

Posted: Thu Feb 01, 2018 01:07
by kob
that makes sense. my professor really hammered home how useful and powerful it is, but i can't wrap my mind around how it's useful. i guess it reduces code bulk somehow? maybe so you only have to write a function once and you can just pass in the ptr and then use downcasting to find out what type it points to and go from there?

i dunno, maybe i have to fiddle around with it to grasp it.

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

Posted: Thu Feb 01, 2018 02:34
by Saladin
The idea is supposed to be that you massively reduce the amount of grunt work you have to do in large solutions because everything is made up on the fly, the computer is doing the work for you.

This is amazing in theory, but what tends to happen in practice is that it has too limited of an application, so it has to be extended anyway. Or it's so complicated that no one understands and uses it correctly, so it's underutilized. Etc etc.

CS professors appreciate elegant and useful code. But most commercial code is strung together garbage that was built on a strict budget and didn't have the luxury of a limited goal, focused planning, and lots of dev time.

There's a divide between computer science and software engineering. The former is pure math, theory and excellence. The latter is just trying to get shit done quickly and effectively. Those two things are at odds a lot more than you'd think.

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

Posted: Thu Feb 01, 2018 04:51
by Mr. Smith
Work is getting more stressful. Drawing labs still fucking sucks and makes me nauseous. lol

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

Posted: Thu Feb 01, 2018 08:18
by Saladin
Drawing labs? Is that like processing blood samples and such?

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

Posted: Thu Feb 08, 2018 04:10
by Mr. Smith
Saladin wrote:
Thu Feb 01, 2018 08:18
Drawing labs? Is that like processing blood samples and such?
Yes, I fucking hate needles.

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

Posted: Fri Feb 09, 2018 17:48
by Saladin
Damn, it still makes you squeamish even to be on the other side of it huh?

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

Posted: Sun Feb 18, 2018 18:47
by Mr. Smith
Saladin wrote:
Fri Feb 09, 2018 17:48
Damn, it still makes you squeamish even to be on the other side of it huh?
It does. Old people have the worst veins, lol.

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

Posted: Mon Feb 19, 2018 01:19
by Saladin
Yeah, I don't think I'd have the stomach to be anywhere in the medical profession. Even the simple stuff seems like it wears you down.

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

Posted: Tue Feb 20, 2018 03:56
by kob
same

amazing pay and (sometimes) hours though. my sister is a nurse and works 3 12s. would kill for those hours

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

Posted: Tue Feb 20, 2018 20:43
by Saladin
Pfft. I dunno about that. Maybe 3 12's at my software job, sure.

But a 12 hour shift on your feet? Caring for sick or injured people? All of whom are stressed out or scared? Some of whom might be combative or trying to scam the place for opiates?

That sounds fucking rough. Although I suppose you at least do get a long weekend to recuperate.