You're confusing a few things, which is easy to do when you get started with functions.
You don't need a global variable for what you're doing, but at some point, you should read Use of "global" keyword in Python to understand more about globals.
In your first 6 lines, where you define the function myfunction
, you're telling python that there is one parameter, named must_print
. This parameter is local within the function, and what you set it to only exists inside that function.
You can invoke the function in several different ways:
m_p = True
myfunction(m_p)
or
myfunction(True)
or even
myfunction(must_print=True)
They all boil down to the same thing: telling python to call the function myfunction
, and pass into the first parameter (which is named must_print
), the value True. Then python invokes the function, making a local variable named must_print
. (Incidentally, in your first line of the function, you set the variable to True, thereby disregarding whatever input is given. Consequently, myfunction(True)
and myfunction(False)
will have the same results right now.)
The key point is that you pass something else when you invoke the function - either a variable or a constant term. (In the third case, you're explicitly telling it that the argument named must_print
is set to be True. In that case, after the =
, there's either a constant or a variable. The first two cases simply set the first argument to True while the last one specifies the argument by name.) Outside of the function myfunction
, the variable must_print
is not defined, so python expects it to be somewhere in the global scope.
Note, to reduce confusion, I'm deliberately avoiding this:
must_print = True
myfunction(must_print)
This is perfectly valid code, but it's confusing. In that case, you have a variable local to your if clause that you use to set the argument you pass into the function, which then has a variable local to the function, of the same name. That's much more confusing, so I started with the m_p
variable, instead.
Some good reading on python scope is Short Description of the Scoping Rules?, particularly these two answers: https://stackoverflow.com/a/34094235/1404311, https://stackoverflow.com/a/292002/1404311
Edit, based on comment:
def myfunction(must_print=True)
and myfunction(must_print=True)
does two entirely different things.
The keyword def
is where you define a function. In a function definition, must_print=True
says that you have a variable named must_print whose default value is True. Thus, inside the function, whether you do myfunction(True)
or just myfunction()
, there will be a parameter named must_print
whose value is True. Only if you explicitly set it to some other value (such as myfunction(False)
will the variable of must_print
not be True.
In the second version, you're explicitly naming the argument. When you only have one argument, it's not really meaningful, so try this:
def foo(arg1=8, arg2='hello', arg3=True):
print(arg1, arg2, arg3)
You can then do foo()
, which will print "8 hello True". Or you can do foo(7)
, which will explicitly set arg1 and use the defaults for arg2 and arg3, and print "7 hello True". Or foo(6, 'goodbye')
prints "6 goodbye True", or foo(5, 'whatever', False)
, will print "5 whatever False". But how can you use defaults and only change the last argument? That's where named parameters come in. Try foo(arg3=False)
, and the output will be "8 hello False".
The best way to learn is by doing. Experiment with small functions like this one, inside the python interpreter. You'll get the hang of it quickly.