What is the difference? Please see attachment

Summary
I originally created int variable and not constexpr. And also I did my for loop inside the string GetGuessAndPrintBack() function.

Question:

  1. is there any difference between using int and constexpr inside the function?
  2. Is there any difference to use for loop inside string GetGuessAndPrintBack() function and not in void main()?

Hi Brave.Nick!
Yes there is a difference between int and constexpr! A huge difference.
when the compiler reads:
int NumberOfGuesses = 5;
it reserves a place in memory just big enough for one int and stores 5 in it. It also has the setting that this can be overwritten at any time by the program.
When the compiler reads:
constexpr int NumberOfGuesses = 5;
it also reserves a place in memory just big enough for one int and stores 5 in it. However, the difference is it has a setting that says: this memory cannot be overwritten or changed, and cannot take a new value until the program exits. Only then is the memory freed up for other use.

As for your second question, at this stage in the program there is not difference between putting the for loop in the function or in main. However structurally there is a big difference, which I suspect will become more important later on.

Privacy & Terms