Parameters & Scoping#
This modified example introduces a couple of key concepts for working with functions:
Inside a function, the arguments are assigned to local variables, or placeholder variables. These local variables are called parameters.
Key term: parameter
The name of the parameter inside the function is separated or isolated from the name outside the function. This separation of namespaces is called scoping.
Key term: scoping
Python Example#
# assign x variable
x = 1
# function definition
def print_number(x):
print(x)
# print x variable
print(x)
# call named function
print_number(2)
The value of x in the first line of this program has nothing to do with the purpose x is serving in the function definition.
In short, the placeholder variables (or parameters) we use inside the function definition are separated or isolated from any instance where a variable or parameter with the same name is used outside the function definition.
Click here to learn more about scope in Python, via W3Schools.