6.5. Variables and Parameters are Local

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

When you try to run the program, it produces an error because the variable y is not declared in the main function.

The variable y only exists while the function is being executed — we call this its lifetime. When the execution of the function terminates (returns), the local variables are destroyed. Codelens helps you visualize this because the local variables disappear after the function returns. Go back and step through the statements paying particular attention to the variables that are created when the function is called. Note when they are subsequently destroyed as the function returns.

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

So it is not possible for a function to set some local variable to a value, complete its execution, and then when it is called again next time, recover the local variable. Each call of the function creates new local variables, and their lifetimes expire when the function returns to the caller.

Variables can only exist in the function that they are declared, but this can be confusing when there are two different variables with the same name. Run the following example using CodeLens:

Step through the above code and notice how there are two local variables, both called y. The difference is that they exist in different local scopes, one in the fun function scope the other in the main function scope. So when the main function prints y, it has the value it was assigned in the main function.

A program that contains functions must put all code, except for import statements and the main call, inside of a function. So it is not possible to create variables outside of functions. The following program will produce an error if you try to run it because the variable x is declared outside of any function.

To cement all of these ideas even further lets look at one final example. Inside the square function we are going to make an assignment to the parameter x There’s no good reason to do this other than to emphasize the fact that the parameter x is a local variable. If you step through the example in CodeLens you will see that although x is 0 in the square function scope, the x in the main function scope remains 2. This is confusing to many beginning programmers who think that an assignment to a formal parameter will cause a change to the value of the variable that was used as the actual parameter, especially when the two share the same name. But this example demonstrates that that is clearly not how Python operates.

Check your understanding

    func-3-1: What is a variable’s scope?

  • Its value
  • Value is the contents of the variable. Scope concerns where the variable is "known".
  • The range of statements in the code where a variable can be accessed.
  • Correct, the scope of the variable is the function it is defined in.
  • Its name
  • The name of a variable is just an identifier or alias. Scope concerns where the variable is "known".

    func-3-2: What is a local variable?

  • A temporary variable that is only used inside a function
  • Yes, a local variable is a temporary variable that is only known (only exists) in the function it is defined in.
  • The same as a parameter
  • While parameters may be considered local variables, functions may also define and use additional local variables.
  • Another name for any variable
  • Variables that are used outside a function are not local, but rather global variables.
You have attempted of activities on this page
6.4. Unit Testing"> 6.6. Functions can Call Other Functions">Next Section - 6.6. Functions can Call Other Functions