Both defining and running "string Guess"

Okay! So I’ve been away for a little while and trying to get into this again. Going through the code, the first thing that got me is “String Guess = GetGuess();” in the “void PlayGame()” function.

My mind was asking: when are we actually asking for the guess now?

So my understanding is that we are simultaneously defining “string Guess” as “GetGuess();”, AND running "GetGuess();

Is this correct?

I am also unsure about return functions. The last line in the “void PrintIntro()”-function is "return;"
Is this just telling it to go back to the “int main” and continue to the next line of code?

Then why isn’t there a “return;” in “void PlayGame()”-function? Does it have something to do with the fact that the next line of code in “int main” is “return 0;” ?

And while we’re at it, what does “return Guess;” at the end of “string Getguess()” do?

To your first point, yes. You’re declaring a variable that is immediately equal to the result of the GetGuess() function. Declaring and defining on separate lines would work as well.

To your question about returns, they aren’t needed in void functions since there’s nothing to return. Sometimes the return statement is used inside a void to end the function immediately, though if there isn’t a return inside a void it goes top to bottom and then finishes. I can’t personally speak to why there was a return in PrintIntro(), I haven’t looked at the code in a while

Finally, when you return Guess, you’re making the function stop and Guess be the result/return of that function for anything that needs to use it. Any function that’s not void needs to return a value of the type it’s defined as.

Thanks for the reply!

When I read that, I remembered seeing that void functions require no parameters and returns nothing :slight_smile:

When you say:

“when you return Guess, you’re making the function stop and Guess be the result/return of that function for anything that needs to use it.”

I’m thinking that the function that needs to use it is:

"cout << “Your guess was " << Guess << endl;”

which of course comes right after :slight_smile:

So what is it called, where this return is put and available for use?

I’m not sure there’s a specific term for it other than calling a return function. You’re just setting a variable to a value, which happens to be the return value of the function called :slight_smile:

Thanks for your help :slight_smile: I will meditate on this :slight_smile:

Void functions don’t need return at the end, so the one that you mentioned in your original post in “void PlayGame()” is completely redundant and doesn’t do anything.
However the tutor in the video said at one point that he likes to put a return at the end of every function whether they need it or not, just to get into the habit of always putting a return. The advantage being that you will end up having a few unnecessary returns instead of sometimes forgetting to put a return where you should have.

The return at the end of main() is special - if main() returns a value of 0 after completing it tells your computer that the program executed until the end. Otherwise, it knows something went wrong.

1 Like

Privacy & Terms