Using void to return guess

Not sure if this is covered in a later article, but I used void GetGuess to create my function in this case. Is there a deeper need to use string GetGuess in this case? Does string perhaps retain it’s returned variable whereas void, by definition returns nothing?
P.S. void worked in this instance.

Like you said, a return type of void returns nothing back to whatever called the function.
Whereas a return type of string returns a string back to the caller, which you can then use later in the code
however you wish.

I have the exact same thing. And although dedday explained it (thank you, by the way), I still don’t get why I cannot use void. It works both ways, so void is return something, and not just “0”, which I find strange. Someone that can elaborate on dedday’s explanation?

A void function returns no value to the code that called it. If you try to return something, the compiler might not issue an error (preventing successful compilation), but it should at least issue a warning (and compile anyway). If the caller code actually tries to use a value “returned” by the void function, then the compiler should issue an error, because it does not really return anything.

I had done the same exact thing, using void GetGuess() instead of string, and was so confused as to why it still worked and why it mattered. Thanks for clarifying!

Thank you all for the explanation. I think I get it. So I did void as well and it worked but the only reason it worked is because we are not using the value later on/anywhere else in the code?

But if we were to use it later for something then we would have gotten an error right away?

Thanks again all! SUPER new to this. Hella green :slight_smile:

It may be getting a little ahead, but i kept the return method as string and put the cout response back in main…

You have to assign the return value to a string so I added another string to main. I suspect it will be needed in later lessons and as a parameter in other functions.

Privacy & Terms