Simplifying with Functions

Okay, I just made it through lecture 17: Simplifying with Functions. Now, I’m a bit confused & I don’t feel like the content was explained well enough as to what it is we were actually doing.

Was the PrintIntro & GetGuessAndPrintBack both functions that we created? If so, why did we put at the beginning of our code: void PrintIntro(); & string GetGuessAndPrintBack();?

I was under the impression that putting the word “string” before something meant we were defining a string. In this case, it seems we are using “string” to define a function?

Yes, those are functions we created. Putting those lines of code at the begining declares those functions so the rest of the file can use them as without it the compiler won’t know of their existence until it gets to them (alternatively you could just have the definitions at the top). That it will give neccessary information to be able to use it (what it returns and any inputs it takes).

That’s because of the (), that means you declaring/defining a function without any parameters and the “string” at the begining is the return type.

I figured the “string” at the beginning was because of the return type.
However, maybe I don’t fully understand what “return” means because the
return type for PrintIntro is void. So I guess I’m completely confused.

It’s what is given back to whatever called that function and void mean it doesn’t return a value. So for a code example

int SquareInput(int i)
{
    return i * i;
}

int main()
{
    int a = SquareInput(4); //a is the result of SquareInput(4) (i.e. 16)
}

That red colour is hard to read :confused:

Okay, you’re example has me even more confused. The comment didn’t make it
very clear, either. I’m just confused a bit on exactly what is happening in
that code.

So I have edited the code to hopefully make more sense. I defined a function to take an integer and square it (incorrectly called it double before) and return the result. In that code example 4 will enter the function and get squared thus 16 will be returned to the caller, effectively making that statement

int a = 16;

Great answer from @DanM, so I may be just adding to the confusion with this;

C++ is a strongly typed language, which basically means that absolutely everything has to have a type. A type can be an integer (int), string, array, etc. This also means that any function you create has to have a type, which is the return type. Think of this that whatever your function does, it has to return something - as you might recognise from the popular Hello World program, even main() returns something (the operating system takes the return value of 0 to mean that everything has been executed correctly):

int main() { // we declare the function main, with the return type int
// code that runs the program
return 0; // we return an integer as we said we would
} 

The way to let the computer know that nothing is to be returned from the function, is to use the type void. So when you declared your void function() and string function2() respectively, you told your computer that function() would not return anything, and that function2() would return a string. However, if function2() does not return a string - but, say, an integer - the compiler will complain and not let you compile the program.

And don’t worry, you’ll get the hang of this a little further down the road :slight_smile:

Those are both great answers & do help a bit. But I guess what I really
need to know is what exactly does “return” mean? Like so what is the string
being returned in GetGuessAndPrintBack? I would understand if it was the
guess, however, I thought the idea of the game was to have multiple
guesses. So what string is being returned in that function?

Like I said previously void functions don’t return a value. A return statement within a void function just exits the function at that point.

As Ben explains in the video for lecture 17, he wants you to return even if the return type for the function is void, just to get you into the habit of returning (and not forgetting to return). If you return from a void function, basically it does nothing - as the program would return from the function itself, and keep running through the rest of the code anyway. I hope that makes sense, if not, just holler and I’ll try to explain it a little better :slight_smile:
Now, for the string GetGuessAndPrintBack() function, it should look like this (and the comments are mine to try to explain the behaviour;

string GetGuessAndPrintBack() { // we declare the function with string as a return type
cout << "Enter your guess: "; // print instructions to the screen
string Guess = " "; // declare a string variable and initialize it
getline(cin, Guess); // grab whatever is input on the console and store it in Guess
cout << "Your guess was " << Guess << endl; // print whatever was stored in Guess back to the user
return Guess; // return the contents of the Guess variable
}

In the current code, the function could just as well have been a void function that didn’t return anything. However, if we want to use this information later in the program, we need to return it to the program somehow. For example, we could store it in a variable called LastGuess, to check if the user is repeating himself. Then we’d do something like

string LastGuess = GetGuessAndPrintBack();
Here, we declare the string variable LastGuess, and initialize it to the return value of GetGuessAndPrintBack().
You could then do
cout << LastGuess << endl
and the output would be whatever you had in the line
cout << "Your guess was " << Guess << endl;
inside the GetGuessAndPrintBack() function.

Keep asking if it’s still a mystery, I hope this helps a little though :slight_smile:

Privacy & Terms