There is one important thing I want to talk to you
about before we continue, and this is the scope of
a variable. The scope refers to where a variable
exists and where it is available. Depending on
where you create a variable, it may or may not be
available to other parts of the program. And to
explain this, I'm going to start with an example.
So we are going to continue with our code here, we
have so, call to print_double_number(3). And
what I'm going to do after, I'm going to do print(result).
I'm going to print the result variable
that we have created here. Okay, so as you can
see, as a warning, it's already red, so let's
see what it does. And we have an error here. name
.result' is not defined. Why is that? We create a
variable result, and then we can't use it, it's
not defined. This is because of the scope. So from
what you have seen before, if you create a
variable, let's say create a variable a is equal
to two, then I can use the variable a, after I've
created it. So after line 11, I can use the
variable a as many times as I want. But when you
create a variable inside a function like here,
well, this variable will not be available from
outside of the function, as you can see here, with
this error. The variable that you create outside of a
function, for example, here, a is equal to two,
this is a global variable in the global scope,
okay. And the variable that you create inside the
function is what we call a local variable, which
only exists inside this local scope of the
function. So knowing this can avoid you a lot of
errors in the future, okay. When you create the
global variable, you can use it everywhere after
you create it. But when you create variables
inside functions, well, the variable can only be
used inside the function. And this is also what's
happening with parameters, okay. When you have
parameters, any parameter you have here, you will
be able to use it inside the function, okay, but
this basically, for every parameter, it's going to
create a new local variable inside the function.
So here if I say, Hello, you have two local
variable user_name and user_age. For double_number,
you have a local variable, named number.
And for print_double_number, you also have the
local variable named number. And as you can see
here, because the scope of this, scope of this, and
the scope of this are different, here this is not
a problem to use number twice, okay. Because this
is actually not the same as this, this number is a
local variable inside this function. And this
number is another local variables inside this
function. So you can have two variables with the
same name in different scopes, and they are going
to be different variables. Alright, so very
important thing to ask yourself when you create a
new variable, where does these variables exist.
And in the current scope, I'm writing code right
now, can I access this variable? Okay. If you want
to access the user_age, well, you can only access
the user_age inside the function say_hello. So if
you write code here, yes, I can access user_age.
Here, no, I can't access user_age and outside of the
function neither and very basically, but when you
create a new function, you create the new local
scope with new local variables. Alright, so
knowing this will help you understand some erros
when you get started. And later on, it will help
you also to avoid those errors.