Confused about how parameters are passed from function to function

In the lesson, a LevelDifficulty variable was declared under the main() function.

int main()
{   
    int LevelDifficulty = 1;

This variable was then used as a parameter for the PlayGame () function inside the SAME code block.

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

And then it was explained somehow that the value of “LevelDifficulty” is passed on to the PlayGame() function OUTSIDE the code block.

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

This outside-codeblock PlayGame() function has declared its own parameter: “Difficulty”

I don’t understand how the value of “LevelDifficulty” is passed on to “Difficulty” when the two parameters clearly have different names.

Question 1:
How is this working and why must the names be different? Why can’t the parameter for the outside-code block PlayGame() be also “LevelDiffcutly” instead of the differently-named “Difficulty”?

Question 2:
Why must the “Difficulty” parameter inside the outside-code block PlayGame() be declared with an “int”? In the main(), the “LevelDifficulty” was already declared with an “int”. If “LevelDifficulty” and “Difficulty” are supposed to carry the same value (which is an integer), then shouldn’t there be no need to repeat the “int” declaration?

I feel like there was something that wasn’t clearly explained. Thanks to anyone who will be able to answer!

My full code below for reference

#include <iostream>

void PrintIntroduction(int Difficulty)
{
    // Print welcome messages to the terminal
    std::cout << "\n\n___________________________AAAAAA\n";
    std::cout << "_______________________AAAAAcceeAA\n";
    std::cout << "_____________________AAAcccceeaaAA\n";
    std::cout << "_________________AAAAAcccceeaaaAA\n";
    std::cout << "_____________AAAAAcccccceeaaaaAA\n";
    std::cout << "__AAAAAAAAAAAAcccccceeaaaaaaaAA\n";
    std::cout << "AAAccccccccccccceeaaaaaaaaaaAA\n";
    std::cout << "__AAAaaaaaaaeeceeeeeaaaaaagAA\n";
    std::cout << "____AAAAAAaaaaaaaaeeeeeeagAA\n";
    std::cout << "________AAAAAAAAaaaaaaeeeggAA\n";
    std::cout << "_________#######AAAAAaaaagggAA\n";
    std::cout << "________###| |########AAaaaagggAA\n";
    std::cout << "______AA###|_|###| tr|#####AAAaaggggAA\n";
    std::cout << "____AAEE#########|_|########AAAAgggAA\n";
    std::cout << "__AAEEAA#################EEEEEAAAAA\n";
    std::cout << "__AAEEEEEE#########EEEEEEEEAAEAA\n";
    std::cout << "__AAAAEEEEEEEEEEEEAAAAAAEEEEEAA\n";
    std::cout << "_A§xxxAAEEEEEEAAAAEEEEEEEEEEEEAA\n";
    std::cout << "_A§xxxAAEEEEAAAAAAEEEEEEEEEEEEAA\n";
    std::cout << "__AAAAEEEEEE§xxxAAEEEEEEEEEEEEAA\n";
    std::cout << "__AAEEAAEEEE§xxxAAEEEEEEEEEEEEAA\n";
    std::cout << "__AAEEAAEEEEAAAAAAEEEEEEEEEEAA\n";
    std::cout << "__AAEEEAAEEEEEEEEEAAEEEEEEEEAA\n";
    std::cout << "__AAEEEEAAAEEEEEEEAAEEEEEEEEAA\n";
    std::cout << "_AAEEEEEEEAAAAAAEEEEAAEEEEAAEEAA\n";
    std::cout << "AAEEEEEEEEEEEEEEEEEEEEAAAAEEEEEEAA\n";
    std::cout << "_AAAAAAAAAAAAAAAAAAAAA____AAAAAAA\n";

    std::cout << "\nWelcome to the Tower of Mathemon. You are at floor " << Difficulty << ".";
    std::cout << "\nEnter the three mathemagical numbers to ascend...\n"; 
}

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

    // Declare mathemagical numbers
    const int CodeA = 4;
    const int CodeB = 2;
    const int CodeC = 1;

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

    // Print CodeSum and CodeProduct to the terminal
    std::cout << std::endl;
    std::cout << "- There are three numbers.";
    std::cout << "\n- They add up to: " << CodeSum;
    std::cout << "\n- They mutiply to give: " << CodeProduct << "\n";

    // 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 guess is corect 
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "\nYou've chosen the correct Mathematemons!";
        return true;
    }

    else
    {
        std::cout << "\nYou've chosen poorly and have angered the Mathemategods!";
        return false;
    }
}

int main()
{   
    int LevelDifficulty = 1;

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

        if (bLevelComplete)
        {
            ++LevelDifficulty; 
        }
        
    }
    return 0;
}

The while statement makes sense as far as that is where the lesson is at…that will change of course.

Let’s ask a question first…what is your understanding of parameters and arguments?

Specifically, what I mean is that they look the same, but a parameter is what the function WANTS:

PlayGame(LevelDifficulty);

Think of it like the function asks for a ‘type’ of a parameter.

An argument is what the function GETS:

We declare this variable so we can store values in it and pass it around.

int LevelDifficulty = 1;

To be specific, the variable declaration is not the argument. The variable, once declared, is given a value and passed around as an argument.

The argument is the ‘value’ of the ‘type’ requested as a parameter.

In this case, you’ll pass in an argument (variable) named LevelDifficulty of type ‘int’ with a value of 1 to satisfy the parameter of type ‘int’ that the function is requesting.

So we pass the argument into the function as a parameter. That’s crucial to get correct.

EDIT: I did some reviewing and I was mistaken about something before, but this pic is good to illustrate.

PlayGame() is asking for a parameter of type int named 'Difficulty" (the variable)
PrintIntroduction() is being provided an argument of the variable ‘Difficulty’ and will print that value: 1.

Your main() method is asking to return an int, which is probably fine.

Inside you declared LevelDifficulty = 1;

The while (true) statement will run forever as mentioned

LevelDifficulty gets passed into PlayGame as an argument

PlayGame is stored in a bool bLevelComplete

if (bLevelComelete) {++LevelDifficulty}; Makes sense

LevelDifficulty and bLevelComplete aren’t necessarily scoped to main() so they might be accessible outside except through interface of some sort. I’m not sure how C++ handles scope. I’ve got a better understanding with JS and C#.

Thanks for the reply, Jay.

Please note that the code I shared is incomplete – I’m still building it as I go through the Unreal Engine C++ lessons on Udemy (currently at Lessons #27). It is entirely based on the step-by-step, concept-to-concept explanations of the instructor. Hence, there are parts that I understand that are still placeholders – such as the while (true) statement. We haven’t gotten to the part where we add a max level or something.

The structure is the instructor’s. I was just trying to mimic that in my code. I appreciate the comments on the functionality of the overall structure, but what I’m really interested are the answers to Questions 1 and 2 stated above.

So as to why LevelDifficulty and Difficulty are declared separately – I don’t know haha. This was what the instructor did and was just trying to copy that in my code. And that’s also something I don’t understand, which is why I’m asking the questions in the first place.

Apologies, but I wrote this post assuming that people reading have also taken the said Lesson 27 on Udemy. I suggest checking it, if you have access to it, for better context.

Thanks again.

:slightly_smiling_face: No worries. I take the Unity and Blender courses for fun, but I also look at a lot of other stuff like React, JavaScript, and Python for work.

I’m not familiar with how C++ is structured. Unreal won’t run on Linux, so I don’t use it. I have no practical means to take the Unreal classes.

That said, ‘digging through code’ is digging through code and seeing what make sense. It was fun to look at.

Still, I’ve never seen variables declared inside of functions like that in any other language. I’ve seen expressions passed in as arguments and such, but declaring variables in there is new to me.

Hi @easydeasy ,
it’s been a while since I did something in Unreal, but I’ll try to give you explanation:

  • in any programming language, you have variables and arguments.
  1. Variables are used to store values:
    a = 1
    b = 2
    etc.

these are declared either globally (available in the entire script) or locally (available in a function or a statement).
Ex:

int main(){
   // int a is available in the entire int main() including in if statement => GLOBAL
   int a = 1;
   int b = 2;
   if (a > 2) {
       // c is declared in if statement so it is available only here in if {} => LOCAL
       // the value of c will be 1 + 2 = 3;
       int c = a + b;   
   } // after if {} statement is done, c disappears 
}
  1. Arguments are something that a function is expecting.
bool PlayGame(int Difficulty){}

This roughly means that your function is waiting for you to give it an argument, in your case, in integer number (not a float, not a bool, not a char). It then executes the commands according to your argument.

If you ever used a comand line application, a terminal, a DOS prompt and so on, you use arguments in your commands to achieve your goal.
For example:

open cmd prompt in Windows and type:

netstat

After that type netstat adding the argumen -a (it’s called a parameter but for this example argument is valid)

netstat -a

(If you need to manually close netstat simply press CTRL+C)

As @b4t54ndw1ch wrote, it’s very important to know the difference between variables and arguments. This are generally used in every programming languages.

Now for your questions:

  1. Question 1: Must the names be different?
    Short answer: NO. As long as you, as a programmer, know what you are typing, and you can read your own code, you can use the same name for a variable and an argument.
    I mostly use different names as the ones in the courses.
    But be careful: you cannot directly change the type of an argument in your function and you cannot double declare it.
bool PlayGame(int LevelDifficulty) {
  bool LevelDifficulty = true; // this is a NO-NO. in this function, Difficult must always be an int because you declared it as an argument in the function
}

You are the programmer, you declare your variable names and argument names as you wish, as long as you avoid conflicts like double declaration, assigning a wrong value and so on.

  1. Question 2: Must the “Difficulty” parameter be declared as an “int”?
    YES. It is standard in most programming languages that you declare the value of your argument/parameter in the head of your function declaration. If you do not declare the type of the argument, your code will simply not compile.
[RETURN_TYPE] NAME(TYPE [PARAM]) {} // (TYPE [PARAM]) only if needed. You can have functions without argument

It’s like you come to me and say: “I saw a great … today.”
And nothing more. I have no idea what you what me to do, or what you are saying to me.
You know what you want to say, but I can’t read minds, I have no idea what you want from me.

And I’ll ask you: “what?” And you answer: “I saw an amazing car today.”
Now that makes sense.
In this analogy, the word “car” is the argument / parameter. And the phrase “I saw an amazing … today.” is the function.
… can be anything: car, house, 25, etc.

in code this will be something like: (my not be 100% accurate because I mostly program in GO, so my C++ syntax may be rusty, but I hope it’s clear enough)

string CreatePhrase(string Thing) {
     return "I saw something " + Thing + " today";
}

void SayPhrase(){
    string Thing = "car";
    string FullPhrase = CreatePhrase(Thing);
}
/* this will NOT compile, because the compiler  will have no ideea
what you want to do with LevelDifficulty*/
bool PlayGame(LevelDifficulty) {}

A compiler is not a AI. It will not know what to do until you tell him everything you need, word by word.

@b4t54ndw1ch hi Jay, just as a side note, you can run Unreal Engine on Linux. I do it to on Ubuntu. You only need to build the source code from GitHub.
Just follow this link to the official documentation: UE on Linux

Take care! :beers:

Thank you @z.goerbe, but I don’t think my message is getting across.

Not even sure my questions have something to do with the difference between variables and arguments (appreciate the explanation though).

Let me restate: What I don’t understand is HOW the “LevelDifficulty” variable under main() is able to pass off the same value to the “Difficulty” varaible under PlayGame(), when the variables have different names.

This is the main() with the “LevelDifficulty” variable.

int main()
{   
    int LevelDifficulty = 1;

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

        if (bLevelComplete)
        {
            ++LevelDifficulty; 
        }
        
    }
    return 0;
}

This is the PlayGame() with the “Difficulty” variable.

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

    // Declare mathemagical numbers
    const int CodeA = 4;
    const int CodeB = 2;
    const int CodeC = 1;

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

    // Print CodeSum and CodeProduct to the terminal
    std::cout << std::endl;
    std::cout << "- There are three numbers.";
    std::cout << "\n- They add up to: " << CodeSum;
    std::cout << "\n- They mutiply to give: " << CodeProduct << "\n";

    // 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 guess is corect 
    if (GuessSum == CodeSum && GuessProduct == CodeProduct)
    {
        std::cout << "\nYou've chosen the correct Mathematemons!";
        return true;
    }

    else
    {
        std::cout << "\nYou've chosen poorly and have angered the Mathemategods!";
        return false;
    }
}

In the code blocks above, “LevelDifficulty” is initialized with a value of 1 under main().

Now that value of 1 is passed off to the “Difficulty” variable under PlayGame().

Question 1 was more of how and why “LevelDifficulty” and “Difficulty” can have the same value of 1 even though they are different names and are in different code blocks.

gfds

I’m wondering how this would compile without casting the int to a bool somehow first? I would think it would throw an exception. Maybe it is time for me to start learning about C++? This is kind of interesting.

Have you watched or worked through the lesson or two following this one? Sometimes the answers are just a little further ahead. Are you sure you’ve gotten to the part where the instructor shows the corrected code?

What I mean is: those two variables should be scoped totally differently, so unless there is something else in the code that we’re not seeing, I wouldn’t expect them to be related.

Suffice to say that the PlayGame appears to be taking in a parameter of type int, but returning a bool. Is there some instructor magic that has yet to be explained?

Once again, this is cool. :grinning:

Side note: I stand corrected on the Linux thing. I know I tried to see if I could get it to run at one time in the past but I had so much trouble I gave up and went with Unity instead. That was a long time ago. I might try to load Unreal up later. Pop!OS is very similar to Ubuntu and Unreal should run on Pop!OS if Ubuntu will run it…honestly though, I’d rather wait until I can get a Macbook.

Hi @easydeasy

OK, I’ll try again:

Variable names and argument names are irrelevant, as long as you declare them correctly.

Programming actually happens in your computer hardware (RAM, CPU, GPU and so on).
A compiler basically transforms what you write (C++ syntax) to Machine Language composed of 0 and 1.
The computer works through machine language, not through C++, C, GO and so on.

  • every time when you declare a new variable, you reserve a space in your computers physical RAM memory. This reserved space will store the information of your variable. In this case, it actually reserves 4 Bytes of Memory to store your value of 1.
    Each memory space is unique so you can’t re declare the same space twice.

  • when you create a function, with an argument, that argument also reserves a physical memory space, but it’s empty and in standby. In your case, bool PlayGame(int Difficulty) also stores 4 Bytes of RAM memory, but it’s empty.
    When you pass a variable as an argument, you basically copy the vale from Address A (int variable) to Address B (in argument). Value from Address A stays intact, you process the information with the value from Address B.

Names of this variables are irrelevant, cause in Machine Language is all about Memory Addresses.
You can only work with int-int, char-char, bool-bool and so on, because each memory address is reserved for specific types. For example you can’t use a memory address reserved for an int and store a char in it. (Except if you cast the variables / arguments, but that’s another topic)

Names are used by us and the compiler what we what we want and to ensure that we don’t screw up the machine.
It’s important that you understand how variables and arguments work, at a very raw level of how they are stored in your physical computer, because you will have to work with memory pointers and memory addresses and then is where the real fun starts :grinning:.
I would recommend you either take some pure C++ course or search online for books or articles, to learn the background processes of how programming takes place in the memory.

Take care. :beers:

Privacy & Terms