FT Salamander
FT Salamander

Reputation: 41

Why does this Kotlin code not print anything?

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)
}  

Upvotes: 3

Views: 337

Answers (2)

derpirscher
derpirscher

Reputation: 17400

  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)
}  

Upvotes: 6

Omar Mahmoud
Omar Mahmoud

Reputation: 3077

you need to call printCakeTop function to print

Upvotes: 2

Related Questions