1. Scope and user-defined functions
Wow! At this point, you know how to define your own functions - but not only that, you know how to write functions with multiple parameters and can return multiple values using tuples. Good job!
2. Crash course on scope in functions
We'll now talk about the idea of scope in the context of user-defined functions. You've been defining variables in your programs and so far, you've been using these variables without any problems. However, one thing you should know is that not all objects that you define are always accessible everywhere in a program.
Enter the idea of scope, which tells you which part of a program an object or a name may be accessed. Names refer to the variables or, more generally, objects such as functions that are defined in your program, for example, a variable x has a name, as does the function sum. There are three types of scope that you should know.
The first one is the idea of the global scope. A name that is in the global scope means that it is defined in the main body of a script or a Python program. The second one, is the local scope. A name that is in a local scope means that it is defined within a function. Once the execution of a function is done, any name inside the local scope ceases to exist, which means you cannot access those names anymore outside of the function definition. The third is something called the built-in scope: this consists of names in the pre-defined built-ins module Python provides, such as print and sum. You'll play around with the built-ins module in the interactive exercises.
3. Global vs. local scope (1)
Let's look at a couple of examples to clarify these definitions.
Let's check out our example function square from earlier. We define the function and then call it. If we then try to access the variable name new_val after function execution, the name is not accessible. This is because it was defined only within the local scope of the function. The name new_val was not defined globally.
4. Global vs. local scope (2)
Now what if we define the name globally before defining and calling the function? In short, any time we call the name in the global scope, it will access the name in the global, such as you see here.
Any time we call the name in the local scope of the function, it will look first in the local scope. That's why calling square(3) results in 9 and not 10.
If Python cannot find the name in the local scope, it will then and only then look in the global scope.
5. Global vs. local scope (3)
Here, for example, we access new_val defined globally within the function square. Note that the global value accessed is the value at the time the function is called, not the value when the function is defined. Thus, if we re-assign new_val and call the function square, we see that the new value of new_val is accessed.
To recap, when we reference a name, first the local scope is searched, then the global. If the name is in neither, then the built-in scope is searched.
6. Global vs. local scope (4)
Now what if we want to alter the value of a global name within a function call? This is where the keyword global comes in handy. To look at how it works, let's look at another example. Within the function definition, we use the keyword global followed by the name of the global variable that we wish to access and alter. For example, here we change new_val to its square. The function call works as one would expect. Now calling new_val, we see that the global value has indeed been squared by running the function square.
7. Let's practice!
Now it's your turn to play with all things scope, local, global and built-in. Have fun!