Kotlin.unit print on result [SOLVED]

Hi, I have recently started learning Kotlin and I am not understanding why am I getting the result

Angry
The general is very kotlin.Unit

with the following code:

fun feeling(mood: String = “Angry”) {
println(mood) }
println("The general is very ${feeling()} ")


I have also tried using the following and god the same result:

fun feeling(mood: String = “Angry”) {
println(mood) }

println("The general is very ${feeling("")} ")

If I use feeling(), it gives me normal result.

Thanks in advance.

Welcome to the forum :slight_smile:

Let’s take a look at your feeling function.

fun feeling(mood: String) {
    println(mood) 
}

Here we have a function that takes a String for the mood and prints it to the console. It may be helpful to imagine this function as a well trained service dog that knows this one task–when you give it a word, it writes it down for you.

This function does not return anything though. Meaning that no matter what it does, the dog wouldn’t fetch anything, he wouldn’t return any words or toys or do anything but go off and complete his task.

Let’s look at this function. I added “happy!” as the String that we pass to the feeling() function.

println("The general is very ${feeling("happy!")} ")

You’re trying to write down a sentence and use whatever value the feeling() function returns as part of that sentence. Kind of like waiting for the dog to return a word to you. The issue is that the dog doesn’t return anything. All the dog did was go off with the word you gave him (“happy!” in this case), write down his word, and come back empty-handed.

Here’s a version of your function that returns a value:

fun feeling(mood: String): String {
    println(mood) 
    return "Chew Toy"
}

Here, we now say the feeling() function returns a String. Now lets see what we get when we run the code:

fun main() {
//sampleStart
println("The general is very ${feeling("happy!")} ")
//sampleEnd
}

fun feeling(mood: String): String {
    println(mood) 
    return "Chew Toy"
}

Try pressing run and you’ll see we get:

happy!
The general is very Chew Toy

The first line is from the function printing to the output (aka, what our trained dog wrote). The second line is what we wrote, including whatever was fetched by our trained dog. In this case, our feeling() function returned “Chey Toy”.

Here’s a final change to make the feeling() function return the same word we passed it:

fun feeling(mood: String): String {
    println(mood) 
    return mood
}

Notice that if we call this function, it will first write down what word it got, then it will return that same word back.
Here’s a runnable (and editable) example to experiment with:

fun main() {
    println("The general is very ${feeling("happy!")} ")
}

fun feeling(mood: String): String {
    println(mood) 
    return "Chew Toy"
}

In your code you point out that you get “kotlin.Unit” instead of a word or even nothing at all. This is because in Kotlin, all functions return something. “kotlin.Unit” just means our trained dog fetched nothing for us. Because Kotlin returns this empty thing, Unit, we get to say all functions return “something”–which makes working with functions nicer.

Here’s a quick runnable example showing two functions that return different things:

fun getNumber(): Int {
    return 5
}

fun sayNumber(): Unit { // Notice we explicitly say we return Unit but we don't have to.
    println("My number is 5")
}

fun main() {
    println("Calling getNumber() function returns: " + getNumber())
    println("Calling sayNumber() function returns: " + sayNumber())
}

Notice that we explicitly say that sayNumber() returns Unit. If you don’t put a return type, all functions will by default return Unit. We could have written it as fun sayNumber() { [...] } instead.

Now to add the last part from your code.
In your code you have a default argument of “Angry” for the feeling() function. All this means is that, if no String is given for a mood, use “Angry” by default.

Be sure to check out the reference page. There you can find books, tutorials, language documentation with runnable examples, hands-on workshops (although those may not be best for beginners), and more.

5 Likes

Not sure that the dog comparison is the best, but I love it anyways (and I can’t come up with a better one) :thinking: :grinning:

1 Like

Haha yeah I don’t quit like it either but once the trained animal thing popped in my head I couldn’t think of anything better :man_shrugging:

1 Like

Thanks for the explanation. Makes much more sense now.
The way I see it now is that should not use a function (with println() command in it )with a println(" …") if we want to print it on the same line. Instead we need to use a function with a return command.
It’s clear now. Cheers mate!!!

1 Like

Yup! :slight_smile:

Here’s a bit more info in case it’s helpful:
The println() function is short for “print line”. You can also print without making a new line using print(). Here’s an example–notice we don’t have to call the sayNumber() function inside the string since, like your original example, it independently prints output instead of returning a value:

fun sayNumber() {
    println(5) // Here it really prints "5\n", notice the newline character \n
}

fun main() {
    print("My favorite number is: ")
    sayNumber()
}

Had we used print("My favorite number is: ${sayNumber}"), it would show a different result. This is because in order to print the whole sentence, Kotlin first has to gather up all the parts and combine them. Since one part of the string is sayNumber(), the sayNumber() function is first called to get it’s result (which is Unit), then the combined sentence is passed to the println() function.
You can try this out by editing the example above.

1 Like

Very helpful and it prints

“My favorite number is: 5”

fun sayNumber() {
    println(5) // Here it really prints "5\n", notice the newline character \n
}

fun main() {
    print("My favorite number is: ")
    sayNumber()
}

But I don’t understand why it shouldn’t work the following way

> fun sayNumber() {
>             print(5) // Here it really prints "5\n", notice the newline character \n
>         }
> 
>         fun main() {
>             println("My favorite number is: ")
>             sayNumber()
>         }

It works fine but the 5 is in the next line in result.

“My favorite number is:
5”

we are a giving a println command, and than we called the saynumber function that has print(5) instead of println(5). In first example I thought println() of saynumber will print 5 in the next line but it does in the same line.

1 Like

@arocnies I think that I got it, Println() creates a new line after the result. I thought it creates a new line before printing the result. :slight_smile:

3 Likes