Parameters vs. Variables

An assignment statement in a function may create a local variable for the variable on the left-hand side (LHS) of the assignment operator. It is called local because this variable only exists inside the function, and you cannot use it outside the function. For example, consider again the square function:

Try running this in the visulizer. When we try to use innerVar on line 6 (outside the function), Python looks for a global variable named innerVar but does not find one. This results in the error: Name Error: 'innerVar' is not defined.

When you create a variable inside of a function you can’t use it outside of that function. In this case, we create a variable called innerVar in the square function. However, we tried to print that variable later on, outside the function. Once the function is finished executing, all of the variables used inside disappear (are destroyed), so innerVar doesn’t exist after the return of the function. Our error tells us that innerVar is not defined, another way of telling us it doesn’t exist anymore!

We call this the lifetime of a variable. In our case, local variables, the ones created inside of the function, have short lifetime. They are only alive as long as the function hasn’t returned.

Parameters are also local, and act like local variables. For example, the lifetime of param begins when square is called, and its lifetime ends when the function completes its execution and returns.

You can think of the parameters as local variables. So

def square(param):
    res = param * param
    return res

print(square(10))

Means that we are telling our code to make param = 10. We then use that value inside of the function and return a value. We come back to the print function and print the return.

To clear up an important point. 10 is called the argument, because it is a concrete value. The parameter is the local variable used in the function. So, the value of the argument is assigned to the parameter.