Play Home Contribute GitHub Join Us Discourse Staff Members GitHub Contribution Guides Team

Im Very Confused *Passing Values* (Python)


Do I need to pass values in pyhton?

Looking at my code right below the white highlight I call a function. I dont pass it any values in the parameters. Now looking at the function with the blue highlight. I create a variable that uses X and Y but I didnt pass the values of X and Y. So how does it know what X and Y equals? Maybe im just use to C++. I want to know if this is special in Python. I dont know how my function knows the values of X and Y if they were never passed in.

I want to mention that this code works. If its not clear. I get the results I was looking for in that function. I dont see how im getting the results if im using variables that werent declared in that function.


Simple answer is to just stick with passing variables to a function, because it keeps the code contained to the function and it’s usually what you want to do.

Slightly complicated answer:

In most programming languages, such as Python and Javascript (even C++), there are 2 “namespaces” for variables, which basically means their rules for accessibility. We call these global and local. A global variable can be accessed by pretty much anybody - functions, your main code, whatever. A local variable exists within the domain of… let’s say a function. Any variables that are declared are defined to be “global” or “local” depending on whether they were created outside of a function or inside of it, respectively.

I can see why you are confused when thinking in terms of C++. When you define variables in C++, you’re defining them in a function with the prototype int main. This, in itself, is a function. That means that each of the variables you create there are local, and need to be passed to a function explicitly. But you don’t have to do that. You could do this:

#include <iostream>
using namespace std;

int test = 5;

void myFunction() {
	cout << test << endl;

int main() {

This function will print 5, because the test variable was created outside of main.

The idea is that, while you can use variables that are defined outside of a function, you don’t usually want to. If a function accesses global variables, it becomes difficult to “reuse” that function to do what you want it to do with slightly different values. It’s also more complicated to understand where all the values are coming from, making it difficult for someone else to use your function. There are certain situations where it can be useful, but most of them that I can think of would involve classes/objects.


I understand how local and global variables work. Ive been coding for a couple years at home with a couple different languages. My question may seem a little confusing because it was only a picture. Ill use your example to shows whats going on.

void myFunction() {
        //myFunction does not know what test is.
	cout << test << endl;

int main() {

       int test = 5;

This is exactly what my problem looks like. Its not possible for myFunction to know the value of test but in my case it does. X and Y were created inside of one function. I never passed the values yet another function knows the values. Now it very well might be because im on CodeCombat. Other than that im aware on how to make variables local and global.

Hope these pictures clear of the misunderstanding. Now I can assure you X and Y are declared locally to that one function. I did not declared the same variables gloablly else where either. They are only local.


Ok I found something very interesting please try to tell me what im doing wrong.

My first picture is an example of the problem thats going on.

I just realized in the 2nd picture when I had my hero say Mouse it was calling the function Mouse not the variable which is understand able since thats the only thing that it would recognize as Mouse.

In the 3rd picture I changed the function name and still called mouse and once again it some how uses variables that are declared locally.

The 4th picture doesnt have Mouses call Cat and now I get an error. This is the kind of error I would expect for all of my pictures.

I think for more advanced programmers that understand local and global. They should use any compiler other than CodeCombats. Maybe this is intentional for begginers. Honestly I think this will have a negative learning curve when the new coders try to code in a real compiler. Having a function call another function shouldnt be passing all of its variables to the function. It all most defeats the purpose of local and global. Thats how errors and bugs occur.

I just found out its kind of passing variables. Its seems to depend on the order in which the functions were created.

This is inside of Mouses().

This is back inside Cat().

To me in Python their isnt such a thing as local or global. Its all about order. Unless once again this problem is special to CodeCombat.

Now I know in C++ you can make function prototypes because if you make functions before Main. Then order matters but function prototypes fixes that problem. Now in my case with the pictures it seems order matters if I dont want to pass variables like I should. Now it seems that passing variables does work in Python but it works in a way im not used to.

Inside Mouses()

Once again this is either something different about Python or how CodeCombats operates the language.


Never mind this article explains everything. Python operates differently than C++ when it comes to passing values.

Heres another topic that shows my problem.


Sorry for assuming that you weren’t aware of global/local. I try to assume that people don’t know much past what CodeCombat teaches.

As for the way in which Python treats arguments, I was not aware that Python uses pass-by-object. Glad you figured it out, though. Thanks for the link.


Its fine it adds more content to the post if anyone wants to read it.