w3resource

Kotlin Function: Personalized greeting message for user

Kotlin Function: Exercise-1 with Solution

Write a Kotlin function that takes a 'name' as an argument and prints a personalized greeting message to the user.

Sample Solution:

Kotlin Code:

fun welcomeUser(name: String) {
    println("Hello, $name! Welcome!")
}
fun main() {
    val name = "Mateu Odelia"
    welcomeUser(name)
}

Sample Output:

Hello, Mateu Odelia! Welcome!

Explanation:

In the above exercise -

  • The "greetUser()" function takes a "name" parameter of type String. It prints a personalized greeting message to the user by concatenating the name with a greeting phrase.
  • In the "main()" function, we initialize a variable "name" with the value "Mateu Odelia" and then call the "greetUser()" function, passing the "name" variable as an argument.

Kotlin Editor:


Previous: Kotlin Function Exercises Home.
Next: Print even numbers from an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.