How to pass a nested case class (struct-like object) to a function in Kotlin

1 Answer

0 votes
data class Address(val city: String, val zipCode: String)

data class Person(val name: String, val age: Int, val address: Address)

fun printPersonInfo(person: Person) {
    println("Name: ${person.name}")
    println("Age: ${person.age}")
    println("City: ${person.address.city}")
    println("Zip Code: ${person.address.zipCode}")
}

fun main() {
    val home = Address("Nebula Vortex", "345645.09604.8471651.172561")
    val p = Person("Axton ", 37, home)

    printPersonInfo(p)
}


/*
run:

Name: Axton 
Age: 37
City: Nebula Vortex
Zip Code: 345645.09604.8471651.172561

*/

 



answered Nov 1, 2025 by avibootz

Related questions

2 answers 102 views
1 answer 56 views
1 answer 116 views
3 answers 1,597 views
1 answer 189 views
189 views asked Oct 8, 2014 by avibootz
...