Someone explain to me what I just did

So in lecture 29: Function Parameters, we did a lot that I didn’t understand how a few things happened. Can someone explain to me what I did and how the code worked? And please do bear with me with this lengthy post.

So firstly in the previous lecture, I wrote in the code this:

while(true)
{
   bool bLevelComplete = PlayGame();
   std::cin.clear();
   std::cin.ignore();
}

What I understand from the code above is that we declared a boolean variable and assigned it to PlayGame, which returns true or false. So, the value of bLevelComplete changes to true or false depending on PlayGame return value. Am I correct so far?

But things got a little confusing after that, with parameters.

void PrintIntroduction(int Difficulty)

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

int main(int Difficulty)
{
    int LevelDifficulty = 1;
    int MaxDifficulty = 5;
    while (true)
    {
        bool bLevelComplete = PlayGame(LevelDifficulty);
        std::cin.clear(); // Clears any errors
        std::cin.ignore(); // Discards the buffer
        if (bLevelComplete)
    {
        ++LevelDifficulty;
    }
    }

So, we wanted to print the level of the player in the PlayerIntroduction. Since we can’t take the value of LevelDifficulty from the main function, the reason being parameters or variables declared in a code block can’t be used in another function, we added a parameter for PrintIntroduction, i.e. we declared a variable to the function as Difficulty.

But since we didn’t initialise Difficulty, it has to get the value from somewhere, as we have to get value of LevelDifficulty, we’ll start from there. We declared LevelDifficulty as a parameter for PlayGame(). So I did some studey on parameters and arguments, and just wanted to ask if LevelDifficulty in PlayGame(LevelDifficulty) is an argument or parameter?

Moving on, after that we declare a parameter for PlayGame() function as Difficulty. So, when we execute the program, first main() function runs and initialises LevelDifficulty value as 1 in PlayGame(LevelDifficulty), this in turn initialises the parameter of PlayGame, i.e. Difficulty as LevelDifficulty.

After that PlayIntroduction function runs, and since we want the Level of the player displayed in it, we declare a parameter for PlayIntroduction() inside PlayGame() function as Difficulty, and since it has been initialised it takes on its value. Then we declare a parameter for PlayIntroduction() function as Difficulty, and it takes on the value of LevelDifficulty.

That is what I understood from lectures: 28 Returning Data From Functions and 29 Function Parameters. Have I got it wrong or gone wrong somewhere?

I have the same doubt here as before, is Difficulty in PlayIntroduction(Difficulty) an argument or Parameter.

Lastly what are PlayGame(); PlayIntroduction(); inside the functions called?

1 Like

Mostly. You aren’t assigning it to PlayGame, you’re initialising (different from assignment) to the value returned from PlayGame not PlayGame itself.

To not get confused, the parameter name for PlayGame is Diffiiculty not LevelDifficulty.

LevelDiffculty = declared in main
Difficulty = declared as the parameter of PlayGame.

It’s an argument.

Bit confused here. This is what initialises LevelDifficulty to 1

int LevelDifficulty = 1;

Specifically to the value of LevelDifficulty which is a copy. It’s as if you did Difficulty = LevelDifficulty

That is passing Difficulty as an argument. Difficulty was already declared as the function parameter.

argument.

Function calls.

1 Like

I meant the same thing but yeah you presented it better, and thanks for clearing up on assigning and initialising. :+1:

Yeah, I meant Difficulty, but I used these two words so much that I missed and didn’t notice it. :smiley:

What I mean to say was, first it gets initialises by

int LevelDifficulty = 1

and gets assigned in PlayGame(LevelDifficulty)?? Right?

To the value of LevelDifficulty. Yep, as I said you present it better. :grinning:

So if I’m to explain it or write it, should I say “Pass/Passing an argument for PlayIntroduction as Difficulty”?

PlayGame(); is basically calling out the function PlayGame(). ‘Function calls’, thank you for that.

Thank you for reading it all and answering every question I had. :+1: :+1:

Well, at least I was close. :wink:

No, that’s not assigning anything. That’s passing LevelDifficulty as an argument to PlayGame (…which will initialise Difficulty).

Yes but I would word it as I just did. Passing Difficulty as an argument to PrintIntroduction.

Indeed! :slight_smile:

1 Like

Thank you so much, I didn’t think anyone would read the whole post, let alone answer it.:pray:

1 Like

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

Privacy & Terms