0
0
R Programmingprogramming~10 mins

Variable scope (lexical scoping) in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Variable scope (lexical scoping)
Start: Global environment
Define function
Call function
Look for variable in function environment
Found?
Use variable
Return value or error if not found
End
When a function runs, it first looks for variables inside itself. If not found, it looks outside in the environment where it was created, following the chain until found or error.
Execution Sample
R Programming
x <- 10
f <- function() {
  x <- 5
  x
}
f()
Defines x globally as 10, then a function f that has its own x as 5 and returns it. Calling f() returns 5, showing function uses its own x.
Execution Table
StepActionEnvironmentVariable looked upValue foundNote
1Assign x <- 10Globalx10Global variable x set to 10
2Define function fGlobal--Function f created, enclosing environment is Global
3Call f()Global -> f--Function f starts execution
4Inside f: assign x <- 5fx5Local variable x set to 5 inside f
5Inside f: return xfx5x found locally in f, value 5 returned
6f() returnsGlobal-5Function call ends, returns 5
💡 Function f returns local x = 5; global x = 10 is not used inside f
Variable Tracker
VariableGlobal StartAfter f() callInside f after assignmentAfter f() returns
x10105 (local in f)10
Key Moments - 3 Insights
Why does f() return 5 and not 10?
Because inside f(), x is assigned locally to 5 (see execution_table step 4). The function uses its local x, not the global one.
What happens if x is not defined inside f()?
The function looks in the parent environment (global) for x (see concept_flow). If found, it uses that value; otherwise, it gives an error.
Is the global x changed when f() runs?
No, the global x remains 10 (see variable_tracker). The local x inside f() is separate and temporary.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 5. What value of x does f() use to return?
A10
B5
CUndefined
D15
💡 Hint
Check the 'Value found' column at step 5 in execution_table.
At which step does the local variable x get assigned inside the function?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look for 'assign x <- 5' in the Action column of execution_table.
If the line 'x <- 5' inside f() was removed, what value would f() return?
A10
BError
C5
DNULL
💡 Hint
Refer to concept_flow: function looks in parent environment if variable not found locally.
Concept Snapshot
Variable scope in R uses lexical scoping.
Functions first look for variables inside themselves.
If not found, they look in the environment where they were created.
Local variables override global ones with the same name.
Global variables remain unchanged by function local assignments.
Full Transcript
This visual trace shows how R uses lexical scoping for variables. We start with a global variable x set to 10. Then we define a function f that assigns x to 5 locally and returns x. When we call f(), it uses the local x inside the function, returning 5, not the global 10. The function looks for variables first inside itself, then outside in the global environment if needed. The global x remains unchanged after calling f(). This helps understand how R finds and uses variables during function execution.