Why does this Kotlin code not print anything?

Viewed 214

This code is supposed to print the = symbol. But, when I run the code, it shows nothing … and my code editor says:

Variable 'age' is never used
Variable 'layer' is never used.

I don't understand what is happening here (note: I am a total beginner).

fun main() {
    val age = 24
    val layers = 5
    // printCakeCandles(age)
    fun printCakeTop(age: Int) {
        repeat(age + 2) {
            print("=")
        }
        println()
    }
    // printCakeBottom(age, layers)
}  
2 Answers
  1. You are never calling the function printCakeTop, ie the only function that would print something

  2. the age you define with val age = 24 (line 2) is a different variable than the age parameter in function printCakeTop(age: int) (line 5 and 6).

  3. As all function calls, that would use your age or layer variables are commented out (//) , they are marked as unused.

So the following would print something, and also use the age variable

fun main() {
    val age = 24
    val layers = 5
    // printCakeCandles(age)
    fun printCakeTop(age: Int) {
        repeat(age + 2) {
            print("=")
        }
        println()
    }

    printCakeTop(age);
    // printCakeBottom(age, layers)
}  

you need to call printCakeTop function to print

Related