The organization of my code

I’m currently on Lecture 27, and everything is going great. Until I started to wonder, how the heck my code is working. I feel like it’s out of order, and I’m not sure why it plays the way it does.

My cpp code.

This is my first time using Gist and I didn’t copy over my header or another .cpp I have so it probably doesn’t work, but I’m mostly confused just by the order in which the code is set up, but why it still plays out in the correct order.

So at this point, I start up the program.
It welcomes with the intro.
It asks for your guess.
It repeats back your guess.

And it is specifically here I get confused, because the order in my code is showing PlayGame with the output for "Your guess was: " as being immediately after the intro, but instead the string GetGuess outputs.

To clarify, it LOOKS to me as if the game should:
Welcome with the intro
"Your guess was: "
Ask for your guess

But it works as it’s supposed to, which is why I am so confused.

I’ve pretty much been following along and trying to internalize as much as I can remember, but when I took a second look, I’ve simply gone and confused myself. Figured it’d be better to figure it out now though, haha.

Thank you!

On that line 49, you can see it has the GetGuess() at the end. That’s telling the code at that point to ignore whatever else is next, and go off and do whatever GetGuess() does.

Even though the functions are ordered the way you say it should work, by referencing/calling them at that point, it interrupts what’s happening to perform the function.

I think I understand. So when we get to the line

std::string Guess = GetGuess();

You’re basically telling it “In order to get Guess, go do GetGuess()”?

Thanks a lot for the help, I really appreciate it.

That’s exactly right :smiley:

If for example, you put PrintIntro(); on the line after that, it would go off and print out your introduction again, before resuming with showing the guess value.

Very cool! I definitely get it now. I made a little test just to see what would happen, and it worked!
Testing Order of Functions

Thank you for your help! Out of curiosity, if you should happen to know or have the time - Something that we’ve been doing in the BullCowGame that I did replicate in my test was putting Void Functions at the beginning. I tested if I could remove those and still run the code, but it won’t allow it to.

Is it because we have to define the functions in the beginning first, before using them in the program?

Thanks again!

Yep, you still need that there, as it tells the compiler that it should keep an eye out for those void functions later in the code. When you get to the bit where that stuff is split into a separate header file, it’ll become more obvious.

I can’t remember if it was this course that used the phrase or I’m remembering something else, but think of those early declarations as a contract with the compiler - you’re telling it to keep an eye out for these things so you can call them from inside your main and other places.

You are awesome, thank you so much for taking the time to help me out. :slight_smile:

I think I’m going to take this time in the course to go back and review.
Now that I’ve started to become more familiar with C++, I think looking
back at some of the intro topics will help me a lot.

Thanks again, truly.

The compiler needs to know that the function exists to be able to use it and the only information it needs for that is the return type, name, and paramater types (if any). If during the linking process it can’t find the definition of it then you would get a linker error like this

Which in human terms, “you used an undefined function Print() which is void and has no parameters, in the function main, in main.cpp”

I suggest you read through this explanation of the C++ compilation process for better understanding

Privacy & Terms