Curious about void vs string

So, on taking the challenge myself the first time - when splitting off GetGuess, I used void rather than string - which, actually worked.

I’m curious as to why we shouldn’t use void when setting up the GetGuess function - what am I missing here?

1 Like

returning a string allows you to do this

std::string Guess = GetGuess();

If the function was void you wouldn’t be able to initialise or assign a variable like that.

Ahh, alright - so if I’m understanding correctly:

  • Using string opens greater options of using the function later down the line
  • Such as assigning a shorter cleaner variable within another function
  • Whereas, using void - I would not be able to assign a different variable to it later on

Forgive me if I’m mixing terminology here - Its been quite a long day.

You give it a return type if you want the function to give back anything to the caller (), in this instance we would want to give back the guess so it can then be stored in a variable or passed into another function

int main()
{
    ProcessString(GetGuess());
}


void ProcessString(std::string Guess)
{
    //does something but doesn't give anything back
}
2 Likes

Thanks Dan!

Came on here to ask the exact same question, thanks for the replies.

Privacy & Terms