Swift scope: variables inside blocks
Goal
Understand how scope controls where variables and constants can be used. Focus on values declared inside if, else, and func blocks. Learn how to avoid "cannot find in scope" errors.
if true {
let message = "Inside"
print(message)
}
// print(message) // Error: message is out of scope
What is scope?
Scope is the region of code where a name is visible. Variables and constants exist only inside the block they are declared in. When the block ends, those names are no longer accessible.
func showTitle() {
let title = "Room calculator"
print(title)
}
// print(title) // Error: title is out of scope
Why developers care about scope
Scope prevents accidental changes to values that should be private. It keeps code organized so each block controls its own data. It reduces bugs by limiting where variables can be used.
func calculate() {
let roomLength = 6.0
let roomWidth = 4.5
let area = roomLength * roomWidth
print(area)
}
Scope in an if block
A value declared inside an if block exists only inside that block. You can use it inside the braces but not after the block ends.
let hasFurniture = true
if hasFurniture {
let furnitureCount = 3
print(furnitureCount)
}
// print(furnitureCount) // Error
Scope in else blocks
Each branch has its own scope. Values declared in if are not visible in else and vice versa.
let isLarge = false
if isLarge {
let label = "Large room"
print(label)
} else {
let label = "Small room"
print(label)
}
// print(label) // Error
Scope in functions
All variables declared inside a function are local to that function. They cannot be used by code outside the function. This makes functions independent and reusable.
func roomVolume() {
let length = 6.0
let width = 4.5
let height = 2.7
let volume = length * width * height
print(volume)
}
// print(volume) // Error
Using values outside a block
Declare the variable before the block if you need it later. Then assign it inside the block. Make sure it always gets a value before you use it.
var message = ""
if true {
message = "Inside"
}
print(message)
Common scope errors
Using a variable outside its block causes a "cannot find in scope" error. Declaring a new variable with the same name can hide the outer one. Forgetting to initialize a variable before use causes another error.
var result = 0
if true {
let result = 10
print(result)
}
print(result) // This prints the outer value
How to test scope behavior
Move a variable inside or outside a block and observe the compiler error. Check which variables appear in code completion at each line. Use print statements to confirm which value is in scope.
if true {
let x = 5
print(x)
}
// print(x) // Uncomment to see the error
Task A
Write an if block that declares a constant inside it. Attempt to print the constant outside the block to trigger a scope error. Explain why the error occurs.
if true {
let note = "Inside"
// print inside the block
}
// try to print note here
Task B
Declare a variable before an if block. Assign a value inside the block and print it afterward. Show that the value is accessible outside.
var status = ""
if true {
// assign a value here
}
// print status here
Task C
Write a function that declares three local constants. Calculate a result and print it inside the function. Try to print the local result outside the function.
func showArea() {
// declare three constants
// calculate area and print it
}
// try to print the area here
Task D
Write an if / else block with a local constant in each branch. Print the constant inside each branch. Attempt to print it after the block to confirm scope rules.
let isLarge = false
if isLarge {
// declare a label and print it
} else {
// declare a label and print it
}
// try to print label here