Scope
Scope determines where variables can be accessed in your code. In Stellartrails, variables defined inside a function are local to that function and cannot be accessed from outside. However, nested functions can access variables from their enclosing (outer) functions, a feature known as lexical scoping.
Understanding scope helps prevent naming conflicts and bugs, and allows for better code organization.
Example
fn outer() {
let x = 10
fn inner() {
print(x)
}
inner()
}
outer() # Should print 10
In this example, inner
can access the variable x
from outer
because of lexical scoping.