Function unit vs float/Int

Viewed 58

I'm writing a simple function to take two variables and return a single float, that I'm trying to print.

fun main() {
    val fugacity = fo2(1200,1).NNO()
    println(fugacity)
}

class fo2(val Tc: Int,val P: Int) {
    
    fun NNO() {
        val Tk = Tc+273.15
        val fug = ((-24930.0/Tk)+9.36+(0.046*(P-1.0)/Tk))
        return fug.toString()
    }      
}

the output is an error 'Type mismatch: inferred type is String but Unit was expected'

I understand that the UNIT type is a null output but I don't understand why this simple sum's output is null. Can anyone help?

Cheers

Ed

1 Answers

If you want your function to return some value then you need to specify that in the function signature. In your particular case if you want to return a Float, then you should update your function as

fun NNO(): Float {
    val Tk = Tc+273.15
    return ((-24930.0/Tk)+9.36+(0.046*(P-1.0)/Tk)).toFloat()
}
Related