w3resource

Kotlin Function: Personalized greeting message for user


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


Pre-Knowledge (Before you start!)

  • Basic Kotlin Syntax.
  • Kotlin Functions.
  • Function Parameters.
  • String Interpolation.
  • Printing Output.

Hints (Try before looking at the solution!)

  • Define the Function.
  • Print the Greeting.
  • Call the Function.
  • Test with Different Names.
  • Common Errors to Avoid:
    • Forgetting function parameters.
    • Misplacing string interpolation syntax.
    • Not handling empty or special-character inputs.

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.

Go to:


PREV : Kotlin Function Exercises Home.
NEXT : Print even numbers from an array.

Kotlin Editor:


Improve this sample solution and post your code through Disqus

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.