Kotlin Function: Print message without return
Kotlin Function: Exercise-6 with Solution
Write a Kotlin function that prints a message and does not return anything.
Sample Solution:
Kotlin Code:
fun printMessage(message: String) {
println(message)
}
fun main() {
printMessage("Hello, world!")
}
Sample Output:
Hello, world!
Explanation:
The "printMessage()" function takes a parameter message of type String. Inside the function, it prints the message using the println function.
In the main function, we call the "printMessage()" function and provide the message "Hello, world!" as an argument. The function prints the message to the console.
Since the "printMessage()" function does not return any value (unit-returning function), its return type is Unit, which is the default return type for functions that do not specify a return type explicitly.
Kotlin Editor:
Previous: Check if a string is a palindrome.
Next: Calculate area of rectangle with default Values.
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-6.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics