COMPSCI4039 Chap.4 Scope and Tracing Function Calls
Scope and Tracing Function Calls
Scope and Tracing Function Calls explains how names are kept apart once a program contains functions. Each call creates its own local variables, including its parameters, and those names vanish when the call ends. The main program's variables are global: a function can read them, but assigning to the same name inside the function creates a new local variable instead of changing the global one.
The reverse direction is closed entirely, so trying to use a function's local name from the main program raises a name error. These rules explain a family of examination-style questions in which the same name appears inside and outside a function.
When main passes a variable to a function that reassigns its parameter, the caller's variable keeps its old value, and the only way for a result to travel back is through the return value. The global keyword lets a function update a global variable deliberately, though passing values in and returning them out is usually the clearer design.
Lists behave differently because a list passed to a function is shared rather than copied, which Chapter 8 develops.
What this chapter covers
- 01
Local variables and the lifetime of a call
- 02
Global variables in the main body
- 03
Reused names in separate scopes
- 04
Why reassigning a parameter cannot change the caller
- 05
Name errors from using a local outside its function
- 06
The global keyword
- 07
Tracing outputs with one box per scope
Worked example · free
Explain an output that mixes a global and a local
- 1The global counter is 0. This allocation of marks is not a university marking scheme; it is our own practice aid.
- 1Inside tick, assigning counter = 5 creates a local counter; the global is not touched.
- 1tick returns the local value plus 1, which is 6, and x is bound to 6.
- 1The final print reads the global counter, still 0, and x: the output is 0 6.
Key terms
- Global variable
- A variable created in the main body of a program, readable from inside functions.
- Name error
- The error raised when code uses a name that does not exist in the current scope.
Scope and Tracing Function Calls FAQ
Why does my function not change the variable I passed in?
The call binds a new local parameter to the argument's value. Reassigning that parameter inside the function changes only the local name, so the caller's variable is untouched. Return the new value and assign it in the caller instead.
When should I use the global keyword?
Only when a function genuinely must update a value owned by the main program. In most cases it is clearer to pass the value in as an argument and return the updated value, because the function can then be read and tested without hidden side effects.
Why do I get a name error after calling my function?
Variables created inside a function exist only while that call runs. Once it returns, they are gone, so printing one from the main program fails. Store the function's returned value in a main-program variable and use that.
Can a function read a variable from the main program?
Yes. A global variable created in the main body can be read inside a function without any declaration. Problems begin only when the function assigns to that name, because the assignment creates a separate local variable. Read freely, but pass values in as arguments whenever the function's behaviour depends on them, so the dependency is visible.
Exam move
Scope questions reward drawing. For each program, sketch one rectangle for the main scope and one for every active function call, and write each variable inside the rectangle that owns it, updating values as you trace. When a call ends, cross out its rectangle.
Practise with pairs of nearly identical programs: one in which a function reassigns its parameter, and one in which it returns the new value to be stored by the caller; predict both outputs before checking. Add a third variant using the global keyword so the difference is visible side by side. Then rehearse explanations aloud, as the lecture asks: say which variable each print reads and why the call did or did not change it.
Keep a short list of the three outcomes that cover most questions here: the caller's value unchanged, a name error from a vanished local, and a deliberate global update. Finally, look ahead to lists, where a function can alter a shared list through a method even though it cannot rebind the caller's name.
Working through Scope and Tracing Function Calls in COMPSCI4039? Sia is AskSia’s AI Computer Science tutor — ask any COMPSCI4039 Scope and Tracing Function Calls question and get a clear, step-by-step explanation grounded in how COMPSCI4039 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.