Incrementing variables

In lesson 13 for Triple X, we cover incrementing variables like: ++MyVariable;

I didn’t touch upon this in the lesson, but you can also increment like: MyVariable++;

What can you find out about the difference between these two? Here’s a good source of info for you: https://en.cppreference.com/w/cpp/language/operator_incdec

Post below!

The difference is to do with at what point the value is incremented, pre (++MyVariable) and post (MyVariable++).

If we take the following examples:

x = 10
a = ++x

With the pre incremental being used the value of “a” would be 11 and the value of “x” would also be 11.

However if we use the post incremental as follows:

x = 10
a = x++

Would result in “a” being 10 and “x” being 11 because the value of “x” is assigned to “a” first and the increment of “x” occurs.

I found the following website explained it best:

2 Likes

Hi, I still don’t understand how the integer variable LevelDifficulty that’s being declared and initialized in main is linked to the integer variable Difficulty that’s being used in PrintIntroduction and PlayGame. Thanks!

When you pass it as an argument to the function, the function gets a copy of the value which is then used inside that function.

But “LevelDifficulty” and “Difficulty” are two different names. How does the program know that i’m reffering to “LevelDifficulty” when i put “Difficulty” in the parameter of the function?

The program only considers what is in a particular scope, for example:

void PrintDifficulty(int Difficulty)
{
    // Once here, this scope knows nothing about the variable "LevelDifficulty"
    std::cout << Difficulty;
}

int main()
{
    int LevelDifficulty = 2;
    int OtherDifficulty = 3;
    // This scope knows nothing about the variable "Difficulty"
    PrintDifficulty(LevelDifficulty);
    // When calling the function, you provide the value of "LevelDifficulty" 
    // In the PrintDifficulty function, it is called "Difficulty" because that is
    // how it was named in the argument list of the function.
    PrintDifficulty(OtherDifficulty);
    // Now, you've provided a different value from a different variable, but
    // in the function, it is still called "Difficulty"
}

There are some exceptions to this, for example, if your variables were in the global scope, then you could also use them in the function, but that is not a good way to do it. Function calls are maintained on what is called the “stack”, when you move into another scope, your computer creates a new memory space (called a stack frame) for working with the variables in that scope. Once you leave the function, the memory space is deleted from the stack. If you’re using Visual Studio, you can put a break point in your function and you will be able to see the stack and you can analyze the contents of variables in each:

Note here I have double clicked on the call stack and it’s showing me the stack frame UNDER the break point frame, here there are two variables:


When I go to the stack frame at the top of the stack, there is only one variable:

When I click continue I will end up in the same place, but a new stack frame with the OtherDifficulty value instead:

If I had a break point in the main between the two function calls, you would see that the stack would be only one high again, before entering the function a second time, creating a new frame at the top of the stack again.

Note also the line listed in the screenshots for the main function stack, once you return from the function the program will continue execution at the line listed there, so in the first two screenshots returning will continue execution on line 14, in the next one, once I return from the function the program will continue on line 15.

2 Likes

It is kinda of weird, I kinda get it but at the same time I’m still struggling to properly word it in my notes. From what I understand the reason it works is

  1. You declare the parameter where you declare the function so you have basically a local variable (like a = 1) in there, however you’ve not declared it with a integer which needs to be done where the function is called rather than declared, thus at

void PrintIntroduction(int Difficulty) you are declaring Difficulty , then

PrintIntroduction(Difficulty);

you are initialising it with a value (I mean my way of describing it would probably give most people with more experience a heart attack but I’m trying to make it make sense to me as well at this point :stuck_out_tongue: ), thus why it could be set to 7 in the lecture.

However as we need it to be LevelDifficulty we then need to Declare the parameter in PlayGame (where we declare the function) as we can then initialise the parameter where it is called in the main function which is also where our LevelDifficulty is! this means instead of initialising it as a random digit we can initialise it to be whatever LevelDifficulty is.

I mean I don’t know if that helped or honestly if it’s even 100% right, and I definitely could do with it being cleared up exactly why it is that we declare the parameter where we declare the function then “initialise” it where the function is called.

1 Like

From my understanding is that there is Prefix increments/decrements and postfix increments/decrements.

The prefix increments and decrements either increase or decrease before the line of code is fully executed.

The postfix increments and decrements either increase or decrease after the line of code is fully executed.

++a Before operator has good work ethic and increment right away by one

a++ After operator will be lazy and procrastinate and increment afterwards by one.

Can someone explain to me how the program knows to increment the level difficulty only when the answer is right.

I know that getting the right answers returns a boolean value of “true”

but what part of the code tells the program to increment the int value of level ONLY when the bool value returned from PlayGame is “true” and not “false”?

There’s so much that I don’t understand. I got confused as to why we are putting the “while” statement in the main function. Why can it be true if we shouldn’t hardcode “if” statements as true? Maybe I need a human explanation of the previous video, because I don’t understand where we are now.

Another thing is: Why are we working in the main function now instead of the PlayGame funciton. This seems redundant; we could have just left everything in the main function, no?

From my understanding, we’re working in the main function now because it’s purpose is to just get the game started. We could leave everything in the main function, but I think it would get confusing and hard to read. I don’t think you would want to many things happening in you’re main function, you want all the details to be divided into other small functions designated to do it’s on specific thing. If this program wasn’t so complex, everything would have probably been left in the main function.
Also, the “while” statement being hardcoded to true is for demonstration purposes just to get the concept of when a while loop will continue to execute. I think later on, we’ll end up actually putting a variable there.

LevelDifficulty exists in memory in the program main. When the call is made to PrintIntroduction, a copy of the value of LevelDifficulty is passed to PrintIntroduction( Difficulty). When PrintIntroduction begins to run the first thing it does is copy the value of LevelDifficulty and assigns that value to Difficulty, a new, unique variable.

They are two different memory locations. Anything done to Difficulty does not link back to LevelDifficulty other than through a return() call, if so written.

Not sure what I did wrong here, but I have to complete each level twice for it to update to the next level

Im dumb >.< Dont be like me and add the PlayGame(); in the while statement

All this passing around of parameters is going to make me pass out, but I think perhaps I also now understand. Unless I don’t.

I kind of get the idea of passing arguments. Need more examples to understand better, for instance passing more than one argument at a time.
What I don’t understand is the while loop here.

while(true) 
   {
       bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear();
        std::cin.ignore();
        if (bLevelComplete)
        {
            //increase the level difficulty
            ++LevelDifficulty;
        }

So how does the ‘true’ in while(true) know “it is true that the player has played the game”? How come it knows to loop the game regardless of whether the level is beat or not? Because, doesn’t the bool PlayGame () return false when the the player guesses are incorrect? So how does while(true) know to loop when PlayGame() returns false?

Also, how does if(bLevelComplete) know to only take action when the the level is beat? I cannot see how a condition written just “bLevelComplete”, which, I understand as, a bool variable that’s supposed to have 2 values (T or F), can tell a function to do things only when it’s true.

So these are my confusions. I don’t even know if what I wrote make sense. I haven’t moved onto the next videos, maybe my questions will be answered there.:persevere:

It’s a style decision. The idea is program functionality should be in named methods, like PlayGame(). Why? It’s easier to debug and maintain. We want the activity in PlayGame() to be just about playing the game, not other actions.

If everything is left in main() it might get unclear as to why variables are changing to what end, and what our logic is attempting to do.

So many programming disasters are caused by people putting in a quick fix somewhere, unrelated to the task in the module, not commenting it, and leaving it for future developers to break their heads trying to figure out.

I don’t think it knows, we just told it to do what we want.

First, the variable is created

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

LevelDifficulty links with PlayGame()


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PlayGame links with PlayGame
PlayGame links with Difficulty
Difficulty links with Difficulty
Difficulty links with PrintIntroduction

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

PrintIntroduction links with PrintIntroduction
PrintIntroduction links with Difficulty
Difficulty is then printed.
NOTE: Difficulty is a local variable, it is not the same variable as the one before even though it has the same name.


I think keeping it the same name though out the entire code might help?
correct me if I am wrong
hope this helps