I was going through scala book by Martin Odersky. In chapter10(Composition and Inheritance), in this code:
def spiral(nEdges: Int, direction: Int): Element = { if (nEdges == 1) { elem("+") } else { println(s"nEdgesInside1=$nEdges") val sp = spiral(nEdges - 1, (direction + 3) % 4) def verticalBar = elem('|', 1, sp.height) def horizontalBar = elem('-', sp.width, 1) println(s"nEdgesInside2=$nEdges") if (direction == 0) (corner beside horizontalBar) above (sp beside space) else if (direction == 1) (sp above space) beside (corner above verticalBar) else if (direction == 2) (space beside sp) above (horizontalBar beside corner) else (verticalBar above corner) beside (space above sp) } } I am unable to understand how code flows beyond the line:
val sp = spiral(nEdges - 1, (direction + 3) % 4) Accoording to my understanding, nEdges is getting decremented in every iteration spiral function is called and once it reaches 1(i.e termination condition) it creates object via elem function.
When I test this code and print the values of nEdges, it gets decremented till the line above call of spiral function and after that line it starts to increment. Could anyone explain me how this is happening.
Thanks in advance
