-2

So I've been following some tutorials on Udemy for C++ and so far it's been incredibly informative and clear however I'm a little stumped by how a for function is calling another function.

So my first function is;

EDIT I've included use namespace std so being the String rather than std::

string getGuess()
{
    cout << "Enter your guess" << endl;
    Guess = "";
    getLine(cin,Guess);
    cout << "Your guess was: " << Guess << endl; 
    return Guess;
}

And then to iterate through this function 5 times so they can guess 5 times I'm writing

void play()
{
    constexpr int guessTimes = 5;
    for(int i = 0; i<=guessTimes ; i++)
    {
        getGuess();
    }
}

Now this all makes sense to me, however the next part of the tutorial is abstracting the;

cout << "Your guess was: " << Guess << endl; 

The for function is then rewritten as so;

void play()
{
    constexpr int guessTimes = 5;
    for(int i = 0; i<=guessTimes ; i++)
    {
        string Guess = getGuess();
        cout << "Your guess was: " << Guess << endl;
        cout << endl;
    }
}

I don't quite understand how it's now running the getGuess function as it's now applied to a new String variable which in itself I find quite strange, some clarification of how the fundamentals of this process works would be highly appreciated.

Munerz
  • 1,052
  • 1
  • 13
  • 26
  • 2
    `i<=guessTimes` this will iterate 6 times not 5 – Killzone Kid Feb 08 '18 at 16:25
  • 1
    What is `String`? There is a `std::string` in C++, but it is not `String`. – PaulMcKenzie Feb 08 '18 at 16:25
  • 1
    `int guessTimes = 5` declares an `int`, and initialises it with an expression which has the value 5. `String Guess = getGuess();` declares a `String` and initialises it with an expression that has a value determined by running the function `getGuess` and seeing what is `return`ed – Caleth Feb 08 '18 at 16:27
  • @PaulMcKenzie Udemy is a "teaching website", OP probably isn't exposed to C++ unadulterated by "simplifications" at this point – Caleth Feb 08 '18 at 16:29
  • `[teach-me]` You need to understand what `return` means in C++. Also, please use the right terminology: " it's now applied to a new String variable" should rather be "the value returned by getGuess() is *assigned* to a new string variable". "Abstracting the line" should probably be "moving the line". –  Feb 08 '18 at 16:29
  • @Arkadiy no, the new String variable is *initialised* with the return value of `getGuess()`. There is no use of `String & operator=(const String &)` here – Caleth Feb 08 '18 at 16:30
  • @Caleth - agreed, I was just trying to simplify for the OP. –  Feb 08 '18 at 16:31

1 Answers1

2

You are probably overthinking it. There's really not much to explain.

Do you find this strange?

string x = "hello";

Do you find this strange?

string y = x;

Then you shouldn't find this strange

string z = f();

"hello", x and f() are all examples of expressions. Expressions are evaluated to produce values. f() is an expression that calls the function f. The return value of the function f is the value of the expression. One of the things you can do with a value is assign it to another variable.

john
  • 85,011
  • 4
  • 57
  • 81