Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Learn about the difference between global and local scope.
-
0:00
Welcome back.
-
0:01
Now that you now to define and call a function,
-
0:04
we have to expand our discussion a little bit to talk about something called scope.
-
0:08
Scope refers to the visibility of parts of our program.
-
0:12
Not all parts of a program can be seen by other parts of our program.
-
0:15
This is really important to remember when you're thinking about functions.
-
0:19
The code you write inside of a function is only accessible inside that function.
-
0:24
That means if you assign a variable inside a function, you can't use that variable
-
0:28
in another part of your program and expect it to reference the same value.
-
0:32
This can be a little hard to grasp without seeing it.
-
0:35
Let's jump into some code.
-
0:37
Okay, take a look at my workspace here.
-
0:39
You see we have a variable assignment num=10.
-
0:43
Then you see a function set_num.
-
0:47
Inside the set_num function there's another variable assignment.
-
0:50
Num=5 after the function definition the program calls set_num.
-
0:57
What do you think will be printed when this program is run?
-
1:00
Pause the video here.
-
1:01
Open up the attached workspace, and run the file in the workspace terminal,
-
1:06
see what's printed, and then come join me again.
-
1:09
All right, how did it go?
-
1:10
Were you a little surprised by the value that was printed?
-
1:13
Let's break this down.
-
1:15
The variable assignment num = 10 takes place inside
-
1:19
what is known as the global context, or the global scope.
-
1:23
It's global because it's not inside any other function,
-
1:26
it's at the highest level in the program.
-
1:28
Any other part of this program can see, access, and
-
1:32
use this variable including the code inside the set_num function.
-
1:36
So when the set_num function is called and
-
1:38
Python encounters the num = 5 variable assignment, it doesn't reassign num, it
-
1:44
creates a new num that only exist inside the local context of the set_num function.
-
1:50
That means that outside of set_num, even if we've already called it, num will
-
1:54
always reference 10 until this reference has changed in the global context.
-
1:59
Inside the set_num function however, num will have a value of five.
-
2:05
Additionally, if we create a variable inside set_num,
-
2:08
we can't access it outside of that function.
-
2:11
I'll create a new variable inside the function called letter, and
-
2:14
it'll reference the letter a.
-
2:21
Let's see what happens when I try to print the letter variable outside of set_num.
-
2:28
I'm gonna save, run this down in my terminal.
-
2:37
Okay, we're getting an error here.
-
2:39
A name error that says name 'letter' is not defined.
-
2:44
Well, that's because there is no letter variable inside the global scope.
-
2:48
Letter only exists inside the set_num function and
-
2:52
can only be accessed inside of it.
-
2:54
If we move our print statement inside the function and try again,
-
2:57
we'll probably get a different result.
-
3:03
Now I'm gonna save and try it again.
-
3:08
Cool, it printed the letter a.
-
3:10
So now that you know a little bit about global and local scope,
-
3:13
you might be wondering how you actually get values and variables out of functions.
-
3:18
A variable's created inside a function, in the local scope,
-
3:21
can't be seen in the global scope, then what's the point.
-
3:24
Stay tuned to find out.
You need to sign up for Treehouse in order to download course files.
Sign up