First of all, you are promising to return Int from printSeries
. Why? If method is printing, isn't much more natural* to make it return Unit (aka void). This is usually can be done either explicitly
def printSeries(upTo: Int): Unit = { .... }
or implicitly:
def printSeries(upTo: Int) { .... }
(see, no equals sign in code above).
Next, why do you have explicit doNothing method? I think it would be better to write something like:
def printSeries(upTo: Int) {
if(upTo > 0) {
println(upTo)
printSeries(upTo - 1)
}
}
You also might want to place @tailrec annotation to ensure that code will be optimized and won't fail with Stackoverflow at big upTo
numbers (if it will and function is annotated compiler will yell on you).
@annotation.tailrec
def printSeries(upTo: Int) {
if(upTo > 0) {
println(upTo)
printSeries(upTo - 1)
}
}
Finally, generally, it is better to produce sequences and only then make something with them.
P.S. you might better to post questions like this on code review site, not on stackoverlow itself
* there is a good rule in programming to make functions do one thing and be good at it so getXandMakeY usually is bad idea.