Now that you are writing your own functions, we need to take some time to carefully understand how variables are used inside and outside of functions. You can initialize and use variables outside of any function, as you have many times already. You can also initialize and use variables inside a function. The location where you first initialize and use a variable is very important, and that location controls the variable's "scope". The concept of scope controls which parts of your code can see and use a variable and how long that variable lasts while your program is running.

Global Scope
When you initialize and use a variable outside of any function, it is said to have global scope. Most of the variables you have created so far have this global scope. In a simple program that has no functions at all, every variable in that program is global. When a variable has global scope, two important things are true:

Global variables can be used from anywhere in your program
Global variables will be valid for as long as your program is running
In the example below, we declare and use two variables in the main program - musicTypes and randomIndex. Because these variables were first used outside of any function, they have global scope. The variables are created when they are first initialized with an assignment statement (=), and they will then last until your program ends.

Try It Now


You can run this example to verify that nothing surprising happens with globally scoped variables. These variables behave the way you expect based on your experience to this point. We have simply defined "global" as a new term to describe these types of variables.

Local Scope
When you create a variable inside a function, it no longer has global scope. Instead, variables defined inside a function have local scope. Local variables have two very important differences from global variables.

Local variables can only be seen and used from inside that function
Local variables will be created when first assigned in the function and destroyed as soon as the function returns
Global variables can be used by code anywhere in your program and will last as long as your program runs. Local variables are created each time your function runs, can only be seen by code inside that function, and will be destroyed when the function returns.
Let's re-work our music selection example to put most of the logic inside a function. In the new code below, both musicTypes and randomIndex are first used inside select_music(), and therefore have local scope inside that function. Everything will work fine - until the main code outside the function tries to access one of those variables. Run the code and see what happens!

Try It Now


If you try to access a local variable from outside the function, you will get a run-time error because that variable is not visible to any other part of the program.

I like Jazz
Traceback (most recent call last):
File "code1.py", line 9, in
print(musicTypes) # ERROR - musicTypes does not exist at this point
NameError: name 'musicTypes' is not defined
When designing your own programs, be sure to understand the difference between global and local scope. Your functions can declare and use local variables that nobody else will see, and they can also use variables that were defined at a global level.

Using Global Variables from Inside a Function
Once you initialize a global variable, you can use it from anywhere in your code, including inside a function. As you can see in the example below, moving musicTypes out of the function to a global level still works just fine. The statements inside select_music() can read musicTypes as if it was declared locally.

import random

musicTypes = ["Rock","Rap","Country","Jazz","Pop"] # global variable

def select_music():
randomIndex = random.randrange(0,len(musicTypes))
return musicTypes[randomIndex] # reading an existing global variable

print("I like " + select_music())
Copy
However, there is a very important situation that should be clearly understood! What happens if you try to assign a new value to a global variable from inside a function? Try running the updated example below. We have declared musicTypes globally and then, inside select_music(), updated the list to contain a new set of values. At the end, we print the global musicTypes list to see what it contains.

Try It Now


Clearly, something strange is going on, as you can see in the example output below.

I like Soul
Global musicTypes: ['Rock', 'Rap', 'Country', 'Jazz', 'Pop']
The select_music() function seems to have updated musicTypes because it pulled a selection ("Soul") from the updated list. However, after the function returns, printing the global musicTypes list shows it still has the original contents!

By default, the first time you write an assignment statement for a particular variable inside a function, that assignment statement will create a new local variable - even if a global variable of the same name already exists! So, while select_music() was running, there were actually two variables named musicTypes in existence - one at the global level and one at the local level. The function statements will all use the local version, leaving the global version untouched.

It is possible to update a global variable from within a function, but one additional step is needed. Inside your function, add a statement that starts with the "global" keyword, followed by the name of the existing global variable.

musicTypes = ["Rock","Rap","Country","Jazz","Pop"] # global variable

def select_music():
global musicTypes # declare use of global variable
musicTypes = ["Reggae","Disco","Techno","Hip Hop","Soul"] # now we are using global variable
Copy
Below, we have updated select_music() with a statement that declares global access to the musicTypes variable. Now, when you run the program, there will only be one version of musicTypes throughout the program, and the function will update that global variable.

Try It Now


Function Parameters are Local Variables
When your functions define input parameters, those parameters are treated as local variables inside your function! You can update or use them as needed, but those parameter variables will be destroyed when the function ends. If the parameter name happens to match the name of an existing global variable, then you will have two copies of that variable - one global and one local - and your function will use the local version.

In this final example, we have changed select_music() to require an input list of music types. We gave that parameter the same name as the global variable ("musicTypes") - though we could have easily selected a different name. When you run the code, you will notice that the original, global musicTypes variable was not changed by the update inside the function. The local parameter variable was changed instead.

Try It Now


Best Practice - Avoid Using Globals inside Functions
When designing your program, it is generally considered a bad idea to write functions that access or update global variables. Your functions may change the global variable in ways that are not expected by other pieces of code. Similarly, if global values are changed elsewhere, then your function may not behave as designed.

Therefore, professional programmers will usually try to write functions that use input parameters to receive all of the data the function needs to perform a task. That way, the function can't possibly interfere with any other code. It will simply accept input values, perform some tasks based on those values and return some data to the calling program.

Of course, this "best practice" rule is simply a guideline. You may find situations in your own programs where using global data inside functions is convenient. But the old saying, "Just because you can, doesn't mean you should" is worth remembering when you make design decisions.

1. The concept of scope describes what two features of a variable?
2. Where do you declare variables that have global scope?
3. How long do global variables last and from where can you access those variables?
4. Where do you declare variables that have local scope?
5. How long do local variables last and from where can you access those variables?
6. What happens if you try to access a global variable by name inside a function?
7. What prefix do you add to a variable name inside a function to ensure access to the globally declared variable?
8. Would you best describe function parameters as having local or global scope?
9. What is best practice for thinking about global variables inside functions?
10. What approach can functions use to avoid having to access global variables?

1. The concept of scope describes where a variable can be seen and used in the code, and how long the variable lasts during the program execution.

2. Variables with global scope are declared outside of any function.
3. Global variables last for the duration of the program's execution and can be accessed from anywhere in the program.
4. Variables with local scope are declared inside a function.
5. Local variables are created when the function is called and destroyed when the function returns, and can only be accessed from within that function.
6. If you try to access a global variable inside a function without declaring it as global, the function will create a new local variable with the same name.
7. To access a globally declared variable inside a function, you need to use the "global" keyword followed by the variable name.
8. Function parameters are treated as local variables inside the function.
9. The best practice is to avoid using global variables inside functions to prevent unexpected behavior and code complexity.
10. Functions can use input parameters to receive all the data they need to perform a task, thus avoiding the need to access global variables.