Local Variables

Variables declared inside of a function are considered to be local variables. They are available inside of the function but not outside of it. And those variables go “out of scope” once the function returns.

So, we could rework our function to use a local variable to hold onto the calculation:

fun main() {
  println(actLocally(1, 1))
}

fun actLocally(left: Int, right: Int) : Int {
  val result = left + right

  return result
}

Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.