Variable Scope
Variable scope determines where a variable is accessible within your code. Understanding scope is crucial to avoid naming conflicts, unexpected behaviors, and bugs. In EasyBite, scope rules are simple and beginner‑friendly: variables declared at the top level are global, while variables declared inside functions are local to those functions. Blocks like loops and conditionals do not create new scopes—everything inside a function shares that function’s scope.
Table of Contents
Why Scope Matters
- Prevents name clashes: Two variables with the same name can coexist if they’re in different scopes.
- Reduces bugs: You know exactly where a variable is valid and won’t accidentally overwrite it.
- Improves readability: Code readers see at a glance which parts of the program can use each variable.
Global Scope
A global variable is declared outside any function. It’s accessible from anywhere in your EasyBite program—both top‑level code and inside functions (unless shadowed by a local variable of the same name).
declare counter
set counter to 0
function incrementCounter()
// This refers to the global 'counter'
set counter to counter + 1
end function
incrementCounter()
incrementCounter()
show "Global counter is: " + counter // Displays: Global counter is: 2
counter
lives in the global scope.- Every call to
incrementCounter
updates the samecounter
.
Local (Function) Scope
A local variable is declared inside a function with declare
. It exists only while that function runs and is not visible outside.
function greet(name)
declare message
set message to "Hello, " + name
show message
end function
greet("Alice") // Displays: Hello, Alice
show message // Error: 'message' is undefined outside the function
message
is local togreet
.- Attempting to use
message
outsidegreet
causes an error.
Block Scope in EasyBite
Unlike some languages, EasyBite does not create new scope for loops or if
blocks. Any variable declared inside a loop or conditional is still local to the enclosing function, not to the block.
function countDown(n)
declare i
set i to n
repeat while(i > 0)
declare temp // temp is in function scope, not loop scope
set temp to i
show temp
set i to i - 1
end repeat
// 'temp' is still accessible here
show "Last temp was: " + temp
end function
countDown(3)
// Displays: 3
// 2
// 1
// Last temp was: 1
temp
is visible both inside the loop and after it, withincountDown
.
Declaring Variables
- Global: use
declare
at the top level. - Local: use
declare
inside a function. - Assigning: use
set … to
for all initializations and updates.
// Global declaration
declare totalSales
set totalSales to 0
function recordSale(amount)
// Local declaration
declare saleMessage
set totalSales to totalSales + amount
set saleMessage to "Recorded: " + amount
show saleMessage
end function
- You must
declare
each variable before using it. - Trying to
set
an undeclared variable causes an error.
Examples of Scope
Example 1: Global vs Local
declare value
set value to 10
function testScope()
declare value
set value to 20
show "Inside function, value = " + value
end function
testScope() // Inside function, value = 20
show "Outside, value = " + value // Outside, value = 10
- The
value
insidetestScope
is a different variable than the globalvalue
.
Example 2: Shadowing Variables
When a local variable shares the same name as a global, the local shadows or override or replace the global inside its function:
declare userName
set userName to "GlobalUser"
function printLocal()
declare userName
set userName to "LocalUser"
show "Local userName: " + userName
end function
printLocal() // Local userName: LocalUser
show "Global userName: " + userName // Global userName: GlobalUser
- Shadowing is intentional but use sparingly to avoid confusion.
Example 3: Loops & Conditionals
function findMax(numbers)
declare max
set max to numbers[1]
repeat while(index <= numbers.length())
if numbers[index] > max
set max to numbers[index]
end if
set index to index + 1
end repeat
return max
end function
max
andindex
are both local tofindMax
, even thoughindex
was not declared inside the loop orif
.
Common Pitfalls & How to Avoid Them
Pitfall | Symptom | Fix |
---|---|---|
Using undeclared variables | Runtime error: variable undefined | Always declare before set or use. |
Relying on block scope | Variables bleed outside blocks | Remember: loops/if s don’t create new scope. |
Shadowing unintentionally | Hard‑to‑trace bugs | Use unique names or avoid reusing global names locally. |
Modifying globals unexpectedly | Functions have side effects | Pass values as parameters and return results instead. |
Best Practices
- Minimize globals: Excessive globals make code hard to reason about.
- Prefer locals: Local variables reduce side effects and improve modularity.
- Use clear names:
totalCount
vs.count
helps differentiate scopes. - Declare early: Place all
declare
statements at the top of your function. - Document side effects: If a function changes globals, note it in comments.
Conclusion
Variable scope in EasyBite is straightforward:
- Global: declared outside functions, accessible everywhere.
- Local: declared inside functions, accessible only there.
- No block scope: loops and conditionals share their function’s scope.
By mastering scope, you’ll write safer, more maintainable code—avoiding name clashes and unintended side effects. Practice by refactoring code: convert globals to locals where possible, track where variables are used, and keep your EasyBite projects clean and bug‑free!