What does “scope” mean in Swift?

The term “scope” refers to the part of the code where a variable or function can be accessed.

Scope is something we use when we’re writing computer programs, the language itself doesn’t generally matter.

  • When we write a program, we often need to use different lines of code to help us do things like counting or remembering information.
  • But we can’t use all of our code expressions everywhere in our program, we have to be careful and make sure we’re only using them in the right places.
  • Kind of like we have different rooms or places for different things in our house.
  • In programming, we call those different areas “scopes.”

There are three main types of scopes: “global scope,” “local scope,” and “type scope.”

Global scope

Imagine yourself as a little kid… “Global scope” just means that we can use those special words or numbers anywhere in our program, kind of like how we can use our toys anywhere in our house.

  • So just like our toys can be used in any room, the special words or numbers in global scope can be used anywhere in our program.
let globalVariable = "I'm in global scope."

func globalFunction() {
   // code in here can access globalVariable
}
  • This example shows a variable called “globalVariable” which is accessible anywhere in the program

Local scope

Back to your toys… “Local scope” just means that we can only use those lines of code in that small part of the program, kind of like how we can only play with certain toys in a specific room of our house.

  • So just like we can only play with certain toys in a certain room, these lines of code in local scope can only be used in a specific part of our program.
func localFunction() {
    let localVariable = "I'm in local scope."
    // code in here can access localVariable
}

// code in here can't access localVariable
  • This example shows a variable that is only accessible within the specific block of code, in this case the localFunction()

Type scope

“Type scope” means we can only use lines of code with that specific type of thing, kind of like how we can only use certain toys with certain games.

  • So just like we only use certain toys for certain games, the special words or numbers in type scope can only be used with a certain type of thing in our program.
struct StructType {
    let structVariable = "I'm in type scope."
    // code in here can access structVariable
    func structFunction() {
        // code in here can also access structVariable
    }
}
  • This example shows a function called structFunction() that is associated with a specific data type, in this case “struct”. It can be used everywhere within this datatype.

Quick notes

  • “Global scope” stands for variables or functions that are accessible anywhere in the program.
  • “Local scope” stands for variables or functions that are only accessible within a specific block of code, such as a function or loop.
  • “Type scope” stands for variables or functions that are associated with a specific data type, such as a class or struct.
  • In each of the examples above, the scope defines where the variable or function can be accessed from within the code.

Leave a Reply

Your email address will not be published. Required fields are marked *