Kotlin Function: Print person details with named arguments
Write a Kotlin function that takes named arguments `name`, `age`, and `city` and prints the details of a person.
Sample Solution:
Kotlin Code:
fun printDetails(name: String, age: Int, city: String) {
println("Name: $name")
println("Age: $age")
println("City: $city")
}
fun main() {
printDetails(name = "Gerhard Rikuto", age = 22, city = "New York")
}
Sample Output:
Name: Gerhard Rikuto Age: 22 City: New York
Explanation:
In the above exercise -
- The printDetails() function takes three named arguments: name of type String, age of type Int, and city of type String.
- Inside the function, it prints the details of a person by accessing the values of the named arguments. The details are printed using println().
- In the main function, we call the "printDetails()" function and provide the values for the named arguments explicitly.
Kotlin Editor:
Previous: Calculate circle area with default pi.
Next: Print a greeting message with person's name.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics