Store result of a function in a variable

Hello, I started the c++ course a few days ago, and in the section 2 in “Clarity is Worth Fighting For” at 8:37 (link here: https://www.gamedev.tv/courses/635496/lectures/11794349), he says that we can store the result of GetGuess() in Guess by doing this:

Guess = GetGuess();

What I don’t understand is how does it works ?
If the function is this:

string GetGuess()
{
	string Guessinside = "";
	// get a guess from the player
	cout << "Enter your guess: ";
	getline(cin, Guessinside);
	return Guessinside;
}

And the return value is GetGuess, shoudln’t we just write Guess = GetGuess; ? :

void PlayGame()
{
	string Guess = "";
	Guess = GetGuess;
}

Is it simply that it has been decided that way, or there is a reason of using the () ?

Second question, if we store GetGuess() in a variable: Guess = GetGuess();, why is it called at the same time ? Shoudln’t we call the function first and then store it ?

GetGuess();
Guess = GetGuess;

Thanks in advance…

No, because that would be trying to initialise Guess to the function GetGuess.Whereas adding the () makes it a function call so you would be initialising it to what the function returns. Hopefully that answers your second question too.

I see, thanks for your reply.
It was confusing at first when I do Guess = GetGuess(); because like you said, Guess would take what the function returns, but it also runs it to “know” what it will return. So if the function has cout in it, the console will return the cout when I initialise Guess.

I programmed a little a few years ago in php and I don’t remember that. Anyway thanks, I advanced a little more in the course and things are starting to get clearer :grinning:

cout isn’t returned, no. Only the value in the return statement is returned.

If it isn’t returned, how is it called ? We can see that the cout in the function is printed.

When a variable takes the value of a function returns, why does the console write what is inside this function ?

Here is an example of what I’m confused about:

#include <iostream>
#include <string>

std::string TestString();

int main()
{
	std::string NewString = TestString();
	std::cout << "Content of variable NewString is : " << NewString << std::endl;
	return 0;
}

std::string TestString()
{
	std::cout << "Test (in function)\n";
	return "Test";
}

When I initialize NewString and define it with the function TestString(), why does the console write “Test (in function)” ? Shouldn’t the NewString just “take” the information of what TestString() returns, which is “Test”, and only when I write std::cout << NewString; should the console print “Test” ?

Here is the result of that code:

Thank you for your help and patience DanM ^^

Because the function is called. Only what is in the return statement is returned from it. Everything happens sequentially so the code you have above, is equivalent to the following.

int main()
{
	std::cout << "Test (in function)\n";
	std::string NewString = "Test";
	std::cout << "Content of variable NewString is : " << NewString << std::endl;
	return 0;
}

Ah yes I see, when I initialise a variable to a function, not only I call the function which will do whatever is inside it, but the variable will also get the value of whatever the function returns. One line does two things and not only one (without counting what is inside the function).

Thank you very much :slight_smile:

1 Like

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

Privacy & Terms