Return value and what the OS is doing to it

Hey, I don’t have a piece of code that isn’t working but rather I don’t know why a particular piece of code is in this tutorial. So he puts the return value of Get guess to guess, but I am not sure whats happening with that value, because when you get line are you not already setting the string? so why do you need to return the value? If someone could help explain why we need to return that value rather then leaving it blank , I will appreciate it.

Could you please post a piece of code you refer to?

string GetGuess()
{
	cout << "Please enter your guess: ";
	getline(cin, Guess);
	return Guess;
}

You have missed a part of code here:
string Guess = ""
image

If you could please copy line by line next time, so people who do not have access to videos
could also try help you.

The string Guess = "" declared a local variable.
Local variables are created on a stack, and will be destroyed when they go out of scope.
In this case, the local variable Guess would be destroyed right after function execution
terminates, so in order for you not to loose it’s value, you want to return it’s value from the
function.
So basically you get a line of text in this function, and then return a copy of it by saying
return Guess;

Ah sorry about that , the only reason why i didn’t include that part of the code was because i moved it out of the function as have got my variables outside functions for a larger scope. I am wondering if because thats the case do I still need the return Guess if it’s already accessible outside the function?

Oh, in that case you don’t need to return it, right

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms