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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/kotlin-exercises/function/kotlin-function-exercise-1.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics