Function Scope and Local Variables
When you declare variables inside a function definition, they are not related in any way to other variables with the same names used outside the function - i.e. variable names are local to the function. This is called the scope of the variable. All variables have the scope of the block they are declared in starting from the point of definition of the name.
Two Places, One Name
Imagine that the surrounding program uses a variable named x, while a function also declares a variable named x. The matching spelling does not make these variables the same. A name declared inside a function is local to that function and is not related to another variable with the same name outside the function.
Following a Value Through a Function
The addtwo trace
Track the names and values when addtwo is called with 3 and 5, computes their sum in a local variable named added, and sends the result back to the calling code.
Arguments enter the function: Within the function, the parameters a and b have the values 3 and 5 respectively.
The local calculation occurs: The function computes the sum of a and b and places that sum in its local function variable named added.
The result leaves the function: The return statement sends the computed value back to the calling code.
The caller receives the result: The calling code assigns the returned value to x and then prints x.
The printed result is 8.
This trace separates two roles. The function uses its local names a, b, and added while doing its work. The calling code uses x after receiving the returned result. The function does not make its local variable added into the caller's variable x; instead, return communicates the computed value to the calling code, where that value is assigned to x.
What Scope Means
Scope is the part of a program in which a variable name belongs to its declaration. When a variable is declared inside a function definition, its name is local to that function. A same-named variable used outside the function is a separate variable, not another reference to the function's local name.
The source describes scope in terms of a block. Every variable has the scope of the block in which its name is declared, beginning at the point where that name is defined. For a function-local variable, the relevant block is the function. The name therefore belongs to the function's local context rather than automatically belonging to the surrounding program.
Where Availability Begins
Scope does not begin everywhere in the program merely because a name exists somewhere. The source states that a variable has the scope of its block starting from the point where the name is defined. This gives scope both a location and a starting point: identify the block, then identify where the declaration occurs within that block.
Use this test when analyzing a variable: Where was the name declared, and what block contains that declaration? Those answers identify the variable's scope according to the source's rule.
Changing a Top-Level Name
This rule is a direct consequence of keeping local and outside names distinct. A function's ordinary local name should not be treated as an assignment to a top-level name. The global statement is the explicit instruction that changes which name an assignment targets.
Mistakes Beginners Make
Assuming that same spelling means same variable.
The source states that a variable declared inside a function is not related to a same-named variable outside that function.
Fix:
Treat the two names as belonging to separate scopes.Forgetting where scope begins.
Scope begins at the point of definition of the name.
Fix:
Locate the declaration first, then trace the scope from that point through the block.Expecting a function's local calculation variable to appear automatically in the calling code.
In the source example, added is local to the function, while the calling code receives the value through return and assigns it to x.
Fix:
Follow the returned value into the calling code instead of treating the local name as shared.Assigning to a top-level name inside a function without global.
The source states that assignment to a variable defined outside a function is impossible without the global statement.
Fix:
Use the global statement when the intended target is the top-level name.
Scope Check Practice
A function receives two values, stores their sum in a local name, and returns the sum. The calling code stores the returned value in a different name. Identify which names belong to the function and which name belongs to the calling code. Then explain why the function's local sum name is not the same variable as the caller's result name.
Hints
- Start by listing the names declared or used inside the function.
- Separate the local calculation from the value returned to the caller.
- Use the rule that same-named variables in different scopes are not related.
What do you think happens?
Before reading the explanation, predict whether a function's local variable named added automatically becomes the caller's variable x.
Reveal answer
Answer: No, because the names belong to separate scopes.
In the source example, the function places the sum in local added, then return sends the value to the calling code, where it is assigned to x.
The Scope Checklist
- A variable declared inside a function is local to that function.
- A same-named variable outside the function is separate and unrelated to the local variable.
- A variable's scope is the block where its name is declared, beginning at the point of definition.
- A function can send a computed value to calling code with return; the caller can assign that value to its own variable.
- The global statement is required when a function is meant to assign to a name defined at the top level.
Key Takeaways
- Function scope keeps names declared inside a function local to that function.
- Matching names inside and outside a function still refer to separate variables.
- Scope begins where a name is defined and belongs to that name's block.
- Return passes a value to calling code without making the function's local variable shared.
- The global statement explicitly identifies a top-level name that a function is meant to assign.