Understanding Variables
Local variables belong only to the function in which they are declared and are independent of global variables with the same name.
A Name in Two Places
Imagine that a program has a variable named x outside a function. The function also has a parameter named x and changes that parameter. The repeated name can be confusing, but the important question is not only which name appears. The important question is which block of code contains that name at the moment it is used.
What do you think happens?
Before tracing the function call, predict what happens when a function has a parameter with the same name as a variable outside the function and modifies its parameter.
Reveal answer
Answer: The function uses an independent local variable, and that local variable cannot be accessed after the function returns.
A parameter is a local variable. It receives a value from the caller, but modifying the parameter does not change the caller's variable. Local variables are created when the function is called and destroyed when the function returns.
Scope Follows the Defining Block
Scope is the part of a program in which a variable name can be used. The source material describes scope as being determined by the block in which a variable is defined. A function creates a boundary: variables declared inside that function are accessible only within that function. A variable declared outside all functions is global and can be accessed from anywhere in the program.
A local variable belongs only to the function in which it is declared. It is independent of a global variable with the same name.
Tracing a Function Call
A Parameter Shadows the Outside Name
Trace a program with an outside variable named score and a function whose parameter is also named score. The function changes its parameter and then returns.
Before the call: The program has an outside variable named score. This variable belongs to the outer, global scope.
At the call: The argument's value is passed into the function parameter. The parameter is a new local variable belonging to that function call.
Inside the function: The name score refers to the local parameter because that is the most local scope available inside the function. Changing this local variable does not change the caller's variable.
After the return: The function's local variable is destroyed when the function returns. The local score cannot be accessed after the function ends, while the outside score remains independent.
The matching names do not make the variables the same variable. Inside the function, score refers to the local parameter; outside the function, score refers to the outside variable.
The phrase most local scope available is a useful tracing rule. When a name is used, first inspect the current function or block. If that block defines the name, use that local variable. A name outside the function is relevant only when a nearer local definition is not available.
Parameters and Local Lifetime
A function parameter is a local variable. When the function is called, the parameter receives its value from the argument supplied by the caller. The parameter exists within the function call, so the function can use and modify it while the function is running. Modifying the parameter does not change the caller's variable. When the function returns, its local variables are destroyed and cannot be accessed after the function ends.
Debugging Scope Confusion
When a scope-related problem appears, trace the program at the point where the name is used. Identify the active function or block, list the variables defined there, and then check whether the name also exists outside that block. The name refers to the most local scope available. Finally, check whether the function has already returned, because its local variables are no longer available after that point.
Assuming that two variables with the same name are automatically the same variable.
A function creates a boundary, and the parameter belongs to the function's local scope.
Fix:
Track the block in which each name is defined.Assuming that changing a parameter changes the caller's variable.
The parameter is a local variable that receives a value from the caller.
Fix:
Trace the parameter and the caller's variable as separate variables.Trying to use a local variable after its function has returned.
Local variables are destroyed when the function returns.
Fix:
Check whether the name is being used within the function that defines it.Looking only at the spelling of a name instead of its scope.
A name refers to the most local scope available at the point where it is used.
Fix:
Identify the active block first, then determine which definition is nearest.
For scope debugging, make a short trace with four questions: Where is the name defined? Which function or block is active? Is there a more local definition with the same name? Has the function that created the local variable already returned?
Scope Tracing Practice
A program has an outside variable named count. A function also has a parameter named count. During the function call, the parameter is modified. Trace the two names and explain what remains available after the function returns.
Hints
- Mark the outside count and the parameter count as separate variables.
- The parameter is local to the function.
- Check the lifetime of the parameter at the return boundary.
- Locate where each variable with the name count is defined.
- Identify which function or block is active when count is used.
- Choose the most local definition available in that block.
- Separate changes to the local parameter from the outside variable.
- After the function returns, remove the local parameter from your trace.
What to Remember
- A function creates a boundary for local variables.
- A local variable belongs only to the function in which it is declared.
- A name refers to the most local scope available where the name is used.
- A function parameter is a local variable that receives its value from the caller.
- Local variables are destroyed when the function returns, and modifying a parameter does not change the caller's variable.
Key Takeaways
- Scope is determined by the block where a variable is defined.
- Variables with the same name can be independent when they belong to different scopes.
- Inside a function, the most local available definition is used.
- Function parameters are local variables, so changing a parameter does not change the caller's variable.
- A function's local variables exist during the function call and are destroyed when the function returns.