Variable scope (lexical scoping) in R Programming - Time & Space Complexity
We want to see how the time to run code changes when using variables inside and outside functions.
How does the place where a variable is defined affect how long the program takes?
Analyze the time complexity of the following code snippet.
outer_function <- function(n) {
x <- 0
inner_function <- function() {
for (i in 1:n) {
x <<- x + i
}
return(x)
}
inner_function()
}
outer_function(1000)
This code defines a function inside another function and uses a variable from the outer function inside a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop inside
inner_functionthat runs from 1 to n. - How many times: It runs exactly n times, where n is the input size.
Each time n gets bigger, the loop runs more times, so the work grows in a straight line with n.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 loop steps |
| 100 | 100 loop steps |
| 1000 | 1000 loop steps |
Pattern observation: The number of steps grows directly with the input size.
Time Complexity: O(n)
This means the time to run the code grows in a straight line as the input number n grows.
[X] Wrong: "Using a variable from outside the inner function makes the code slower or more complex."
[OK] Correct: The variable access is quick because R uses lexical scoping, so it finds the variable without extra loops or work.
Understanding how variable scope affects performance helps you write clear and efficient code, a skill valued in many programming tasks.
"What if the inner function created its own local variable instead of using the outer one? How would the time complexity change?"
Practice
lexical scoping mean in R programming?Solution
Step 1: Understand lexical scoping definition
Lexical scoping means R looks for variables in the environment where the function was written, not where it is called.Step 2: Compare options with definition
Only Variables are looked up where the function is defined, not where it is called. correctly describes this behavior; others are incorrect or unrelated.Final Answer:
Variables are looked up where the function is defined, not where it is called. -> Option AQuick Check:
Lexical scoping = lookup where defined [OK]
- Thinking variables are always global
- Confusing lexical with dynamic scoping
- Assuming function variables overwrite globals automatically
x inside a function in R?Solution
Step 1: Recall R syntax for variable assignment
In R, variables are assigned using the arrow operator (<-), especially inside functions.Step 2: Check each option
x <- 5 inside the function body uses correct R syntax. Options B, C, and D use syntax from other languages or invalid forms.Final Answer:
x <- 5 inside the function body -> Option BQuick Check:
Use <- for assignment in R [OK]
- Using global or var keywords (not R syntax)
- Using := which is not valid in base R
- Confusing R with other languages like JavaScript or Python
y <- 10
f <- function() {
y <- 5
return(y)
}
f()Solution
Step 1: Identify variable scopes
Global y is 10, but inside function f, y is locally assigned 5.Step 2: Determine returned value
The function returns the local y, which is 5, not the global y.Final Answer:
5 -> Option CQuick Check:
Local variable y inside function = 5 [OK]
- Assuming function returns global y (10)
- Thinking function returns NULL by default
- Expecting an error due to variable shadowing
z <- 3
f <- function() {
z <- z + 1
return(z)
}
f()What is the result?
Solution
Step 1: Analyze variable usage inside function
Inside f, z <- z + 1 tries to read local z before assignment, but local z doesn't exist yet.Step 2: Understand lexical scoping rules
R looks for z in local scope first; it is not found, so it does not use global z for the right side in assignment.Step 3: Resulting error
This causes an error: object 'z' not found.Final Answer:
Error: object 'z' not found -> Option DQuick Check:
Local variable used before assignment causes error [OK]
- Assuming global z is used automatically
- Expecting output 4 without error
- Ignoring variable initialization order
g() return?a <- 2
f <- function() {
a <- 3
h <- function() {
a <- 4
return(a)
}
return(h())
}
g <- function() {
return(f())
}
g()Solution
Step 1: Trace nested function variable scopes
Global a = 2, f() sets local a = 3, h() inside f sets local a = 4.Step 2: Determine return value of g()
g() calls f(), which calls h(), which returns its local a = 4.Final Answer:
4 -> Option AQuick Check:
Innermost local variable a = 4 returned [OK]
- Returning outer function's a (3) instead of inner (4)
- Returning global a (2)
- Expecting an error due to nested functions
Ready to prove it?
Pass the test and this pattern moves unknown
