Scope can be defined as the accessibility of a variable.
Say for example, in every office, there is a restricted area where only a few people have access.
Similarly, in Kotlin, the variables also have some access restrictions. The variables that are defined inside a Function, cannot be accessed outside of it.
fun main() { myFunction() println(x) } fun myFunction() { var x = 5 }
So, in the first line itself, the Function myFunction() is called.
myFunction()
And the function execution begins.
fun myFunction() { var x = 5 }
And initialised the variable x with 5 inside the function fun().
var x = 5
Then we come to the print statement,
println(x)
And end up with an error as output,
This is because the variable x is not accessable outside the Function. And that is called as Local Scope.
The variables that are declared inside a Function, can only be accessed inside the Function and is local to that Function. That is called the Local Scope of a variable.
Say for example, if there are two functions, firstFunction() and secondFunction().
fun main() { firstFunction() secondFunction() } fun firstFunction() { var x = 5 println("The variable x : "+x+" is not accesible outside the firstFunction()") } fun secondFunction() { var y = 7 println("The variable y : "+y+" is not accesible outside the secondFunction()") }
So, we have declared two functions, firstFunction(),
fun firstFunction() { var x = 5 println("The variable x : "+x+" is not accesible outside the firstFunction()") }
And second_func().
fun secondFunction() { var y = 7 println("The variable y : "+y+" is not accesible outside the secondFunction()") }
And we have two variables, x and y.
While x is declared inside the Function firstFunction() and is local to the firstFunction() and cannot be accessed outside it.
Similarly, y is declared inside the Function secondFunction() and is local to the secondFunction() and cannot be accessed outside it.
Next, let us see the Local Scope in nested Function.
Nested Function is a Function inside an existing Function.
fun main() { outerFunction() } fun outerFunction() { var x = 5 println("The variable x is accesible from the outerFunction() and the value is "+ x) var innerFunction = fun() { println("The variable x is accesible from the innerFunction() and the value is "+ x) } innerFunction() }
So, in the above example, there are two Functions, outerFunction() and innerFunction(). And the innerFunction() resides inside the outerFunction().
Now, we have declared a variable x inside the outerFunction().
fun outerFunction() { var x = 5 println("The variable x is accesible from the outerFunction() and the value is "+ x) var innerFunction = fun() { println("The variable x is accesible from the innerFunction() and the value is "+ x) } innerFunction() }
Now, since the innerFunction() resides inside the outerFunction(). The variable x is also accessable inside the innerFunction().
Now, what if, the variable x was defined inside the innerFunction(). Can the variable x be accessed from the outerFunction()?
Well! The answer is no.
Let us see the above example.
fun main() { outerFunction() } fun outerFunction() { println("The variable x is accesible from the outerFunction() and the value is "+x) var innerFunction = fun() { var x = 5 println("The variable x is accesible from the innerFunction() and the value is "+x) } innerFunction() }
So, in the above code, we have declared the variable x inside the inner_func().
fun outerFunction() { println("The variable x is accesible from the outerFunction() and the value is "+x) var innerFunction = fun() { var x = 5 println("The variable x is accesible from the innerFunction() and the value is "+x) } innerFunction() }
So, the variable x is not accesible from the outerFunction().
So, the moral of the story is, the variable that is declared at a higher scope could be accessed at a lower scope.
Say for example, your Boss will have access to your details. But you will not have access to any of your Boss's details.
And with this we come to the concept of Global Scope.
The variables that are declared outside the Function are accessable inside a Function. And they are said to be in Global Scope.
Let us see in the below example :
var x = 5 fun main() { myFunction() println("Outside the function "+x) } fun myFunction() { println("Inside the function "+x) }
So, in the above example, we have declared a variable x, outside the Function myFunction() and main() Function and initialised with 5.
var x = 5
Now since, the variable x is declared outside the Function myFunction() and main() Function, it is accessible both myFunction() and main() Function.