I have a recursive function that will repeat the function until the if condition is not met then output an integer. However the function outside this function that requires an integer is receiving a unit. How should I modify the code in order to return an int?
count(r,c,1,0)
def count(r: Int, c: Int, countR: Int, lalaCount: Int): Int = {
if (countR < (r + 1)) count(r,c,countR + 1, lalaCount + countR)
else (lalaCount + c + 1)
}
This is the whole program
object hw1 {
def pascal(c: Int, r: Int): Int = {
count(r,c,1,0)
def count(r: Int, c: Int, countR: Int, lalaCount: Int): Int = {
if (countR < (r + 1)) count(r,c,countR + 1, lalaCount + countR)
else (lalaCount + c + 1)
}
} //On this line eclipse is saying "Multiple markers at this line
//- type mismatch; found : Unit required: Int
//- type mismatch; found : Unit required: Int
pascal(3,4)
}
The value returned from
pascalis the last expression it contains. You want it to be your evaluation ofcountbut that’s not the last thing. Assignments (def, val etc) are of type Unit, as you’ve discovered:Just move
count(r,c,1,0)after thedefand that should fix the problem.