How parameter "Difficulty" knows it should be "1" than "2" etc

Hello!
I know there are similar topics, and I tried to find the answer, but I’m not managed to do it.
Here is my question related to “27. Function Parameters” video:

Below is the code which I have as on lecture №27 and I totally unclear with how the program knows that “Difficulty” should be “1” then after increasing the level - “2” etc.

I was confused because we just put this word as a function parameter, but didn’t initialize it somehow.
However, we initialize the parameter “Difficulty” and incremented it. It was clear to me. But I still don’t understand, how my program knows that there should be “1”, “2” etc after the words "You are secreting agent breaking into a level "

Please help to understand what makes the parameter “Difficulty” appears in Terminal as a number and what increments this parameter?

#include <iostream>

void PrintIntroduction(int Difficulty)
{
    // Print welcome messages to the terminal
    std::cout << "\n\nYou are a secrete agent breaking into a level " << Difficulty;
    std::cout << " secure server room...\nEnter the correct codes to continue...\n\n";
}


bool PlayGame(int Difficulty)
{
    PrintIntroduction(Difficulty);

    // Declare 3 number code 
    const int CodeA = 4;
    const int CodeB = 3;
    const int CodeC = 2;

    const int CodeSum = CodeA + CodeB + CodeC;
    const int CodeProduct = CodeA * CodeB * CodeC;

    // Print CodeSum and product to the terminal
    std::cout << "+ There are 3 numbers in the code";
    std::cout << "\n+ The codes add-up to: " << CodeSum;
    std::cout << "\n+ The codes multiply to give: " << CodeProduct;
    std::cout << std::endl;

    // Store player guess
    int GuessA, GuessB, GuessC;
    std::cin >> GuessA >> GuessB >> GuessC;
    
    int GuessSum = GuessA + GuessB + GuessC;
    int GuessProduct = GuessA * GuessB * GuessC;

    // Check if the player's guess is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "\nYou win!";
        return true;
    }
    else
    {
        std::cout << "\nYou lose!";
        return false;
    }
}

int main()
{
    int LevelDifficulty = 1;
    while (true)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); //Clears any errors
        std::cin.ignore(); //Discrards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
    }

    return 0;
}

Because those are the values you are passing into the functions.

int LevelDifficulty = 1;

Here you have initialised LevelDifficulty to 1. You then pass this value to PlayGame here

PlayGame(LevelDifficulty);

This will initialise PlayGame’s parameter with the value of LevelDifficulty (currently 1).
Same thing happens here

PrintIntroduction(Difficulty);

This is initialising PrintIntroductions parameter with the value of Difficulty (currently 1).

After all of PlayGame has finished executing and you would be back in main where LevelDifficulty is incremented if the level was completed successfully

if (bLevelComplete)
{
    ++LevelDifficulty;
}

Then this is repeated.

PlayGame(LevelDifficulty);

Now LevelDifficulty is 2, so it will be passing that value to PlayGame and initialising its parameter to that value.

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

Privacy & Terms