Recursively print in scala I have just picked up Scala like 2 hours ago, and I am thinking of printing a series like 1 to 10 but in a recursive fashion. I do not understand what is wrong with this:

def printSeries(x:Int):Int = {
  if(x==0) doNothing() else doSomething()

  def doNothing()  = {}

  def doSomething() = {
    println(x)
    printSeries(x-1)
  }

}


Sorry, this might be very basic. Please point out my mistakes or any alternative and simpler implementation. (I do not want to use loops).

Thanks,
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 &gt; 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 &gt; 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.