Passing values into functions

Hello, i finished the TripleX module, but i’m a little unclear on one part. The difficulty value (variable?) is being passed into a couple of functions, PrintIntroduction and PlayGame, but i haven’t defined a ‘difficulty’ variable anywhere. So i’m not sure where the value is coming from. Anyone help me to understand that? Thank you!

#include <iostream>
#include <ctime>

void PrintIntroduction(int Difficulty)
{
    // Print welcome message
    std::cout << "\n\nYou are a secret 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 = rand() % Difficulty + Difficulty;
    const int CodeB = rand() % Difficulty + Difficulty;
    const int CodeC = rand() % Difficulty + Difficulty;

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

    // Print sum and product
    std::cout << "+ There are 3 numbers in the code\n";
    std::cout << "\n+ The codes add-up to: " << CodeSum;
    std::cout << "\n+ The codes multiple to give: " << CodeProduct << 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 player is correct
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "Level up!";
        return true;
    }
    else
    {
        std::cout << "Try again!";
        return false;
    }
}

int main()
{
    srand(time(NULL)); //create a new random sequence based on the time of the day
    int LevelDifficulty = 1;
    int const MaxDifficulty = 5;

    while (LevelDifficulty <= MaxDifficulty) // Loops until all levels are completed
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); // clears any errors
        std::cin.ignore(); // discards the buffer

        if (bLevelComplete)
        {
            ++LevelDifficulty;
        }
        
    }
    
    std::cout << "\nGame completed";
    return 0;
}

When you declare a variable inside “()” in function, it means that it’s a parameter of that function. You define what parameter is when you call the function. Also, you can only use the parameter inside the function where it’s declared.

Here is example:

int main()
{
  ExambleFunction(1);
  ExambleFunction(2);

  return 0
}

void ExambleFunction(int Parameter);
{
   std::cout << Parameter;
}

In my example The first time the ExambleFunction is called in main, it’s going to print 1. The second time it’s going to print 2. So ExambleFunction prints anything you put as its parameter.

In your code, you define the Difficulty in main too when you call the PlayGame function.

Thankyou for the explanation! So let me check if i understand this correctly…
The part i was struggling with, was how did ‘difficulty’ get a value of 1.

The ‘difficulty’ parameter is being passed a value from the PlayGame() function. Which is getting it’s value further down in the code from ‘LevelDifficulty’

So really, ‘dfficulty’ is just a label and doesn’t matter that much? Hope i understood this.

you got it right. You declare and initialize LevelDifficulty in main. Then you give that LevelDifficulty to the PlayGame function as a parameter, Which uses that integer to create the game with the right difficulty.

but I don’t really understand what you mean by "So really, ‘dfficulty’ is just a label and doesn’t matter that much? ". Could you explain more what you mean by label?

Thanks so much for your explanation! I meant, it doesn’t matter what the parameter is called? It can be called anything but the value will not change. (as long as it is consistent within the function). If i understand right correctly.

Yes, it’s like a local variable.

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

Privacy & Terms