9 – Function Signatures 1

What is a function declaration, and why is it important? A function declaration has multiple pieces and this is a little easier to look at in code. Maybe let’s look at your tests normalize function. When you look at just the way the function is defined, there’s the bool and then the tests normalize and the open paren and the close paren. And that bool says that it’s going to return true or false. Very similar to Python, you can return whatever you want, except in C++ you have to tell it the type you’re going to return. It has some advantages. For example in Python, if you accidentally returned just the row, instead of the whole grid, it wouldn’t tell you, you wouldn’t know until it ran, and there was an error. Maybe three hours into what you were doing, it would suddenly get to a place in the code where there was a bug and you would now. But C++, it tells you right away, you can’t even compile if you didn’t return something and return something of the right variety. Something I do in Python a lot is actually forget the return statement, entirely. And then I am working with a none type object forever. And that’s actually can be a really hard bug to debug. So I can see how this might be valuable. Exactly. You’re at least guaranteed that the parts fit together correctly. Not necessarily that they do what they’re supposed to, but that they fit. Right, the Lego blocks go together, and that you didn’t forget one. Okay. It’s so easy to forget the return or to return just the wrong thing, because you were thinking about something else. Hopefully, C++ has a few safety checks for you.

%d 블로거가 이것을 좋아합니다: